From 90e1136ee2410bf3399618d2130e955d15b9d7d2 Mon Sep 17 00:00:00 2001 From: joan Date: Mon, 20 Feb 2023 12:40:13 +0100 Subject: [PATCH 1/2] fix(image): store images with a name generated through uuid instead of url path Refs: #5266 --- back/methods/image/download.js | 7 +++++-- back/methods/image/upload.js | 11 ++++------- back/models/image.js | 6 +++--- front/salix/components/upload-photo/index.html | 8 -------- front/salix/components/upload-photo/index.js | 3 +-- .../back/methods/item-image-queue/downloadImages.js | 7 +++---- 6 files changed, 16 insertions(+), 26 deletions(-) diff --git a/back/methods/image/download.js b/back/methods/image/download.js index bbfe8e41a..3a8bcf30e 100644 --- a/back/methods/image/download.js +++ b/back/methods/image/download.js @@ -74,10 +74,13 @@ module.exports = Self => { const container = await models.ImageContainer.getContainer(collection); const rootPath = container.client.root; const fileSrc = path.join(rootPath, collection, size); + + const ext = image.name.substring((image.name.length - 4)); + const fileName = ext !== '.png' ? `${image.name}.png` : image.name; const file = { - path: `${fileSrc}/${image.name}.png`, + path: `${fileSrc}/${fileName}`, contentType: 'image/png', - name: `${image.name}.png` + name: image.name }; if (!fs.existsSync(file.path)) return []; diff --git a/back/methods/image/upload.js b/back/methods/image/upload.js index 676a4b5fb..143da275e 100644 --- a/back/methods/image/upload.js +++ b/back/methods/image/upload.js @@ -1,6 +1,7 @@ const UserError = require('vn-loopback/util/user-error'); const fs = require('fs-extra'); const path = require('path'); +const uuid = require('uuid'); module.exports = Self => { Self.remoteMethodCtx('upload', { @@ -18,12 +19,6 @@ module.exports = Self => { type: 'string', description: 'The collection name', required: true - }, - { - arg: 'fileName', - type: 'string', - description: 'The file name', - required: true }], returns: { type: 'Object', @@ -56,10 +51,12 @@ module.exports = Self => { const [uploadedFile] = Object.values(uploaded.files).map(file => { return file[0]; }); + const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name); srcFile = path.join(file.client.root, file.container, file.name); - await models.Image.registerImage(args.collection, srcFile, args.fileName, args.id); + const fileName = `${uuid.v4()}.png`; + await models.Image.registerImage(args.collection, srcFile, fileName, args.id); } catch (e) { if (fs.existsSync(srcFile)) await fs.unlink(srcFile); diff --git a/back/models/image.js b/back/models/image.js index 37f4ec20d..bdc555dbe 100644 --- a/back/models/image.js +++ b/back/models/image.js @@ -84,9 +84,9 @@ module.exports = Self => { const container = await models.ImageContainer.container(collectionName); const rootPath = container.client.root; const collectionDir = path.join(rootPath, collectionName); - const file = `${fileName}.png`; + // const file = `${fileName}.png`; const dstDir = path.join(collectionDir, 'full'); - const dstFile = path.join(dstDir, file); + const dstFile = path.join(dstDir, fileName); const buffer = readChunk.sync(srcFilePath, 0, 12); const type = imageType(buffer); @@ -120,7 +120,7 @@ module.exports = Self => { const sizes = collection.sizes(); for (let size of sizes) { const dstDir = path.join(collectionDir, `${size.width}x${size.height}`); - const dstFile = path.join(dstDir, file); + const dstFile = path.join(dstDir, fileName); const resizeOpts = { withoutEnlargement: true, fit: size.crop ? 'cover' : 'inside' diff --git a/front/salix/components/upload-photo/index.html b/front/salix/components/upload-photo/index.html index 9b64a380f..4422d8855 100644 --- a/front/salix/components/upload-photo/index.html +++ b/front/salix/components/upload-photo/index.html @@ -69,14 +69,6 @@ value-field="code"> - - - - diff --git a/front/salix/components/upload-photo/index.js b/front/salix/components/upload-photo/index.js index fb115992f..da1fda923 100644 --- a/front/salix/components/upload-photo/index.js +++ b/front/salix/components/upload-photo/index.js @@ -64,8 +64,7 @@ export default class UploadPhoto extends Component { this.editor = null; this.newPhoto = { id: id, - collection: collection, - fileName: id + collection: collection }; this.$.dialog.show(); } diff --git a/modules/item/back/methods/item-image-queue/downloadImages.js b/modules/item/back/methods/item-image-queue/downloadImages.js index 0f57856c7..08e5df050 100644 --- a/modules/item/back/methods/item-image-queue/downloadImages.js +++ b/modules/item/back/methods/item-image-queue/downloadImages.js @@ -1,6 +1,7 @@ const https = require('https'); const fs = require('fs-extra'); const path = require('path'); +const uuid = require('uuid'); module.exports = Self => { Self.remoteMethod('downloadImages', { @@ -46,10 +47,8 @@ module.exports = Self => { if (!image) return; - const srcFile = image.url; - const fileName = srcFile.replace(/\.|\/|:|\?|\\|=|%/g, ''); - const file = `${fileName}.png`; - const filePath = path.join(tempPath, file); + const fileName = `${uuid.v4()}.png`; + const filePath = path.join(tempPath, fileName); const imageUrl = image.url.replace('http://', 'https://'); https.get(imageUrl, async response => { From 0393d9cb6a07a41d698171b2435a474be9e63ec1 Mon Sep 17 00:00:00 2001 From: joan Date: Mon, 20 Feb 2023 14:17:18 +0100 Subject: [PATCH 2/2] Removed commented block code --- back/models/image.js | 1 - 1 file changed, 1 deletion(-) diff --git a/back/models/image.js b/back/models/image.js index bdc555dbe..666989a85 100644 --- a/back/models/image.js +++ b/back/models/image.js @@ -84,7 +84,6 @@ module.exports = Self => { const container = await models.ImageContainer.container(collectionName); const rootPath = container.client.root; const collectionDir = path.join(rootPath, collectionName); - // const file = `${fileName}.png`; const dstDir = path.join(collectionDir, 'full'); const dstFile = path.join(dstDir, fileName);