Merge branch 'dev' into 4954-client-isTaxDataChecked-businessTypeFk
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:
commit
c823a0d6f1
|
@ -2,6 +2,7 @@
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethod('changePassword', {
|
Self.remoteMethod('changePassword', {
|
||||||
description: 'Changes the user password',
|
description: 'Changes the user password',
|
||||||
|
accessType: 'WRITE',
|
||||||
accepts: [
|
accepts: [
|
||||||
{
|
{
|
||||||
arg: 'id',
|
arg: 'id',
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethod('setPassword', {
|
Self.remoteMethod('setPassword', {
|
||||||
description: 'Sets the user password',
|
description: 'Sets the user password',
|
||||||
|
accessType: 'WRITE',
|
||||||
accepts: [
|
accepts: [
|
||||||
{
|
{
|
||||||
arg: 'id',
|
arg: 'id',
|
||||||
|
|
|
@ -4,7 +4,8 @@
|
||||||
"options": {
|
"options": {
|
||||||
"mysql": {
|
"mysql": {
|
||||||
"table": "salix.User"
|
"table": "salix.User"
|
||||||
}
|
},
|
||||||
|
"resetPasswordTokenTTL": "604800"
|
||||||
},
|
},
|
||||||
"properties": {
|
"properties": {
|
||||||
"id": {
|
"id": {
|
||||||
|
|
|
@ -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');
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ module.exports = Self => {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.download = async () => {
|
Self.download = async() => {
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
const tempContainer = await models.TempContainer.container(
|
const tempContainer = await models.TempContainer.container(
|
||||||
'salix-image'
|
'salix-image'
|
||||||
|
@ -32,13 +32,13 @@ 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', 'itemFk', 'url', 'attempts'],
|
fields: ['id', 'itemFk', 'url', 'attempts'],
|
||||||
where: {
|
where: {
|
||||||
url: { neq: null },
|
url: {neq: null},
|
||||||
attempts: {
|
attempts: {
|
||||||
lt: maxAttempts,
|
lt: maxAttempts,
|
||||||
},
|
},
|
||||||
|
@ -59,7 +59,7 @@ module.exports = Self => {
|
||||||
'model',
|
'model',
|
||||||
'property',
|
'property',
|
||||||
],
|
],
|
||||||
where: { name: collectionName },
|
where: {name: collectionName},
|
||||||
include: {
|
include: {
|
||||||
relation: 'sizes',
|
relation: 'sizes',
|
||||||
scope: {
|
scope: {
|
||||||
|
@ -116,16 +116,16 @@ module.exports = Self => {
|
||||||
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();
|
||||||
});
|
});
|
||||||
|
@ -133,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);
|
||||||
|
|
||||||
|
@ -153,7 +153,7 @@ module.exports = Self => {
|
||||||
|
|
||||||
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();
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,105 +0,0 @@
|
||||||
const https = require('https');
|
|
||||||
const fs = require('fs-extra');
|
|
||||||
const path = require('path');
|
|
||||||
const uuid = require('uuid');
|
|
||||||
|
|
||||||
module.exports = Self => {
|
|
||||||
Self.remoteMethod('downloadImages', {
|
|
||||||
description: 'Returns last entries',
|
|
||||||
accessType: 'WRITE',
|
|
||||||
returns: {
|
|
||||||
type: ['Object'],
|
|
||||||
root: true
|
|
||||||
},
|
|
||||||
http: {
|
|
||||||
path: `/downloadImages`,
|
|
||||||
verb: 'POST'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Self.downloadImages = async() => {
|
|
||||||
const models = Self.app.models;
|
|
||||||
const container = await models.TempContainer.container('salix-image');
|
|
||||||
const tempPath = path.join(container.client.root, container.name);
|
|
||||||
const maxAttempts = 3;
|
|
||||||
|
|
||||||
const images = await Self.find({
|
|
||||||
where: {attempts: {eq: maxAttempts}}
|
|
||||||
});
|
|
||||||
|
|
||||||
for (let image of images) {
|
|
||||||
const currentStamp = Date.vnNew().getTime();
|
|
||||||
const updatedStamp = image.updated.getTime();
|
|
||||||
const graceTime = Math.abs(currentStamp - updatedStamp);
|
|
||||||
const maxTTL = 3600 * 48 * 1000; // 48 hours in ms;
|
|
||||||
|
|
||||||
if (graceTime >= maxTTL)
|
|
||||||
await Self.destroyById(image.itemFk);
|
|
||||||
}
|
|
||||||
|
|
||||||
download();
|
|
||||||
|
|
||||||
async function download() {
|
|
||||||
const image = await Self.findOne({
|
|
||||||
where: {url: {neq: null}, attempts: {lt: maxAttempts}},
|
|
||||||
order: 'priority, attempts, updated'
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!image) return;
|
|
||||||
|
|
||||||
const fileName = `${uuid.v4()}.png`;
|
|
||||||
const filePath = path.join(tempPath, fileName);
|
|
||||||
const imageUrl = image.url.replace('http://', 'https://');
|
|
||||||
|
|
||||||
https.get(imageUrl, async response => {
|
|
||||||
if (response.statusCode != 200) {
|
|
||||||
const error = new Error(`Could not download the image. Status code ${response.statusCode}`);
|
|
||||||
|
|
||||||
return await errorHandler(image.itemFk, error, filePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
const writeStream = fs.createWriteStream(filePath);
|
|
||||||
writeStream.on('open', () => response.pipe(writeStream));
|
|
||||||
writeStream.on('error', async error =>
|
|
||||||
await errorHandler(image.itemFk, error, filePath));
|
|
||||||
writeStream.on('finish', () => writeStream.end());
|
|
||||||
|
|
||||||
writeStream.on('close', async function() {
|
|
||||||
try {
|
|
||||||
await models.Image.registerImage('catalog', filePath, fileName, image.itemFk);
|
|
||||||
await image.destroy();
|
|
||||||
|
|
||||||
download();
|
|
||||||
} catch (error) {
|
|
||||||
await errorHandler(image.itemFk, error, filePath);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}).on('error', async error => {
|
|
||||||
await errorHandler(image.itemFk, error, filePath);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function errorHandler(rowId, error, filePath) {
|
|
||||||
try {
|
|
||||||
const row = await Self.findById(rowId);
|
|
||||||
|
|
||||||
if (!row) return;
|
|
||||||
|
|
||||||
if (row.attempts < maxAttempts) {
|
|
||||||
await row.updateAttributes({
|
|
||||||
error: error,
|
|
||||||
attempts: row.attempts + 1,
|
|
||||||
updated: Date.vnNew()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (filePath && fs.existsSync(filePath))
|
|
||||||
await fs.unlink(filePath);
|
|
||||||
|
|
||||||
download();
|
|
||||||
} catch (err) {
|
|
||||||
throw new Error(`Image download failed: ${err}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
Loading…
Reference in New Issue