Don't throw error on file unlink
gitea/salix/pipeline/head Something is wrong with the build of this commit Details

This commit is contained in:
Joan Sanchez 2023-03-16 07:18:29 +01:00
parent a947666838
commit 596a017cfd
1 changed files with 66 additions and 56 deletions

View File

@ -11,14 +11,19 @@ module.exports = Self => {
accessType: 'WRITE', accessType: 'WRITE',
http: { http: {
path: `/download`, path: `/download`,
verb: 'POST' verb: 'POST',
} },
}); });
Self.download = async () => { Self.download = async () => {
const models = Self.app.models; const models = Self.app.models;
const tempContainer = await models.TempContainer.container('salix-image'); const tempContainer = await models.TempContainer.container(
const tempPath = path.join(tempContainer.client.root, tempContainer.name); 'salix-image'
);
const tempPath = path.join(
tempContainer.client.root,
tempContainer.name
);
const maxAttempts = 3; const maxAttempts = 3;
const collectionName = 'catalog'; const collectionName = 'catalog';
@ -29,55 +34,58 @@ module.exports = Self => {
try { try {
const myOptions = { transaction: tx }; const myOptions = { transaction: tx };
queueRow = await Self.findOne({ queueRow = await Self.findOne(
fields: [ {
'id', fields: ['id', 'itemFk', 'url', 'attempts'],
'itemFk',
'url',
'attempts'
],
where: { where: {
url: { neq: null }, url: { neq: null },
attempts: { attempts: {
lt: maxAttempts lt: maxAttempts,
}
}, },
order: 'priority, attempts, updated' },
}, myOptions); order: 'priority, attempts, updated',
},
myOptions
);
if (!queueRow) return; if (!queueRow) return;
const collection = await models.ImageCollection.findOne({ const collection = await models.ImageCollection.findOne(
{
fields: [ fields: [
'id', 'id',
'maxWidth', 'maxWidth',
'maxHeight', 'maxHeight',
'model', 'model',
'property' 'property',
], ],
where: { name: collectionName }, where: { name: collectionName },
include: { include: {
relation: 'sizes', relation: 'sizes',
scope: { scope: {
fields: ['width', 'height', 'crop'] fields: ['width', 'height', 'crop'],
} },
} },
}, myOptions); },
myOptions
);
const fileName = `${uuid.v4()}.png`; const fileName = `${uuid.v4()}.png`;
tempFilePath = path.join(tempPath, fileName); tempFilePath = path.join(tempPath, fileName);
// Insert image row // Insert image row
await models.Image.create({ await models.Image.create(
{
name: fileName, name: fileName,
collectionFk: collectionName, collectionFk: collectionName,
updated: Date.vnNow() updated: Date.vnNow(),
}, myOptions); },
myOptions
);
// Update item // Update item
const model = models[collection.model]; const model = models[collection.model];
if (!model) if (!model) throw new Error('No matching model found');
throw new Error('No matching model found');
const item = await model.findById(queueRow.itemFk, null, myOptions); const item = await model.findById(queueRow.itemFk, null, myOptions);
if (item) { if (item) {
@ -101,7 +109,9 @@ module.exports = Self => {
}); });
// Resize // Resize
const container = await models.ImageContainer.container(collectionName); const container = await models.ImageContainer.container(
collectionName
);
const rootPath = container.client.root; const rootPath = container.client.root;
const collectionDir = path.join(rootPath, collectionName); const collectionDir = path.join(rootPath, collectionName);
@ -139,8 +149,7 @@ module.exports = Self => {
.crop(width, height); .crop(width, height);
} }
if (!size.crop) if (!size.crop) gmInstance.resize(width, height, '>');
gmInstance.resize(width, height, '>');
gmInstance gmInstance
.setFormat('png') .setFormat('png')
@ -151,8 +160,9 @@ module.exports = Self => {
}); });
} }
if (await fs.stat(tempFilePath)) try {
await fs.unlink(tempFilePath); await fs.unlink(tempFilePath);
} catch (error) { }
await queueRow.destroy(myOptions); await queueRow.destroy(myOptions);
@ -167,7 +177,7 @@ module.exports = Self => {
await queueRow.updateAttributes({ await queueRow.updateAttributes({
error: error, error: error,
attempts: queueRow.attempts + 1, attempts: queueRow.attempts + 1,
updated: Date.vnNew() updated: Date.vnNew(),
}); });
} }