cds init Attachment
namespace Media.db;
entity MediaFile {
key id : Integer;
@Core.MediaType : mediaType
content : LargeBinary;
@Core.IsMediaType : true
mediaType : String;
fileName : String;
url : String;
};
using {Media.db as db} from '../db/data-model';
service MediaService @(path : '/media') {
entity MediaFile as projection on db.MediaFile;
};
const SequenceHelper = require("./library/SequenceHelper");
const { Readable, PassThrough } = require("stream");
const cds = require('@sap/cds');
cds.env.features.fetch_csrf = true
module.exports = cds.service.impl(async function (srv) {
const {
MediaFile
} = this.entities;
/**
* Handler method called before creating data entry
* for entity Mediafile.
*/
srv.before('CREATE', MediaFile, async (req) => {
const db = await cds.connect.to("db");
// Create Constructor for SequenceHelper
// Pass the sequence name and db
const SeqReq = new SequenceHelper({
db: db,
sequence: "MEDIA_ID",
table: "MediaFile",
field: "ID"
});
//Call method getNextNumber() to fetch the next sequence number
let seq_no = await SeqReq.getNextNumber();
// Assign the sequence number to id element
req.data.id = seq_no;
//Assign the url by appending the id
req.data.url = `/media/MediaFile(${req.data.id})/content`;
});
/**
* Handler method called on reading data entry
* for entity Mediafile.
**/
srv.on("READ", MediaFile, async (req, next) => {
if (!req.data.id) {
return next();
}
//Fetch the url from where the req is triggered
const url = req._.req.path;
//If the request url contains keyword "content"
// then read the media content
if (url.includes("content")) {
const id = req.data.id;
var tx = cds.transaction(req);
// Fetch the media obj from database
var mediaObj = await tx.run(
SELECT.one.from("Media_db_MediaFile", ["content", "mediaType"]).where(
"id =",
id
)
);
if (mediaObj.length <= 0) {
req.reject(404, "Media not found for the ID");
return;
}
var decodedMedia = "";
decodedMedia = new Buffer.from(mediaObj.content, 'base64'
);
return _formatResult(decodedMedia, mediaObj.mediaType);
} else return next();
});
});
function _formatResult(decodedMedia, mediaType) {
const readable = new Readable();
readable.push(decodedMedia);
readable.push(null);
return {
value: readable,
'*@odata.mediaContentType': mediaType
}
}
module.exports = class SequenceHelper {
constructor (options) {
this.db = options.db;
this.sequence = options.sequence;
this.table = options.table;
this.field = options.field || "ID";
}
getNextNumber() {
return new Promise((resolve, reject) => {
let nextNumber = 0;
switch (this.db.kind) {
case "hana":
this.db.run(`SELECT "${this.sequence}".NEXTVAL FROM DUMMY`)
.then(result => {
nextNumber = result[0][`${this.sequence}.NEXTVAL`];
resolve(nextNumber);
})
.catch(error => {
reject(error);
});
break;
case "sql":
case "sqlite":
default:
reject(new Error(`Unsupported DB kind --> ${this.db.kind}`));
}
});
}
};
cds add hana
SEQUENCE "MEDIA_ID" START WITH 1 MAXVALUE 2999999999
cds deploy --to hana
npm install
cds watch --profile hybrid
cds import API_CV_ATTACHMENT_SRV.edmx
const SequenceHelper = require("./library/SequenceHelper");
const { Readable, PassThrough } = require("stream");
const cds = require('@sap/cds');
cds.env.features.fetch_csrf = true
module.exports = cds.service.impl(async function (srv) {
const {
MediaFile
} = this.entities;
/**
* Handler method called before creating data entry
* for entity Mediafile.
*/
const InvoiceAttachment = await cds.connect.to('API_CV_ATTACHMENT_SRV');
srv.before('CREATE', MediaFile, async (req) => {
const db = await cds.connect.to("db");
// Create Constructor for SequenceHelper
// Pass the sequence name and db
const SeqReq = new SequenceHelper({
db: db,
sequence: "MEDIA_ID",
table: "MediaFile",
field: "ID"
});
//Call method getNextNumber() to fetch the next sequence number
let seq_no = await SeqReq.getNextNumber();
// Assign the sequence number to id element
req.data.id = seq_no;
//Assign the url by appending the id
req.data.url = `/media/MediaFile(${req.data.id})/content`;
var host = req.headers.host;
const PostingResult = await InvoiceAttachment.send('CreateUrlAsAttachment',
{ 'MIMEType': 'text/html', 'UrlDescription': 'blog', 'Url': 'http://' + host + req.data.url,
'BusinessObjectTypeName': 'BUS2012','LinkedSAPObjectKey': '4500000###', 'x-csrf-fetch':'fetch'});
});
/**
* Handler method called on reading data entry
* for entity Mediafile.
**/
srv.on("READ", MediaFile, async (req, next) => {
if (!req.data.id) {
return next();
}
//Fetch the url from where the req is triggered
const url = req._.req.path;
//If the request url contains keyword "content"
// then read the media content
if (url.includes("content")) {
const id = req.data.id;
var tx = cds.transaction(req);
// Fetch the media obj from database
var mediaObj = await tx.run(
SELECT.one.from("Media_db_MediaFile", ["content", "mediaType"]).where(
"id =",
id
)
);
if (mediaObj.length <= 0) {
req.reject(404, "Media not found for the ID");
return;
}
var decodedMedia = "";
decodedMedia = new Buffer.from(mediaObj.content, 'base64' );
return _formatResult(decodedMedia, mediaObj.mediaType);
} else return next();
});
});
function _formatResult(decodedMedia, mediaType) {
const readable = new Readable();
readable.push(decodedMedia);
readable.push(null);
return {
value: readable,
'*@odata.mediaContentType': mediaType
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
11 | |
11 | |
9 | |
7 | |
5 | |
4 | |
4 | |
4 | |
4 | |
3 |