This commit is contained in:
parent
edb06a894a
commit
c01083d696
|
@ -59,6 +59,9 @@
|
|||
"ImageCollectionSize": {
|
||||
"dataSource": "vn"
|
||||
},
|
||||
"ImageConfig": {
|
||||
"dataSource": "vn"
|
||||
},
|
||||
"ImageContainer": {
|
||||
"dataSource": "imageStorage"
|
||||
},
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
@ -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');
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue