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) => {
|
Self.uploadFile = async(ctx, options) => {
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
const TempContainer = models.TempContainer;
|
const TempContainer = models.TempContainer;
|
||||||
const DmsContainer = models.DmsContainer;
|
|
||||||
const fileOptions = {};
|
const fileOptions = {};
|
||||||
const args = ctx.args;
|
const args = ctx.args;
|
||||||
|
|
||||||
|
@ -79,19 +78,21 @@ module.exports = Self => {
|
||||||
|
|
||||||
const addedDms = [];
|
const addedDms = [];
|
||||||
for (const uploadedFile of files) {
|
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);
|
const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name);
|
||||||
srcFile = path.join(file.client.root, file.container, file.name);
|
srcFile = path.join(file.client.root, file.container, file.name);
|
||||||
|
|
||||||
const dmsContainer = await DmsContainer.container(pathHash);
|
const data = {
|
||||||
const dstFile = path.join(dmsContainer.client.root, pathHash, newDms.file);
|
workerFk: ctx.req.accessToken.userId,
|
||||||
|
dmsTypeFk: args.dmsTypeId,
|
||||||
await fs.move(srcFile, dstFile, {
|
companyFk: args.companyId,
|
||||||
overwrite: true
|
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);
|
addedDms.push(newDms);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,27 +108,4 @@ module.exports = Self => {
|
||||||
throw e;
|
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 UserError = require('vn-loopback/util/user-error');
|
||||||
|
const fs = require('fs-extra');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
require('../methods/dms/downloadFile')(Self);
|
require('../methods/dms/downloadFile')(Self);
|
||||||
|
@ -35,4 +37,32 @@ module.exports = Self => {
|
||||||
|
|
||||||
return [stream, dms.contentType, `filename="${dms.file}"`];
|
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,7 +45,7 @@ module.exports = Self => {
|
||||||
const zipConfig = await models.ZipConfig.findOne(null, myOptions);
|
const zipConfig = await models.ZipConfig.findOne(null, myOptions);
|
||||||
let totalSize = 0;
|
let totalSize = 0;
|
||||||
ids = ids.split(',');
|
ids = ids.split(',');
|
||||||
try {
|
|
||||||
const baseUrl = (await Self.app.models.Url.getUrl()).replace('#!', 'api');
|
const baseUrl = (await Self.app.models.Url.getUrl()).replace('#!', 'api');
|
||||||
|
|
||||||
for (const id of ids) {
|
for (const id of ids) {
|
||||||
|
@ -63,14 +63,11 @@ module.exports = Self => {
|
||||||
if (response.headers['content-type'] !== 'application/pdf')
|
if (response.headers['content-type'] !== 'application/pdf')
|
||||||
throw new UserError(`The response is not a PDF`);
|
throw new UserError(`The response is not a PDF`);
|
||||||
|
|
||||||
zip.file(`${id}.pdf`, response.data, { binary: true });
|
zip.file(`${id}.pdf`, response.data, {binary: true});
|
||||||
}
|
}
|
||||||
|
|
||||||
const zipStream = zip.generateNodeStream({ streamFiles: true });
|
const zipStream = zip.generateNodeStream({streamFiles: true});
|
||||||
|
|
||||||
return [zipStream, 'application/zip', `filename="cmrs.zip"`];
|
return [zipStream, 'application/zip', `filename="cmrs.zip"`];
|
||||||
} catch (e) {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -41,31 +41,31 @@ module.exports = Self => {
|
||||||
|
|
||||||
if (ticket.cmrFk) {
|
if (ticket.cmrFk) {
|
||||||
const hasDmsCmr = await models.TicketDms.findOne({
|
const hasDmsCmr = await models.TicketDms.findOne({
|
||||||
where: { ticketFk: ticketId },
|
where: {ticketFk: ticketId},
|
||||||
include: {
|
include: {
|
||||||
relation: 'dms',
|
relation: 'dms',
|
||||||
fields: ['dmsFk'],
|
fields: ['dmsFk'],
|
||||||
scope: {
|
scope: {
|
||||||
where: { dmsTypeFk: dmsTypeCmr.id }
|
where: {dmsTypeFk: dmsTypeCmr.id}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, myOptions);
|
}, myOptions);
|
||||||
|
|
||||||
if (!hasDmsCmr.dms()) {
|
if (!hasDmsCmr.dms()) {
|
||||||
const zip = await models.Route.downloadCmrsZip(ctx, ticket.cmrFk.toString(), myOptions);
|
const zip = await models.Route.downloadCmrsZip(ctx, ticket.cmrFk.toString(), myOptions);
|
||||||
ctx.req.file = Object.assign({}, zip);
|
const stream = Object.assign({}, zip);
|
||||||
const ctxUploadFile = {
|
const data = {
|
||||||
...ctx,
|
workerFk: ctx.req.accessToken.userId,
|
||||||
args: {
|
dmsTypeFk: dmsTypeCmr.id,
|
||||||
warehouseId: ticket.warehouseFk,
|
companyFk: ticket.companyFk,
|
||||||
companyId: ticket.companyFk,
|
warehouseFk: ticket.warehouseFk,
|
||||||
dmsTypeId: dmsTypeCmr.id,
|
|
||||||
reference: ticket.id,
|
reference: ticket.id,
|
||||||
description: `${ticket.cmrFk} - ${ticket.id}`,
|
description: `${ticket.cmrFk} - ${ticket.id}`,
|
||||||
|
contentType: '?',
|
||||||
hasFile: false
|
hasFile: false
|
||||||
}
|
|
||||||
};
|
};
|
||||||
const dms = await models.Dms.uploadFile(ctxUploadFile, myOptions);
|
|
||||||
|
const dms = await models.Dms.createFromStream(data, 'zip', stream, myOptions);
|
||||||
await models.TicketDms.create({
|
await models.TicketDms.create({
|
||||||
ticketFk: ticketId,
|
ticketFk: ticketId,
|
||||||
dmsFk: dms[0].id
|
dmsFk: dms[0].id
|
||||||
|
|
Loading…
Reference in New Issue