60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('downloadFile', {
|
|
description: 'Get the claim file',
|
|
accessType: 'READ',
|
|
accepts: [
|
|
{
|
|
arg: 'id',
|
|
type: 'Number',
|
|
description: 'The document id',
|
|
http: {source: 'path'}
|
|
}
|
|
],
|
|
returns: [
|
|
{
|
|
arg: 'body',
|
|
type: 'file',
|
|
root: true
|
|
},
|
|
{
|
|
arg: 'Content-Type',
|
|
type: 'String',
|
|
http: {target: 'header'}
|
|
},
|
|
{
|
|
arg: 'Content-Disposition',
|
|
type: 'String',
|
|
http: {target: 'header'}
|
|
}
|
|
],
|
|
http: {
|
|
path: `/:id/downloadFile`,
|
|
verb: 'GET'
|
|
}
|
|
});
|
|
|
|
Self.downloadFile = async function(ctx, id) {
|
|
const models = Self.app.models;
|
|
const ClaimContainer = models.ClaimContainer;
|
|
const dms = await models.Dms.findById(id);
|
|
const pathHash = ClaimContainer.getHash(dms.id);
|
|
try {
|
|
await ClaimContainer.getFile(pathHash, dms.file);
|
|
} catch (e) {
|
|
if (e.code != 'ENOENT')
|
|
throw e;
|
|
|
|
const error = new UserError(`File doesn't exists`);
|
|
error.statusCode = 404;
|
|
|
|
throw error;
|
|
}
|
|
|
|
const stream = ClaimContainer.downloadStream(pathHash, dms.file);
|
|
|
|
return [stream, dms.contentType, `filename="${dms.file}"`];
|
|
};
|
|
};
|