2020-07-13 09:58:31 +00:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const sharp = require('sharp');
|
|
|
|
const path = require('path');
|
2021-02-24 13:37:49 +00:00
|
|
|
const readChunk = require('read-chunk');
|
|
|
|
const imageType = require('image-type');
|
|
|
|
const bmp = require('bmp-js');
|
2020-07-13 09:58:31 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
2020-11-10 07:39:34 +00:00
|
|
|
require('../methods/image/download')(Self);
|
2020-11-27 12:10:39 +00:00
|
|
|
require('../methods/image/upload')(Self);
|
2020-11-10 07:39:34 +00:00
|
|
|
|
2021-02-24 13:37:49 +00:00
|
|
|
// Function extracted from jimp package (utils)
|
|
|
|
function scan(image, x, y, w, h, f) {
|
|
|
|
// round input
|
|
|
|
x = Math.round(x);
|
|
|
|
y = Math.round(y);
|
|
|
|
w = Math.round(w);
|
|
|
|
h = Math.round(h);
|
|
|
|
|
|
|
|
for (let _y = y; _y < y + h; _y++) {
|
|
|
|
for (let _x = x; _x < x + w; _x++) {
|
|
|
|
const idx = (image.bitmap.width * _y + _x) << 2;
|
|
|
|
f.call(image, _x, _y, idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function extracted from jimp package (type-bmp)
|
|
|
|
function fromAGBR(bitmap) {
|
|
|
|
return scan({bitmap}, 0, 0, bitmap.width, bitmap.height, function(
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
index
|
|
|
|
) {
|
|
|
|
const alpha = this.bitmap.data[index + 0];
|
|
|
|
const blue = this.bitmap.data[index + 1];
|
|
|
|
const green = this.bitmap.data[index + 2];
|
|
|
|
const red = this.bitmap.data[index + 3];
|
|
|
|
|
|
|
|
this.bitmap.data[index + 0] = red;
|
|
|
|
this.bitmap.data[index + 1] = green;
|
|
|
|
this.bitmap.data[index + 2] = blue;
|
|
|
|
this.bitmap.data[index + 3] = bitmap.is_with_alpha ? alpha : 0xff;
|
|
|
|
}).bitmap;
|
|
|
|
}
|
|
|
|
|
2020-12-18 16:23:04 +00:00
|
|
|
Self.registerImage = async(collectionName, srcFilePath, fileName, entityId) => {
|
2020-07-13 09:58:31 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
const tx = await Self.beginTransaction({});
|
|
|
|
const myOptions = {transaction: tx};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const collection = await models.ImageCollection.findOne({
|
|
|
|
fields: [
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'maxWidth',
|
|
|
|
'maxHeight',
|
|
|
|
'model',
|
|
|
|
'property'
|
|
|
|
],
|
|
|
|
where: {name: collectionName},
|
|
|
|
include: {
|
|
|
|
relation: 'sizes',
|
|
|
|
scope: {
|
|
|
|
fields: ['width', 'height', 'crop']
|
|
|
|
}
|
|
|
|
}
|
2020-07-14 07:32:50 +00:00
|
|
|
}, myOptions);
|
2020-07-13 09:58:31 +00:00
|
|
|
|
|
|
|
const data = {
|
|
|
|
name: fileName,
|
|
|
|
collectionFk: collectionName
|
|
|
|
};
|
|
|
|
const newImage = await Self.upsertWithWhere(data, {
|
|
|
|
name: fileName,
|
|
|
|
collectionFk: collectionName,
|
2023-01-16 14:18:24 +00:00
|
|
|
updated: Date.vnNow()
|
2020-07-13 09:58:31 +00:00
|
|
|
}, myOptions);
|
|
|
|
|
|
|
|
// Resizes and saves the image
|
2020-12-18 16:23:04 +00:00
|
|
|
const container = await models.ImageContainer.container(collectionName);
|
2020-11-27 12:10:39 +00:00
|
|
|
const rootPath = container.client.root;
|
2020-07-13 09:58:31 +00:00
|
|
|
const collectionDir = path.join(rootPath, collectionName);
|
2020-12-18 16:23:04 +00:00
|
|
|
const file = `${fileName}.png`;
|
2020-07-13 09:58:31 +00:00
|
|
|
const dstDir = path.join(collectionDir, 'full');
|
|
|
|
const dstFile = path.join(dstDir, file);
|
|
|
|
|
2021-02-24 13:37:49 +00:00
|
|
|
const buffer = readChunk.sync(srcFilePath, 0, 12);
|
|
|
|
const type = imageType(buffer);
|
|
|
|
|
|
|
|
let sharpOptions;
|
|
|
|
let imgSrc = srcFilePath;
|
|
|
|
if (type.mime == 'image/bmp') {
|
|
|
|
const bmpBuffer = fs.readFileSync(srcFilePath);
|
|
|
|
const bmpData = fromAGBR(bmp.decode(bmpBuffer));
|
|
|
|
imgSrc = bmpData.data;
|
|
|
|
sharpOptions = {
|
|
|
|
raw: {
|
|
|
|
width: bmpData.width,
|
|
|
|
height: bmpData.height,
|
|
|
|
channels: 4
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-13 09:58:31 +00:00
|
|
|
const resizeOpts = {
|
|
|
|
withoutEnlargement: true,
|
|
|
|
fit: 'inside'
|
|
|
|
};
|
|
|
|
|
|
|
|
await fs.mkdir(dstDir, {recursive: true});
|
2021-02-24 13:37:49 +00:00
|
|
|
await sharp(imgSrc, sharpOptions)
|
2020-07-13 09:58:31 +00:00
|
|
|
.resize(collection.maxWidth, collection.maxHeight, resizeOpts)
|
2020-08-18 07:45:26 +00:00
|
|
|
.png()
|
2020-07-13 09:58:31 +00:00
|
|
|
.toFile(dstFile);
|
|
|
|
|
|
|
|
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 resizeOpts = {
|
|
|
|
withoutEnlargement: true,
|
|
|
|
fit: size.crop ? 'cover' : 'inside'
|
|
|
|
};
|
|
|
|
|
|
|
|
await fs.mkdir(dstDir, {recursive: true});
|
2021-02-24 13:37:49 +00:00
|
|
|
await sharp(imgSrc, sharpOptions)
|
2020-07-13 09:58:31 +00:00
|
|
|
.resize(size.width, size.height, resizeOpts)
|
2020-08-18 07:45:26 +00:00
|
|
|
.png()
|
2020-07-13 09:58:31 +00:00
|
|
|
.toFile(dstFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
const model = models[collection.model];
|
|
|
|
|
|
|
|
if (!model)
|
|
|
|
throw new Error('Matching model not found');
|
|
|
|
|
2020-11-27 12:10:39 +00:00
|
|
|
const item = await model.findById(entityId, null, myOptions);
|
2020-07-13 09:58:31 +00:00
|
|
|
if (item) {
|
|
|
|
await item.updateAttribute(
|
|
|
|
collection.property,
|
|
|
|
fileName,
|
|
|
|
myOptions
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-18 16:23:04 +00:00
|
|
|
if (fs.existsSync(srcFilePath))
|
|
|
|
await fs.unlink(srcFilePath);
|
2020-08-27 11:53:11 +00:00
|
|
|
|
2020-07-13 09:58:31 +00:00
|
|
|
await tx.commit();
|
2021-10-15 14:00:50 +00:00
|
|
|
|
2020-07-13 09:58:31 +00:00
|
|
|
return newImage;
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|