This commit is contained in:
parent
edb06a894a
commit
c01083d696
|
@ -59,6 +59,9 @@
|
||||||
"ImageCollectionSize": {
|
"ImageCollectionSize": {
|
||||||
"dataSource": "vn"
|
"dataSource": "vn"
|
||||||
},
|
},
|
||||||
|
"ImageConfig": {
|
||||||
|
"dataSource": "vn"
|
||||||
|
},
|
||||||
"ImageContainer": {
|
"ImageContainer": {
|
||||||
"dataSource": "imageStorage"
|
"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 fs = require('fs-extra');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const gm = require('gm');
|
const gm = require('gm');
|
||||||
|
const crypto = require('crypto');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
require('../methods/image/download')(Self);
|
require('../methods/image/download')(Self);
|
||||||
|
@ -31,6 +32,14 @@ module.exports = Self => {
|
||||||
|
|
||||||
// Insert image row
|
// Insert image row
|
||||||
const imageName = path.parse(fileName).name;
|
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(
|
await models.Image.upsertWithWhere(
|
||||||
{
|
{
|
||||||
name: imageName,
|
name: imageName,
|
||||||
|
@ -64,8 +73,10 @@ module.exports = Self => {
|
||||||
|
|
||||||
// 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/${dstDir}`);
|
||||||
|
const fullSizeOriginalPath = path.join(collectionDir, `full`);
|
||||||
const toFullSizePath = `${fullSizePath}/${fileName}`;
|
const toFullSizePath = `${fullSizePath}/${fileName}`;
|
||||||
|
const toFullSizeOriginalPath = `${fullSizeOriginalPath}/${fileName}`;
|
||||||
|
|
||||||
await fs.mkdir(fullSizePath, {recursive: true});
|
await fs.mkdir(fullSizePath, {recursive: true});
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
|
@ -78,13 +89,19 @@ module.exports = Self => {
|
||||||
if (!err) resolve();
|
if (!err) resolve();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
try {
|
||||||
|
await fs.unlink(toFullSizeOriginalPath);
|
||||||
|
} catch (e) {}
|
||||||
|
await fs.symlink(toFullSizeOriginalPath, toFullSizePath, 'file');
|
||||||
|
|
||||||
// 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}/${dstDir}`);
|
||||||
const toSizePath = `${sizePath}/${fileName}`;
|
const toSizePath = `${sizePath}/${fileName}`;
|
||||||
|
const sizeOriginalPath = path.join(collectionDir, `${width}x${height}`);
|
||||||
|
const toSizeOriginalPath = `${sizeOriginalPath}/${fileName}`;
|
||||||
|
|
||||||
await fs.mkdir(sizePath, {recursive: true});
|
await fs.mkdir(sizePath, {recursive: true});
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
|
@ -107,6 +124,10 @@ module.exports = Self => {
|
||||||
if (!err) resolve();
|
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`)
|
INSERT INTO `vn`.`buyConfig` (`id`, `monthsAgo`)
|
||||||
VALUES
|
VALUES
|
||||||
(1, 6);
|
(1, 6);
|
||||||
|
|
||||||
|
INSERT INTO `hedera`.`imageConfig` (`maxSize`, `dirLevels`)
|
||||||
|
VALUES
|
||||||
|
(20, 2);
|
||||||
|
|
Loading…
Reference in New Issue