2020-04-03 15:52:47 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2024-01-17 12:07:26 +00:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
2020-04-03 15:52:47 +00:00
|
|
|
|
2019-05-01 16:49:39 +00:00
|
|
|
module.exports = Self => {
|
2019-06-06 11:59:11 +00:00
|
|
|
require('../methods/dms/downloadFile')(Self);
|
|
|
|
require('../methods/dms/uploadFile')(Self);
|
|
|
|
require('../methods/dms/removeFile')(Self);
|
2019-07-15 09:40:11 +00:00
|
|
|
require('../methods/dms/updateFile')(Self);
|
2022-05-03 09:16:56 +00:00
|
|
|
require('../methods/dms/deleteTrashFiles')(Self);
|
2020-04-03 15:52:47 +00:00
|
|
|
|
|
|
|
Self.checkRole = async function(ctx, id) {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const dms = await Self.findById(id);
|
|
|
|
|
|
|
|
return await models.DmsType.hasReadRole(ctx, dms.dmsTypeFk);
|
|
|
|
};
|
|
|
|
|
|
|
|
Self.getFile = async function(id) {
|
|
|
|
const models = Self.app.models;
|
2020-12-18 16:23:04 +00:00
|
|
|
const DmsContainer = models.DmsContainer;
|
2020-04-03 15:52:47 +00:00
|
|
|
const dms = await Self.findById(id);
|
2020-12-18 16:23:04 +00:00
|
|
|
const pathHash = DmsContainer.getHash(dms.id);
|
2020-04-03 15:52:47 +00:00
|
|
|
try {
|
2020-12-18 16:23:04 +00:00
|
|
|
await DmsContainer.getFile(pathHash, dms.file);
|
2020-04-03 15:52:47 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (e.code != 'ENOENT')
|
|
|
|
throw e;
|
|
|
|
|
|
|
|
const error = new UserError(`File doesn't exists`);
|
|
|
|
error.statusCode = 404;
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2020-12-18 16:23:04 +00:00
|
|
|
const stream = DmsContainer.downloadStream(pathHash, dms.file);
|
2020-04-03 15:52:47 +00:00
|
|
|
|
|
|
|
return [stream, dms.contentType, `filename="${dms.file}"`];
|
|
|
|
};
|
2024-01-17 12:07:26 +00:00
|
|
|
|
|
|
|
Self.getPath = async function(dms) {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const pathHash = await models.DmsContainer.getHash(dms.id);
|
|
|
|
const dmsContainer = await models.DmsContainer.container(pathHash);
|
|
|
|
const dstFile = path.join(dmsContainer.client.root, pathHash, dms.file);
|
|
|
|
return dstFile;
|
|
|
|
};
|
|
|
|
|
|
|
|
Self.createWithExtension = async function(data, extension, options) {
|
|
|
|
const newDms = await Self.create(data, options);
|
|
|
|
return newDms.updateAttribute('file', `${newDms.id}.${extension}`, options);
|
|
|
|
};
|
|
|
|
|
|
|
|
Self.createFromFile = async function(data, extension, srcFile, options) {
|
|
|
|
const dms = await Self.createWithExtension(data, extension, options);
|
|
|
|
const dstFile = await Self.getPath(dms);
|
|
|
|
await fs.move(srcFile, dstFile, {overwrite: true});
|
|
|
|
return dms;
|
|
|
|
};
|
|
|
|
|
|
|
|
Self.createFromStream = async function(data, extension, stream, options) {
|
|
|
|
const dms = await Self.createWithExtension(data, extension, options);
|
|
|
|
const dstFile = await Self.getPath(dms);
|
|
|
|
const writeStream = await fs.createWriteStream(dstFile);
|
2024-01-17 13:18:31 +00:00
|
|
|
await stream.pipe(writeStream);
|
2024-01-17 12:07:26 +00:00
|
|
|
return dms;
|
|
|
|
};
|
2019-05-01 16:49:39 +00:00
|
|
|
};
|