feat: #6184 Modified uploadFile
gitea/salix/pipeline/head There was a failure building this commit
Details
gitea/salix/pipeline/head There was a failure building this commit
Details
This commit is contained in:
parent
f1b84ee7a0
commit
45fd96cee6
|
@ -49,7 +49,6 @@ module.exports = Self => {
|
|||
Self.uploadFile = async(ctx, options) => {
|
||||
const models = Self.app.models;
|
||||
const TempContainer = models.TempContainer;
|
||||
const DmsContainer = models.DmsContainer;
|
||||
const fileOptions = {};
|
||||
const args = ctx.args;
|
||||
|
||||
|
@ -79,19 +78,21 @@ module.exports = Self => {
|
|||
|
||||
const addedDms = [];
|
||||
for (const uploadedFile of files) {
|
||||
const newDms = await createDms(ctx, uploadedFile, myOptions);
|
||||
const pathHash = DmsContainer.getHash(newDms.id);
|
||||
|
||||
const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name);
|
||||
srcFile = path.join(file.client.root, file.container, file.name);
|
||||
|
||||
const dmsContainer = await DmsContainer.container(pathHash);
|
||||
const dstFile = path.join(dmsContainer.client.root, pathHash, newDms.file);
|
||||
|
||||
await fs.move(srcFile, dstFile, {
|
||||
overwrite: true
|
||||
});
|
||||
|
||||
const data = {
|
||||
workerFk: ctx.req.accessToken.userId,
|
||||
dmsTypeFk: args.dmsTypeId,
|
||||
companyFk: args.companyId,
|
||||
warehouseFk: args.warehouseId,
|
||||
reference: args.reference,
|
||||
description: args.description,
|
||||
contentType: file.type,
|
||||
hasFile: args.hasFile
|
||||
};
|
||||
const extension = await models.DmsContainer.getFileExtension(uploadedFile.name);
|
||||
const newDms = await Self.createFromFile(data, extension, srcFile, myOptions);
|
||||
addedDms.push(newDms);
|
||||
}
|
||||
|
||||
|
@ -107,27 +108,4 @@ module.exports = Self => {
|
|||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
async function createDms(ctx, file, myOptions) {
|
||||
const models = Self.app.models;
|
||||
const myUserId = ctx.req.accessToken.userId;
|
||||
const args = ctx.args;
|
||||
|
||||
const newDms = await Self.create({
|
||||
workerFk: myUserId,
|
||||
dmsTypeFk: args.dmsTypeId,
|
||||
companyFk: args.companyId,
|
||||
warehouseFk: args.warehouseId,
|
||||
reference: args.reference,
|
||||
description: args.description,
|
||||
contentType: file.type,
|
||||
hasFile: args.hasFile
|
||||
}, myOptions);
|
||||
|
||||
let fileName = file.name;
|
||||
const extension = models.DmsContainer.getFileExtension(fileName);
|
||||
fileName = `${newDms.id}.${extension}`;
|
||||
|
||||
return newDms.updateAttribute('file', fileName, myOptions);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
const UserError = require('vn-loopback/util/user-error');
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
module.exports = Self => {
|
||||
require('../methods/dms/downloadFile')(Self);
|
||||
|
@ -35,4 +37,32 @@ module.exports = Self => {
|
|||
|
||||
return [stream, dms.contentType, `filename="${dms.file}"`];
|
||||
};
|
||||
|
||||
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);
|
||||
await readStream.pipe(writeStream);
|
||||
return dms;
|
||||
};
|
||||
};
|
||||
|
|
|
@ -45,14 +45,14 @@ module.exports = Self => {
|
|||
const zipConfig = await models.ZipConfig.findOne(null, myOptions);
|
||||
let totalSize = 0;
|
||||
ids = ids.split(',');
|
||||
try {
|
||||
const baseUrl = (await Self.app.models.Url.getUrl()).replace('#!', 'api');
|
||||
|
||||
for (const id of ids) {
|
||||
if (zipConfig && totalSize > zipConfig.maxSize) throw new UserError('Files are too large');
|
||||
const baseUrl = (await Self.app.models.Url.getUrl()).replace('#!', 'api');
|
||||
|
||||
const response = await axios.get(
|
||||
`${baseUrl}Routes/${id}/cmr`, {
|
||||
for (const id of ids) {
|
||||
if (zipConfig && totalSize > zipConfig.maxSize) throw new UserError('Files are too large');
|
||||
|
||||
const response = await axios.get(
|
||||
`${baseUrl}Routes/${id}/cmr`, {
|
||||
...myOptions,
|
||||
headers: {
|
||||
Authorization: ctx.req.accessToken.id
|
||||
|
@ -60,17 +60,14 @@ module.exports = Self => {
|
|||
responseType: 'arraybuffer',
|
||||
});
|
||||
|
||||
if (response.headers['content-type'] !== 'application/pdf')
|
||||
throw new UserError(`The response is not a PDF`);
|
||||
if (response.headers['content-type'] !== 'application/pdf')
|
||||
throw new UserError(`The response is not a PDF`);
|
||||
|
||||
zip.file(`${id}.pdf`, response.data, { binary: true });
|
||||
}
|
||||
|
||||
const zipStream = zip.generateNodeStream({ streamFiles: true });
|
||||
|
||||
return [zipStream, 'application/zip', `filename="cmrs.zip"`];
|
||||
} catch (e) {
|
||||
throw e;
|
||||
zip.file(`${id}.pdf`, response.data, {binary: true});
|
||||
}
|
||||
|
||||
const zipStream = zip.generateNodeStream({streamFiles: true});
|
||||
|
||||
return [zipStream, 'application/zip', `filename="cmrs.zip"`];
|
||||
};
|
||||
};
|
||||
|
|
|
@ -41,31 +41,31 @@ module.exports = Self => {
|
|||
|
||||
if (ticket.cmrFk) {
|
||||
const hasDmsCmr = await models.TicketDms.findOne({
|
||||
where: { ticketFk: ticketId },
|
||||
where: {ticketFk: ticketId},
|
||||
include: {
|
||||
relation: 'dms',
|
||||
fields: ['dmsFk'],
|
||||
scope: {
|
||||
where: { dmsTypeFk: dmsTypeCmr.id }
|
||||
where: {dmsTypeFk: dmsTypeCmr.id}
|
||||
}
|
||||
}
|
||||
}, myOptions);
|
||||
|
||||
if (!hasDmsCmr.dms()) {
|
||||
const zip = await models.Route.downloadCmrsZip(ctx, ticket.cmrFk.toString(), myOptions);
|
||||
ctx.req.file = Object.assign({}, zip);
|
||||
const ctxUploadFile = {
|
||||
...ctx,
|
||||
args: {
|
||||
warehouseId: ticket.warehouseFk,
|
||||
companyId: ticket.companyFk,
|
||||
dmsTypeId: dmsTypeCmr.id,
|
||||
reference: ticket.id,
|
||||
description: `${ticket.cmrFk} - ${ticket.id}`,
|
||||
hasFile: false
|
||||
}
|
||||
const stream = Object.assign({}, zip);
|
||||
const data = {
|
||||
workerFk: ctx.req.accessToken.userId,
|
||||
dmsTypeFk: dmsTypeCmr.id,
|
||||
companyFk: ticket.companyFk,
|
||||
warehouseFk: ticket.warehouseFk,
|
||||
reference: ticket.id,
|
||||
description: `${ticket.cmrFk} - ${ticket.id}`,
|
||||
contentType: '?',
|
||||
hasFile: false
|
||||
};
|
||||
const dms = await models.Dms.uploadFile(ctxUploadFile, myOptions);
|
||||
|
||||
const dms = await models.Dms.createFromStream(data, 'zip', stream, myOptions);
|
||||
await models.TicketDms.create({
|
||||
ticketFk: ticketId,
|
||||
dmsFk: dms[0].id
|
||||
|
|
Loading…
Reference in New Issue