
{
"@odata.context": "../$metadata#Media(ae86a20e-015b-4766-82c6-a1a97001a432)/fileName",
"value": "bird.jpg"
}
bird.jpg
entity Media : cuid, managed {
@Core.MediaType : mediaType
media : LargeBinary;
@Core.IsMediaType : true
mediaType : String(50); //map to mine
url: String(1000); //full URL of the image. for easy consume by front-end
desc: String(200);
@Core.MediaType : mediaType
@Core.ContentDisposition.Filename: fileName
@Core.ContentDisposition.Type: 'attachment' //inline
virtual mediaDownload : LargeBinary; //share the same content as the media
fileName: String(200);
};
service MediaService {
entity Media as projection on schema.Media actions {
//current we can't find good way to directly return the Edm.Stream format
function download() returns LargeBinary;
}
}
{
"@odata.context":"../$metadata#Edm.Binary",
"value": "base_64_format_data"
}
//Only the Admin can access the Images. for read, will check in the on("READ")
this.before(['CREATE', 'UPDATE', 'DELETE'], Media, req =>
req.user.is('Admin') || req.reject(403)
);
//for the update, manually set the url and decode media from base64
this.before(["CREATE", "UPDATE"], Media, async (req) => {
let ID = req.data.ID;
if (!req.data.url) {
req.data.url = `${SRV_URL}/media/Media(${ID})/media`;
}
//the media content passed from front-end is base64 encode, just convert into binary
if (req.data.media) {
req.data.media = new Buffer.from(
req.data.media,
"base64"
);
}
});
/**
* Handler method called on mediaDownload
**/
this.on("READ", Media, async (req, next) => {
if (!req.data.ID) {
//for the normal read, need Admin scope
req.user.is('Admin') || req.reject(403)
return next();
}
const url = req._.req.path;
if (url.includes("/mediaDownload")) {
return await downloadMedia(Media, req);
} else if (url.includes("/media")) {
//for read the media, let it public
return next();
} else {
//for the normal read, need Admin scope
req.user.is('Admin') || req.reject(403)
return next();
}
});
async function downloadMedia(Media, req, forFunction = false) {
//!!for read the image content, let it public for simple
let ID = req.data.ID;
if (!ID) {
//like the Media(ee4087ee-9af9-46cd-9d2d-c393bb415ff5)/download()
let match = /Media\((.*)\)\/download/.exec(req._.req.url);
if (match) {
ID = match[1];
}
}
let tx = cds.transaction(req);
// Fetch the media obj from database
let mediaObj = await tx.run(
SELECT.one.from(Media, ["media", "mediaType", "fileName"]).where(
"ID =",
ID
)
);
if (!mediaObj) {
req.reject(501, `"Internal error. Can't get the object with ID ${ID} from DB`);
return;
}
if (!mediaObj.media || mediaObj.length <= 0) {
req.reject(404, "Media not found for the ID " + ID);
return;
}
//when add the 'virtual', then we need to set the http header to support download manually
req._.res.setHeader('Content-Disposition', `attachment; filename="${mediaObj.fileName}"`);
if (forFunction) {
//The Binary will try to return as the base64 format
return mediaObj.media;
} else {
return _formatResult(mediaObj.media, mediaObj.mediaType);
}
}
function _formatResult(mediaContent, mediaType) {
const readable = new Readable();
readable.push(mediaContent);
readable.push(null);
return {
value: readable,
'*@odata.mediaContentType': mediaType
};
}
Image Management
Image Upload
onImageUploadChanged: function (oEvent) {
let source = oEvent.getSource();
this.oSourceFile = oEvent.getParameter("files") && oEvent.getParameter("files")[0];
if (this.oSourceFile && window.FileReader) {
let reader = new FileReader();
let self = this;
reader.onload = function (evn) {
let text = evn.target.result;
self.onImageLoaded(text);
//from the file name guess the mine type
let fileName = source.getValue(); //like my.jpg
let idx = fileName.lastIndexOf(".");
self.imageType = fileName.slice(idx + 1);
self.fileName = fileName;
};
reader.readAsDataURL(this.oSourceFile);
}
},
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
16 | |
13 | |
11 | |
10 | |
10 | |
9 | |
8 | |
8 | |
8 | |
7 |