Don't throw error on file unlink
gitea/salix/pipeline/head Something is wrong with the build of this commit
Details
gitea/salix/pipeline/head Something is wrong with the build of this commit
Details
This commit is contained in:
parent
a947666838
commit
596a017cfd
|
@ -1,7 +1,7 @@
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const uuid = require('uuid');
|
const uuid = require('uuid');
|
||||||
const fs = require('fs/promises');
|
const fs = require('fs/promises');
|
||||||
const {createWriteStream} = require('fs');
|
const { createWriteStream } = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const gm = require('gm');
|
const gm = require('gm');
|
||||||
|
|
||||||
|
@ -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';
|
||||||
|
|
||||||
|
@ -27,57 +32,60 @@ module.exports = Self => {
|
||||||
let tempFilePath;
|
let tempFilePath;
|
||||||
let queueRow;
|
let queueRow;
|
||||||
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,21 +109,23 @@ 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);
|
||||||
|
|
||||||
// To max size
|
// To max size
|
||||||
const {maxWidth, maxHeight} = collection;
|
const { maxWidth, maxHeight } = collection;
|
||||||
const fullSizePath = path.join(collectionDir, 'full');
|
const fullSizePath = path.join(collectionDir, 'full');
|
||||||
const toFullSizePath = `${fullSizePath}/${fileName}`;
|
const toFullSizePath = `${fullSizePath}/${fileName}`;
|
||||||
|
|
||||||
await fs.mkdir(fullSizePath, {recursive: true});
|
await fs.mkdir(fullSizePath, { recursive: true });
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
gm(tempFilePath)
|
gm(tempFilePath)
|
||||||
.resize(maxWidth, maxHeight, '>')
|
.resize(maxWidth, maxHeight, '>')
|
||||||
.setFormat('png')
|
.setFormat('png')
|
||||||
.write(toFullSizePath, function(err) {
|
.write(toFullSizePath, function (err) {
|
||||||
if (err) reject(err);
|
if (err) reject(err);
|
||||||
if (!err) resolve();
|
if (!err) resolve();
|
||||||
});
|
});
|
||||||
|
@ -123,12 +133,12 @@ module.exports = Self => {
|
||||||
|
|
||||||
// To collection sizes
|
// To collection sizes
|
||||||
for (const size of 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 sizePath = path.join(collectionDir, `${width}x${height}`);
|
||||||
const toSizePath = `${sizePath}/${fileName}`;
|
const toSizePath = `${sizePath}/${fileName}`;
|
||||||
|
|
||||||
await fs.mkdir(sizePath, {recursive: true});
|
await fs.mkdir(sizePath, { recursive: true });
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
const gmInstance = gm(tempFilePath);
|
const gmInstance = gm(tempFilePath);
|
||||||
|
|
||||||
|
@ -139,20 +149,20 @@ 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')
|
||||||
.write(toSizePath, function(err) {
|
.write(toSizePath, function (err) {
|
||||||
if (err) reject(err);
|
if (err) reject(err);
|
||||||
if (!err) resolve();
|
if (!err) resolve();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
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,13 +177,13 @@ 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(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fs.unlink(tempFilePath);
|
await fs.unlink(tempFilePath);
|
||||||
} catch (error) {}
|
} catch (error) { }
|
||||||
|
|
||||||
Self.download();
|
Self.download();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue