refs #5772 Fix for unhandled ENOENT error
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Juan Ferrer 2023-06-23 22:02:33 +02:00
parent c9f11830a2
commit 665339c8d2
2 changed files with 36 additions and 36 deletions

View File

@ -42,42 +42,42 @@ module.exports = Self => {
if (typeof options == 'object')
Object.assign(myOptions, options);
const invoiceOut = await models.InvoiceOut.findById(id, {
fields: ['ref', 'issued']
}, myOptions);
const issued = invoiceOut.issued;
const year = issued.getFullYear().toString();
const month = (issued.getMonth() + 1).toString();
const day = issued.getDate().toString();
const container = await models.InvoiceContainer.container(year);
const rootPath = container.client.root;
const src = path.join(rootPath, year, month, day);
const fileName = `${year}${invoiceOut.ref}.pdf`;
const fileSrc = path.join(src, fileName);
const file = {
path: fileSrc,
contentType: 'application/pdf',
name: fileName
};
try {
const invoiceOut = await models.InvoiceOut.findById(id, {
fields: ['ref', 'issued']
}, myOptions);
const issued = invoiceOut.issued;
const year = issued.getFullYear().toString();
const month = (issued.getMonth() + 1).toString();
const day = issued.getDate().toString();
const container = await models.InvoiceContainer.container(year);
const rootPath = container.client.root;
const src = path.join(rootPath, year, month, day);
const fileName = `${year}${invoiceOut.ref}.pdf`;
const fileSrc = path.join(src, fileName);
const file = {
path: fileSrc,
contentType: 'application/pdf',
name: fileName
};
try {
await fs.access(file.path);
} catch (error) {
await Self.createPdf(ctx, id, myOptions);
}
const stream = fs.createReadStream(file.path);
return [stream, file.contentType, `filename="${file.name}"`];
await fs.access(file.path);
} catch (error) {
if (error.code === 'ENOENT')
throw new UserError('The PDF document does not exist');
throw error;
await Self.createPdf(ctx, id, myOptions);
}
const stream = await fs.createReadStream(file.path);
// XXX: To prevent unhandled ENOENT error
// https://stackoverflow.com/questions/17136536/is-enoent-from-fs-createreadstream-uncatchable
stream.on('error', err => {
const e = new Error(err.message);
err.stack = e.stack;
console.error(err);
});
return [stream, file.contentType, `filename="${file.name}"`];
};
};

View File

@ -63,12 +63,12 @@ class Email extends Component {
await getAttachments(componentPath, component.attachments);
if (component.components)
await getSubcomponentAttachments(component)
await getSubcomponentAttachments(component);
}
}
}
await getSubcomponentAttachments(instance)
await getSubcomponentAttachments(instance);
if (this.attachments)
await getAttachments(this.path, this.attachments);