mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-10 07:55:46 +01:00
refactor: topic thumbs lib to topics.thumbs
This commit is contained in:
@@ -124,7 +124,7 @@ module.exports = function (Posts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (data.thumb) {
|
if (data.thumb) {
|
||||||
await topics.resizeAndUploadThumb(data);
|
await topics.thumbs.resizeAndUpload(data);
|
||||||
newTopicData.thumb = data.thumb;
|
newTopicData.thumb = data.thumb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ module.exports = function (Topics) {
|
|||||||
Topics.create = async function (data) {
|
Topics.create = async function (data) {
|
||||||
// This is an internal method, consider using Topics.post instead
|
// This is an internal method, consider using Topics.post instead
|
||||||
const timestamp = data.timestamp || Date.now();
|
const timestamp = data.timestamp || Date.now();
|
||||||
await Topics.resizeAndUploadThumb(data);
|
await Topics.thumbs.resizeAndUpload(data);
|
||||||
|
|
||||||
const tid = await db.incrObjectField('global', 'nextTid');
|
const tid = await db.incrObjectField('global', 'nextTid');
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ require('./tags')(Topics);
|
|||||||
require('./teaser')(Topics);
|
require('./teaser')(Topics);
|
||||||
require('./suggested')(Topics);
|
require('./suggested')(Topics);
|
||||||
require('./tools')(Topics);
|
require('./tools')(Topics);
|
||||||
require('./thumb')(Topics);
|
Topics.thumbs = require('./thumbs');
|
||||||
require('./bookmarks')(Topics);
|
require('./bookmarks')(Topics);
|
||||||
require('./merge')(Topics);
|
require('./merge')(Topics);
|
||||||
|
|
||||||
|
|||||||
@@ -1,87 +0,0 @@
|
|||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
var nconf = require('nconf');
|
|
||||||
var path = require('path');
|
|
||||||
var fs = require('fs');
|
|
||||||
var request = require('request');
|
|
||||||
var mime = require('mime');
|
|
||||||
var validator = require('validator');
|
|
||||||
var util = require('util');
|
|
||||||
|
|
||||||
var meta = require('../meta');
|
|
||||||
var image = require('../image');
|
|
||||||
var file = require('../file');
|
|
||||||
var plugins = require('../plugins');
|
|
||||||
|
|
||||||
module.exports = function (Topics) {
|
|
||||||
const getHead = util.promisify(request.head);
|
|
||||||
|
|
||||||
function pipeToFile(source, destination, callback) {
|
|
||||||
request(source).pipe(fs.createWriteStream(destination)).on('close', callback);
|
|
||||||
}
|
|
||||||
const pipeToFileAsync = util.promisify(pipeToFile);
|
|
||||||
|
|
||||||
Topics.resizeAndUploadThumb = async function (data) {
|
|
||||||
const allowedExtensions = file.allowedExtensions();
|
|
||||||
|
|
||||||
// Handle protocol-relative URLs
|
|
||||||
if (data.thumb && data.thumb.startsWith('//')) {
|
|
||||||
data.thumb = `${nconf.get('secure') ? 'https' : 'http'}:${data.thumb}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only continue if passed in thumbnail exists and is a URL. A system path means an upload is not necessary.
|
|
||||||
if (!data.thumb || !validator.isURL(data.thumb)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var pathToUpload;
|
|
||||||
const res = await getHead(data.thumb);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const type = res.headers['content-type'];
|
|
||||||
if (!type.match(/image./)) {
|
|
||||||
throw new Error('[[error:invalid-file]]');
|
|
||||||
}
|
|
||||||
|
|
||||||
var extension = path.extname(data.thumb);
|
|
||||||
if (!extension) {
|
|
||||||
extension = '.' + mime.getExtension(type);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!allowedExtensions.includes(extension)) {
|
|
||||||
throw new Error('[[error:invalid-file]]');
|
|
||||||
}
|
|
||||||
|
|
||||||
const filename = Date.now() + '-topic-thumb' + extension;
|
|
||||||
const folder = 'files';
|
|
||||||
pathToUpload = path.join(nconf.get('upload_path'), folder, filename);
|
|
||||||
|
|
||||||
await pipeToFileAsync(data.thumb, pathToUpload);
|
|
||||||
|
|
||||||
await image.isFileTypeAllowed(pathToUpload);
|
|
||||||
|
|
||||||
await image.checkDimensions(pathToUpload);
|
|
||||||
await image.resizeImage({
|
|
||||||
path: pathToUpload,
|
|
||||||
width: meta.config.topicThumbSize,
|
|
||||||
height: meta.config.topicThumbSize,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!plugins.hooks.hasListeners('filter:uploadImage')) {
|
|
||||||
data.thumb = '/assets/uploads/' + folder + '/' + filename;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const uploadedFile = await plugins.hooks.fire('filter:uploadImage', {
|
|
||||||
image: { path: pathToUpload, name: '' },
|
|
||||||
uid: data.uid,
|
|
||||||
folder: folder,
|
|
||||||
});
|
|
||||||
file.delete(pathToUpload);
|
|
||||||
data.thumb = uploadedFile.url;
|
|
||||||
} catch (err) {
|
|
||||||
file.delete(pathToUpload);
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
88
src/topics/thumbs.js
Normal file
88
src/topics/thumbs.js
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var nconf = require('nconf');
|
||||||
|
var path = require('path');
|
||||||
|
var fs = require('fs');
|
||||||
|
var request = require('request');
|
||||||
|
var mime = require('mime');
|
||||||
|
var validator = require('validator');
|
||||||
|
var util = require('util');
|
||||||
|
|
||||||
|
var meta = require('../meta');
|
||||||
|
var image = require('../image');
|
||||||
|
var file = require('../file');
|
||||||
|
var plugins = require('../plugins');
|
||||||
|
|
||||||
|
const Thumbs = {};
|
||||||
|
module.exports = Thumbs;
|
||||||
|
|
||||||
|
const getHead = util.promisify(request.head);
|
||||||
|
|
||||||
|
function pipeToFile(source, destination, callback) {
|
||||||
|
request(source).pipe(fs.createWriteStream(destination)).on('close', callback);
|
||||||
|
}
|
||||||
|
const pipeToFileAsync = util.promisify(pipeToFile);
|
||||||
|
|
||||||
|
Thumbs.resizeAndUpload = async function (data) {
|
||||||
|
const allowedExtensions = file.allowedExtensions();
|
||||||
|
|
||||||
|
// Handle protocol-relative URLs
|
||||||
|
if (data.thumb && data.thumb.startsWith('//')) {
|
||||||
|
data.thumb = `${nconf.get('secure') ? 'https' : 'http'}:${data.thumb}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only continue if passed in thumbnail exists and is a URL. A system path means an upload is not necessary.
|
||||||
|
if (!data.thumb || !validator.isURL(data.thumb)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var pathToUpload;
|
||||||
|
const res = await getHead(data.thumb);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const type = res.headers['content-type'];
|
||||||
|
if (!type.match(/image./)) {
|
||||||
|
throw new Error('[[error:invalid-file]]');
|
||||||
|
}
|
||||||
|
|
||||||
|
var extension = path.extname(data.thumb);
|
||||||
|
if (!extension) {
|
||||||
|
extension = '.' + mime.getExtension(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!allowedExtensions.includes(extension)) {
|
||||||
|
throw new Error('[[error:invalid-file]]');
|
||||||
|
}
|
||||||
|
|
||||||
|
const filename = Date.now() + '-topic-thumb' + extension;
|
||||||
|
const folder = 'files';
|
||||||
|
pathToUpload = path.join(nconf.get('upload_path'), folder, filename);
|
||||||
|
|
||||||
|
await pipeToFileAsync(data.thumb, pathToUpload);
|
||||||
|
|
||||||
|
await image.isFileTypeAllowed(pathToUpload);
|
||||||
|
|
||||||
|
await image.checkDimensions(pathToUpload);
|
||||||
|
await image.resizeImage({
|
||||||
|
path: pathToUpload,
|
||||||
|
width: meta.config.topicThumbSize,
|
||||||
|
height: meta.config.topicThumbSize,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!plugins.hooks.hasListeners('filter:uploadImage')) {
|
||||||
|
data.thumb = '/assets/uploads/' + folder + '/' + filename;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uploadedFile = await plugins.hooks.fire('filter:uploadImage', {
|
||||||
|
image: { path: pathToUpload, name: '' },
|
||||||
|
uid: data.uid,
|
||||||
|
folder: folder,
|
||||||
|
});
|
||||||
|
file.delete(pathToUpload);
|
||||||
|
data.thumb = uploadedFile.url;
|
||||||
|
} catch (err) {
|
||||||
|
file.delete(pathToUpload);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user