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

@ -1,7 +1,7 @@
const axios = require('axios');
const uuid = require('uuid');
const fs = require('fs/promises');
const {createWriteStream} = require('fs');
const { createWriteStream } = require('fs');
const path = require('path');
const gm = require('gm');
@ -11,14 +11,19 @@ module.exports = Self => {
accessType: 'WRITE',
http: {
path: `/download`,
verb: 'POST'
}
verb: 'POST',
},
});
Self.download = async() => {
Self.download = async () => {
const models = Self.app.models;
const tempContainer = await models.TempContainer.container('salix-image');
const tempPath = path.join(tempContainer.client.root, tempContainer.name);
const tempContainer = await models.TempContainer.container(
'salix-image'
);
const tempPath = path.join(
tempContainer.client.root,
tempContainer.name
);
const maxAttempts = 3;
const collectionName = 'catalog';
@ -27,57 +32,60 @@ module.exports = Self => {
let tempFilePath;
let queueRow;
try {
const myOptions = {transaction: tx};
const myOptions = { transaction: tx };
queueRow = await Self.findOne({
fields: [
'id',
'itemFk',
'url',
'attempts'
],
where: {
url: {neq: null},
attempts: {
lt: maxAttempts
}
queueRow = await Self.findOne(
{
fields: ['id', 'itemFk', 'url', 'attempts'],
where: {
url: { neq: null },
attempts: {
lt: maxAttempts,
},
},
order: 'priority, attempts, updated',
},
order: 'priority, attempts, updated'
}, myOptions);
myOptions
);
if (!queueRow) return;
const collection = await models.ImageCollection.findOne({
fields: [
'id',
'maxWidth',
'maxHeight',
'model',
'property'
],
where: {name: collectionName},
include: {
relation: 'sizes',
scope: {
fields: ['width', 'height', 'crop']
}
}
}, myOptions);
const collection = await models.ImageCollection.findOne(
{
fields: [
'id',
'maxWidth',
'maxHeight',
'model',
'property',
],
where: { name: collectionName },
include: {
relation: 'sizes',
scope: {
fields: ['width', 'height', 'crop'],
},
},
},
myOptions
);
const fileName = `${uuid.v4()}.png`;
tempFilePath = path.join(tempPath, fileName);
// Insert image row
await models.Image.create({
name: fileName,
collectionFk: collectionName,
updated: Date.vnNow()
}, myOptions);
await models.Image.create(
{
name: fileName,
collectionFk: collectionName,
updated: Date.vnNow(),
},
myOptions
);
// Update item
const model = models[collection.model];
if (!model)
throw new Error('No matching model found');
if (!model) throw new Error('No matching model found');
const item = await model.findById(queueRow.itemFk, null, myOptions);
if (item) {
@ -101,21 +109,23 @@ module.exports = Self => {
});
// Resize
const container = await models.ImageContainer.container(collectionName);
const container = await models.ImageContainer.container(
collectionName
);
const rootPath = container.client.root;
const collectionDir = path.join(rootPath, collectionName);
// To max size
const {maxWidth, maxHeight} = collection;
const { maxWidth, maxHeight } = collection;
const fullSizePath = path.join(collectionDir, 'full');
const toFullSizePath = `${fullSizePath}/${fileName}`;
await fs.mkdir(fullSizePath, {recursive: true});
await fs.mkdir(fullSizePath, { recursive: true });
await new Promise((resolve, reject) => {
gm(tempFilePath)
.resize(maxWidth, maxHeight, '>')
.setFormat('png')
.write(toFullSizePath, function(err) {
.write(toFullSizePath, function (err) {
if (err) reject(err);
if (!err) resolve();
});
@ -123,12 +133,12 @@ module.exports = Self => {
// To collection sizes
for (const size of collection.sizes()) {
const {width, height} = size;
const { width, height } = size;
const sizePath = path.join(collectionDir, `${width}x${height}`);
const toSizePath = `${sizePath}/${fileName}`;
await fs.mkdir(sizePath, {recursive: true});
await fs.mkdir(sizePath, { recursive: true });
await new Promise((resolve, reject) => {
const gmInstance = gm(tempFilePath);
@ -139,20 +149,20 @@ module.exports = Self => {
.crop(width, height);
}
if (!size.crop)
gmInstance.resize(width, height, '>');
if (!size.crop) gmInstance.resize(width, height, '>');
gmInstance
.setFormat('png')
.write(toSizePath, function(err) {
.write(toSizePath, function (err) {
if (err) reject(err);
if (!err) resolve();
});
});
}
if (await fs.stat(tempFilePath))
try {
await fs.unlink(tempFilePath);
} catch (error) { }
await queueRow.destroy(myOptions);
@ -167,13 +177,13 @@ module.exports = Self => {
await queueRow.updateAttributes({
error: error,
attempts: queueRow.attempts + 1,
updated: Date.vnNew()
updated: Date.vnNew(),
});
}
try {
await fs.unlink(tempFilePath);
} catch (error) {}
} catch (error) { }
Self.download();
}