mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-10 16:47:36 +01:00
* feat: allow both callback&and await
* feat: ignore async key
* feat: callbackify and promisify in same file
* Revert "feat: callbackify and promisify in same file"
This reverts commit cea206a9b8.
* feat: no need to store .callbackify
* feat: change getTopics to async
* feat: remove .async
* fix: byScore
* feat: rewrite topics/index and social with async/await
* fix: rewrite topics/data.js
fix issue with async.waterfall, only pass result if its not undefined
* feat: add callbackify to redis/psql
* feat: psql use await
* fix: redis 🌋
* feat: less returns
* feat: more await rewrite
* fix: redis tests
* feat: convert sortedSetAdd
rewrite psql transaction to async/await
* feat: 🐶
* feat: test
* feat: log client and query
* feat: log bind
* feat: more logs
* feat: more logs
* feat: check perform
* feat: dont callbackify transaction
* feat: remove logs
* fix: main functions
* feat: more logs
* fix: increment
* fix: rename
* feat: remove cls
* fix: remove console.log
* feat: add deprecation message to .async usage
* feat: update more dbal methods
* fix: redis :voodoo:
* feat: fix redis zrem, convert setObject
* feat: upgrade getObject methods
* fix: psql getObjectField
* fix: redis tests
* feat: getObjectKeys
* feat: getObjectValues
* feat: isObjectField
* fix: add missing return
* feat: delObjectField
* feat: incrObjectField
* fix: add missing await
* feat: remove exposed helpers
* feat: list methods
* feat: flush/empty
* feat: delete
* fix: redis delete all
* feat: get/set
* feat: incr/rename
* feat: type
* feat: expire
* feat: setAdd
* feat: setRemove
* feat: isSetMember
* feat: getSetMembers
* feat: setCount, setRemoveRandom
* feat: zcard,zcount
* feat: sortedSetRank
* feat: isSortedSetMember
* feat: zincrby
* feat: sortedSetLex
* feat: processSortedSet
* fix: add mising await
* feat: debug psql
* fix: psql test
* fix: test
* fix: another test
* fix: test fix
* fix: psql tests
* feat: remove logs
* feat: user arrow func
use builtin async promises
* feat: topic bookmarks
* feat: topic.delete
* feat: topic.restore
* feat: topics.purge
* feat: merge
* feat: suggested
* feat: topics/user.js
* feat: topics modules
* feat: topics/follow
* fix: deprecation msg
* feat: fork
* feat: topics/posts
* feat: sorted/recent
* feat: topic/teaser
* feat: topics/tools
* feat: topics/unread
* feat: add back node versions
disable deprecation notice
wrap async controllers in try/catch
* feat: use db directly
* feat: promisify in place
* fix: redis/psql
* feat: deprecation message
logs for psql
* feat: more logs
* feat: more logs
* feat: logs again
* feat: more logs
* fix: call release
* feat: restore travis, remove logs
* fix: loops
* feat: remove .async. usage
184 lines
4.6 KiB
JavaScript
184 lines
4.6 KiB
JavaScript
'use strict';
|
|
|
|
var os = require('os');
|
|
var fs = require('fs');
|
|
var path = require('path');
|
|
var crypto = require('crypto');
|
|
var async = require('async');
|
|
var winston = require('winston');
|
|
const util = require('util');
|
|
const readFileAsync = util.promisify(fs.readFile);
|
|
|
|
var file = require('./file');
|
|
var plugins = require('./plugins');
|
|
var meta = require('./meta');
|
|
|
|
var image = module.exports;
|
|
|
|
function requireSharp() {
|
|
var sharp = require('sharp');
|
|
if (os.platform() === 'win32') {
|
|
// https://github.com/lovell/sharp/issues/1259
|
|
sharp.cache(false);
|
|
}
|
|
return sharp;
|
|
}
|
|
|
|
image.resizeImage = async function (data) {
|
|
if (plugins.hasListeners('filter:image.resize')) {
|
|
await plugins.fireHook('filter:image.resize', {
|
|
path: data.path,
|
|
target: data.target,
|
|
width: data.width,
|
|
height: data.height,
|
|
quality: data.quality,
|
|
});
|
|
} else {
|
|
const sharp = requireSharp();
|
|
const buffer = await readFileAsync(data.path);
|
|
const sharpImage = sharp(buffer, {
|
|
failOnError: true,
|
|
});
|
|
const metadata = await sharpImage.metadata();
|
|
|
|
sharpImage.rotate(); // auto-orients based on exif data
|
|
sharpImage.resize(data.hasOwnProperty('width') ? data.width : null, data.hasOwnProperty('height') ? data.height : null);
|
|
|
|
if (data.quality && metadata.format === 'jpeg') {
|
|
sharpImage.jpeg({ quality: data.quality });
|
|
}
|
|
|
|
await sharpImage.toFile(data.target || data.path);
|
|
}
|
|
};
|
|
|
|
image.normalise = function (path, extension, callback) {
|
|
if (plugins.hasListeners('filter:image.normalise')) {
|
|
plugins.fireHook('filter:image.normalise', {
|
|
path: path,
|
|
}, function (err) {
|
|
callback(err, path + '.png');
|
|
});
|
|
} else {
|
|
var sharp = requireSharp();
|
|
sharp(path, { failOnError: true }).png().toFile(path + '.png', function (err) {
|
|
callback(err, path + '.png');
|
|
});
|
|
}
|
|
};
|
|
|
|
image.size = function (path, callback) {
|
|
if (plugins.hasListeners('filter:image.size')) {
|
|
plugins.fireHook('filter:image.size', {
|
|
path: path,
|
|
}, function (err, image) {
|
|
callback(err, image ? { width: image.width, height: image.height } : undefined);
|
|
});
|
|
} else {
|
|
var sharp = requireSharp();
|
|
sharp(path, { failOnError: true }).metadata(function (err, metadata) {
|
|
callback(err, metadata ? { width: metadata.width, height: metadata.height } : undefined);
|
|
});
|
|
}
|
|
};
|
|
|
|
image.stripEXIF = function (path, callback) {
|
|
if (!meta.config.stripEXIFData || path.endsWith('.gif')) {
|
|
return setImmediate(callback);
|
|
}
|
|
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) {
|
|
const meta = require('./meta');
|
|
image.size(path, function (err, result) {
|
|
if (err) {
|
|
return callback(err);
|
|
}
|
|
|
|
const maxWidth = meta.config.rejectImageWidth;
|
|
const maxHeight = meta.config.rejectImageHeight;
|
|
if (result.width > maxWidth || result.height > maxHeight) {
|
|
return callback(new Error('[[error:invalid-image-dimensions]]'));
|
|
}
|
|
|
|
callback();
|
|
});
|
|
};
|
|
|
|
image.convertImageToBase64 = function (path, callback) {
|
|
fs.readFile(path, 'base64', callback);
|
|
};
|
|
|
|
image.mimeFromBase64 = function (imageData) {
|
|
return imageData.slice(5, imageData.indexOf('base64') - 1);
|
|
};
|
|
|
|
image.extensionFromBase64 = function (imageData) {
|
|
return file.typeToExtension(image.mimeFromBase64(imageData));
|
|
};
|
|
|
|
image.writeImageDataToTempFile = function (imageData, callback) {
|
|
var filename = crypto.createHash('md5').update(imageData).digest('hex');
|
|
|
|
var type = image.mimeFromBase64(imageData);
|
|
var extension = file.typeToExtension(type);
|
|
|
|
var filepath = path.join(os.tmpdir(), filename + extension);
|
|
|
|
var buffer = Buffer.from(imageData.slice(imageData.indexOf('base64') + 7), 'base64');
|
|
|
|
fs.writeFile(filepath, buffer, {
|
|
encoding: 'base64',
|
|
}, function (err) {
|
|
callback(err, filepath);
|
|
});
|
|
};
|
|
|
|
image.sizeFromBase64 = function (imageData) {
|
|
return Buffer.from(imageData.slice(imageData.indexOf('base64') + 7), 'base64').length;
|
|
};
|
|
|
|
image.uploadImage = function (filename, folder, image, callback) {
|
|
if (plugins.hasListeners('filter:uploadImage')) {
|
|
return plugins.fireHook('filter:uploadImage', {
|
|
image: image,
|
|
uid: image.uid,
|
|
}, callback);
|
|
}
|
|
|
|
async.waterfall([
|
|
function (next) {
|
|
file.isFileTypeAllowed(image.path, next);
|
|
},
|
|
function (next) {
|
|
file.saveFileToLocal(filename, folder, image.path, next);
|
|
},
|
|
function (upload, next) {
|
|
next(null, {
|
|
url: upload.url,
|
|
path: upload.path,
|
|
name: image.name,
|
|
});
|
|
},
|
|
], callback);
|
|
};
|
|
|
|
require('./promisify')(image);
|