mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
refactor: abstract some common code out to local utility methods
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const async = require('async');
|
|
||||||
const nconf = require('nconf');
|
const nconf = require('nconf');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
@@ -20,6 +19,12 @@ module.exports = function (Posts) {
|
|||||||
const pathPrefix = path.join(nconf.get('upload_path'), 'files');
|
const pathPrefix = path.join(nconf.get('upload_path'), 'files');
|
||||||
const searchRegex = /\/assets\/uploads\/files\/([^\s")]+\.?[\w]*)/g;
|
const searchRegex = /\/assets\/uploads\/files\/([^\s")]+\.?[\w]*)/g;
|
||||||
|
|
||||||
|
const _getFullPath = relativePath => path.resolve(pathPrefix, relativePath);
|
||||||
|
const _filterValidPaths = async filePaths => (await Promise.all(filePaths.map(async (filePath) => {
|
||||||
|
const fullPath = _getFullPath(filePath);
|
||||||
|
return fullPath.startsWith(pathPrefix) && await file.exists(fullPath) ? filePath : false;
|
||||||
|
}))).filter(Boolean);
|
||||||
|
|
||||||
Posts.uploads.sync = async function (pid) {
|
Posts.uploads.sync = async function (pid) {
|
||||||
// Scans a post's content and updates sorted set of uploads
|
// Scans a post's content and updates sorted set of uploads
|
||||||
|
|
||||||
@@ -92,8 +97,7 @@ module.exports = function (Posts) {
|
|||||||
if (!filePaths.length) {
|
if (!filePaths.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Only process files that exist
|
filePaths = await _filterValidPaths(filePaths); // Only process files that exist and are within uploads directory
|
||||||
filePaths = await async.filter(filePaths, async filePath => await file.exists(path.join(pathPrefix, filePath)));
|
|
||||||
|
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const scores = filePaths.map(() => now);
|
const scores = filePaths.map(() => now);
|
||||||
@@ -131,7 +135,7 @@ module.exports = function (Posts) {
|
|||||||
});
|
});
|
||||||
await Promise.all(filePaths.map(async (fileName) => {
|
await Promise.all(filePaths.map(async (fileName) => {
|
||||||
try {
|
try {
|
||||||
const size = await image.size(path.join(pathPrefix, fileName));
|
const size = await image.size(_getFullPath(fileName));
|
||||||
winston.verbose(`[posts/uploads/${fileName}] Saving size`);
|
winston.verbose(`[posts/uploads/${fileName}] Saving size`);
|
||||||
await db.setObject(`upload:${md5(fileName)}`, {
|
await db.setObject(`upload:${md5(fileName)}`, {
|
||||||
width: size.width,
|
width: size.width,
|
||||||
|
|||||||
Reference in New Issue
Block a user