mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
refactor: async/await image.js
This commit is contained in:
118
src/image.js
118
src/image.js
@@ -1,22 +1,22 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var os = require('os');
|
const os = require('os');
|
||||||
var fs = require('fs');
|
const fs = require('fs');
|
||||||
var path = require('path');
|
const path = require('path');
|
||||||
var crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
var async = require('async');
|
const winston = require('winston');
|
||||||
var winston = require('winston');
|
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
const readFileAsync = util.promisify(fs.readFile);
|
const readFileAsync = util.promisify(fs.readFile);
|
||||||
|
const writeFileAsync = util.promisify(fs.writeFile);
|
||||||
|
|
||||||
var file = require('./file');
|
const file = require('./file');
|
||||||
var plugins = require('./plugins');
|
const plugins = require('./plugins');
|
||||||
var meta = require('./meta');
|
const meta = require('./meta');
|
||||||
|
|
||||||
var image = module.exports;
|
const image = module.exports;
|
||||||
|
|
||||||
function requireSharp() {
|
function requireSharp() {
|
||||||
var sharp = require('sharp');
|
const sharp = require('sharp');
|
||||||
if (os.platform() === 'win32') {
|
if (os.platform() === 'win32') {
|
||||||
// https://github.com/lovell/sharp/issues/1259
|
// https://github.com/lovell/sharp/issues/1259
|
||||||
sharp.cache(false);
|
sharp.cache(false);
|
||||||
@@ -67,78 +67,55 @@ image.resizeImage = async function (data) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
image.normalise = function (path, extension, callback) {
|
image.normalise = async function (path) {
|
||||||
if (plugins.hasListeners('filter:image.normalise')) {
|
if (plugins.hasListeners('filter:image.normalise')) {
|
||||||
plugins.fireHook('filter:image.normalise', {
|
await plugins.fireHook('filter:image.normalise', {
|
||||||
path: path,
|
path: path,
|
||||||
}, function (err) {
|
|
||||||
callback(err, path + '.png');
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
var sharp = requireSharp();
|
const sharp = requireSharp();
|
||||||
sharp(path, { failOnError: true }).png().toFile(path + '.png', function (err) {
|
await sharp(path, { failOnError: true }).png().toFile(path + '.png');
|
||||||
callback(err, path + '.png');
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
return path + '.png';
|
||||||
};
|
};
|
||||||
|
|
||||||
image.size = function (path, callback) {
|
image.size = async function (path) {
|
||||||
|
let imageData;
|
||||||
if (plugins.hasListeners('filter:image.size')) {
|
if (plugins.hasListeners('filter:image.size')) {
|
||||||
plugins.fireHook('filter:image.size', {
|
imageData = await plugins.fireHook('filter:image.size', {
|
||||||
path: path,
|
path: path,
|
||||||
}, function (err, image) {
|
|
||||||
callback(err, image ? { width: image.width, height: image.height } : undefined);
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
var sharp = requireSharp();
|
const sharp = requireSharp();
|
||||||
sharp(path, { failOnError: true }).metadata(function (err, metadata) {
|
imageData = await sharp(path, { failOnError: true }).metadata();
|
||||||
callback(err, metadata ? { width: metadata.width, height: metadata.height } : undefined);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
return imageData ? { width: imageData.width, height: imageData.height } : undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
image.stripEXIF = function (path, callback) {
|
image.stripEXIF = async function (path) {
|
||||||
if (!meta.config.stripEXIFData || path.endsWith('.gif')) {
|
if (!meta.config.stripEXIFData || path.endsWith('.gif')) {
|
||||||
return setImmediate(callback);
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const buffer = await readFileAsync(path);
|
||||||
|
const sharp = requireSharp();
|
||||||
|
await sharp(buffer, { failOnError: true }).rotate().toFile(path);
|
||||||
|
} catch (err) {
|
||||||
|
winston.error(err);
|
||||||
}
|
}
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
fs.readFile(path, next);
|
|
||||||
},
|
|
||||||
function (buffer, next) {
|
|
||||||
const sharp = requireSharp();
|
|
||||||
const sharpImage = sharp(buffer, {
|
|
||||||
failOnError: true,
|
|
||||||
});
|
|
||||||
sharpImage.rotate().toFile(path, next);
|
|
||||||
},
|
|
||||||
], function (err) {
|
|
||||||
if (err) {
|
|
||||||
winston.error(err);
|
|
||||||
}
|
|
||||||
callback();
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
image.checkDimensions = function (path, callback) {
|
image.checkDimensions = async function (path) {
|
||||||
const meta = require('./meta');
|
const meta = require('./meta');
|
||||||
image.size(path, function (err, result) {
|
const result = await image.size(path);
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
const maxWidth = meta.config.rejectImageWidth;
|
if (result.width > meta.config.rejectImageWidth || result.height > meta.config.rejectImageHeight) {
|
||||||
const maxHeight = meta.config.rejectImageHeight;
|
throw new Error('[[error:invalid-image-dimensions]]');
|
||||||
if (result.width > maxWidth || result.height > maxHeight) {
|
}
|
||||||
return callback(new Error('[[error:invalid-image-dimensions]]'));
|
|
||||||
}
|
|
||||||
|
|
||||||
callback();
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
image.convertImageToBase64 = function (path, callback) {
|
image.convertImageToBase64 = async function (path) {
|
||||||
fs.readFile(path, 'base64', callback);
|
return await readFileAsync(path, 'base64');
|
||||||
};
|
};
|
||||||
|
|
||||||
image.mimeFromBase64 = function (imageData) {
|
image.mimeFromBase64 = function (imageData) {
|
||||||
@@ -149,21 +126,18 @@ image.extensionFromBase64 = function (imageData) {
|
|||||||
return file.typeToExtension(image.mimeFromBase64(imageData));
|
return file.typeToExtension(image.mimeFromBase64(imageData));
|
||||||
};
|
};
|
||||||
|
|
||||||
image.writeImageDataToTempFile = function (imageData, callback) {
|
image.writeImageDataToTempFile = async function (imageData) {
|
||||||
var filename = crypto.createHash('md5').update(imageData).digest('hex');
|
const filename = crypto.createHash('md5').update(imageData).digest('hex');
|
||||||
|
|
||||||
var type = image.mimeFromBase64(imageData);
|
const type = image.mimeFromBase64(imageData);
|
||||||
var extension = file.typeToExtension(type);
|
const extension = file.typeToExtension(type);
|
||||||
|
|
||||||
var filepath = path.join(os.tmpdir(), filename + extension);
|
const filepath = path.join(os.tmpdir(), filename + extension);
|
||||||
|
|
||||||
var buffer = Buffer.from(imageData.slice(imageData.indexOf('base64') + 7), 'base64');
|
const buffer = Buffer.from(imageData.slice(imageData.indexOf('base64') + 7), 'base64');
|
||||||
|
|
||||||
fs.writeFile(filepath, buffer, {
|
await writeFileAsync(filepath, buffer, { encoding: 'base64' });
|
||||||
encoding: 'base64',
|
return filepath;
|
||||||
}, function (err) {
|
|
||||||
callback(err, filepath);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
image.sizeFromBase64 = function (imageData) {
|
image.sizeFromBase64 = function (imageData) {
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ module.exports = function (User) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
picture.path = await getTempPath(data);
|
picture.path = await getTempPath(data);
|
||||||
picture.path = await convertToPNG(picture.path, extension);
|
picture.path = await convertToPNG(picture.path);
|
||||||
|
|
||||||
await image.resizeImage({
|
await image.resizeImage({
|
||||||
path: picture.path,
|
path: picture.path,
|
||||||
@@ -131,12 +131,12 @@ module.exports = function (User) {
|
|||||||
return await image.writeImageDataToTempFile(data.imageData);
|
return await image.writeImageDataToTempFile(data.imageData);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function convertToPNG(path, extension) {
|
async function convertToPNG(path) {
|
||||||
const convertToPNG = meta.config['profile:convertProfileImageToPNG'] === 1;
|
const convertToPNG = meta.config['profile:convertProfileImageToPNG'] === 1;
|
||||||
if (!convertToPNG) {
|
if (!convertToPNG) {
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
const newPath = await image.normalise(path, extension);
|
const newPath = await image.normalise(path);
|
||||||
file.delete(path);
|
file.delete(path);
|
||||||
return newPath;
|
return newPath;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user