diff --git a/back/model-config.json b/back/model-config.json index ff2bf5850..a39cdb014 100644 --- a/back/model-config.json +++ b/back/model-config.json @@ -59,6 +59,9 @@ "ImageCollectionSize": { "dataSource": "vn" }, + "ImageConfig": { + "dataSource": "vn" + }, "ImageContainer": { "dataSource": "imageStorage" }, diff --git a/back/models/image-config.json b/back/models/image-config.json new file mode 100644 index 000000000..325859d91 --- /dev/null +++ b/back/models/image-config.json @@ -0,0 +1,27 @@ +{ + "name": "ImageConfig", + "base": "VnModel", + "options": { + "mysql": { + "table": "hedera.imageConfig" + } + }, + "properties": { + "id": { + "type": "number", + "id": true + }, + "maxSize": { + "type": "number" + }, + "useXsendfile": { + "type": "number" + }, + "url": { + "type": "string" + }, + "dirLevels": { + "type": "number" + } + } +} diff --git a/back/models/image.js b/back/models/image.js index e13f9e100..58e26a734 100644 --- a/back/models/image.js +++ b/back/models/image.js @@ -1,6 +1,7 @@ const fs = require('fs-extra'); const path = require('path'); const gm = require('gm'); +const crypto = require('crypto'); module.exports = Self => { require('../methods/image/download')(Self); @@ -31,6 +32,14 @@ module.exports = Self => { // Insert image row const imageName = path.parse(fileName).name; + const shasum = crypto.createHash('sha1'); + shasum.update(imageName); + const hash = shasum.digest('hex'); + const pairs = hash.match(/(..?)/g); + const imageConfig = await models.ImageConfig.findOne({fields: ['dirLevels']}); + const firstPairs = pairs.slice(0, imageConfig.dirLevels).reverse(); + const dstDir = firstPairs.join('/'); + await models.Image.upsertWithWhere( { name: imageName, @@ -64,8 +73,10 @@ module.exports = Self => { // To max size const {maxWidth, maxHeight} = collection; - const fullSizePath = path.join(collectionDir, 'full'); + const fullSizePath = path.join(collectionDir, `full/${dstDir}`); + const fullSizeOriginalPath = path.join(collectionDir, `full`); const toFullSizePath = `${fullSizePath}/${fileName}`; + const toFullSizeOriginalPath = `${fullSizeOriginalPath}/${fileName}`; await fs.mkdir(fullSizePath, {recursive: true}); await new Promise((resolve, reject) => { @@ -78,13 +89,19 @@ module.exports = Self => { if (!err) resolve(); }); }); + try { + await fs.unlink(toFullSizeOriginalPath); + } catch (e) {} + await fs.symlink(toFullSizeOriginalPath, toFullSizePath, 'file'); // To collection sizes for (const size of collection.sizes()) { const {width, height} = size; - const sizePath = path.join(collectionDir, `${width}x${height}`); + const sizePath = path.join(collectionDir, `${width}x${height}/${dstDir}`); const toSizePath = `${sizePath}/${fileName}`; + const sizeOriginalPath = path.join(collectionDir, `${width}x${height}`); + const toSizeOriginalPath = `${sizeOriginalPath}/${fileName}`; await fs.mkdir(sizePath, {recursive: true}); await new Promise((resolve, reject) => { @@ -107,6 +124,10 @@ module.exports = Self => { if (!err) resolve(); }); }); + try { + await fs.unlink(toSizeOriginalPath); + } catch (e) {} + await fs.symlink(toSizePath, toSizeOriginalPath); } }; }; diff --git a/db/changes/232601/00-hederaImageConfig.sql b/db/changes/232601/00-hederaImageConfig.sql new file mode 100644 index 000000000..f1cdd44ea --- /dev/null +++ b/db/changes/232601/00-hederaImageConfig.sql @@ -0,0 +1,4 @@ +ALTER TABLE `hedera`.`imageConfig` ADD dirLevels INT UNSIGNED NOT NULL DEFAULT 2; +INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) + VALUES + ('ImageConfig', '*', 'READ', 'ALLOW', 'ROLE', 'employee'); diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 4441ec19c..a9bd05092 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2897,3 +2897,7 @@ INSERT INTO `vn`.`travelConfig` (`id`, `warehouseInFk`, `warehouseOutFk`, `agenc INSERT INTO `vn`.`buyConfig` (`id`, `monthsAgo`) VALUES (1, 6); + +INSERT INTO `hedera`.`imageConfig` (`maxSize`, `dirLevels`) + VALUES + (20, 2);