feat: add folder to filter:uploadImage and filter:uploadFile

v1.18.x
Barış Soner Uşaklı 5 years ago
parent 478c8dd72b
commit 7f24200c73

@ -243,7 +243,7 @@ async function uploadImage(filename, folder, uploadedFile, req, res, next) {
let imageData; let imageData;
try { try {
if (plugins.hasListeners('filter:uploadImage')) { if (plugins.hasListeners('filter:uploadImage')) {
imageData = await plugins.fireHook('filter:uploadImage', { image: uploadedFile, uid: req.uid }); imageData = await plugins.fireHook('filter:uploadImage', { image: uploadedFile, uid: req.uid, folder: folder });
} else { } else {
imageData = await file.saveFileToLocal(filename, folder, uploadedFile.path); imageData = await file.saveFileToLocal(filename, folder, uploadedFile.path);
} }

@ -64,6 +64,7 @@ async function uploadAsImage(req, uploadedFile) {
return await plugins.fireHook('filter:uploadImage', { return await plugins.fireHook('filter:uploadImage', {
image: uploadedFile, image: uploadedFile,
uid: req.uid, uid: req.uid,
folder: 'files',
}); });
} }
await image.isFileTypeAllowed(uploadedFile.path); await image.isFileTypeAllowed(uploadedFile.path);
@ -129,6 +130,7 @@ uploadsController.uploadThumb = async function (req, res, next) {
return await plugins.fireHook('filter:uploadImage', { return await plugins.fireHook('filter:uploadImage', {
image: uploadedFile, image: uploadedFile,
uid: req.uid, uid: req.uid,
folder: 'files',
}); });
} }
@ -141,6 +143,7 @@ uploadsController.uploadFile = async function (uid, uploadedFile) {
return await plugins.fireHook('filter:uploadFile', { return await plugins.fireHook('filter:uploadFile', {
file: uploadedFile, file: uploadedFile,
uid: uid, uid: uid,
folder: 'files',
}); });
} }
@ -159,16 +162,16 @@ uploadsController.uploadFile = async function (uid, uploadedFile) {
throw new Error('[[error:invalid-file-type, ' + allowed.join(', ') + ']]'); throw new Error('[[error:invalid-file-type, ' + allowed.join(', ') + ']]');
} }
return await saveFileToLocal(uid, uploadedFile); return await saveFileToLocal(uid, 'files', uploadedFile);
}; };
async function saveFileToLocal(uid, uploadedFile) { async function saveFileToLocal(uid, folder, uploadedFile) {
const name = uploadedFile.name || 'upload'; const name = uploadedFile.name || 'upload';
const extension = path.extname(name) || ''; const extension = path.extname(name) || '';
const filename = Date.now() + '-' + validator.escape(name.substr(0, name.length - extension.length)).substr(0, 255) + extension; const filename = Date.now() + '-' + validator.escape(name.substr(0, name.length - extension.length)).substr(0, 255) + extension;
const upload = await file.saveFileToLocal(filename, 'files', uploadedFile.path); const upload = await file.saveFileToLocal(filename, folder, uploadedFile.path);
const storedFile = { const storedFile = {
url: nconf.get('relative_path') + upload.url, url: nconf.get('relative_path') + upload.url,
path: upload.path, path: upload.path,

@ -149,6 +149,7 @@ image.uploadImage = async function (filename, folder, imageData) {
return await plugins.fireHook('filter:uploadImage', { return await plugins.fireHook('filter:uploadImage', {
image: imageData, image: imageData,
uid: imageData.uid, uid: imageData.uid,
folder: folder,
}); });
} }
await image.isFileTypeAllowed(imageData.path); await image.isFileTypeAllowed(imageData.path);

@ -40,7 +40,8 @@ module.exports = function (Topics) {
extension = '.' + mime.getExtension(type); extension = '.' + mime.getExtension(type);
} }
const filename = Date.now() + '-topic-thumb' + extension; const filename = Date.now() + '-topic-thumb' + extension;
pathToUpload = path.join(nconf.get('upload_path'), 'files', filename); const folder = 'files';
pathToUpload = path.join(nconf.get('upload_path'), folder, filename);
await pipeToFileAsync(data.thumb, pathToUpload); await pipeToFileAsync(data.thumb, pathToUpload);
@ -53,11 +54,15 @@ module.exports = function (Topics) {
}); });
if (!plugins.hasListeners('filter:uploadImage')) { if (!plugins.hasListeners('filter:uploadImage')) {
data.thumb = '/assets/uploads/files/' + filename; data.thumb = '/assets/uploads/' + folder + '/' + filename;
return; return;
} }
const uploadedFile = await plugins.fireHook('filter:uploadImage', { image: { path: pathToUpload, name: '' }, uid: data.uid }); const uploadedFile = await plugins.fireHook('filter:uploadImage', {
image: { path: pathToUpload, name: '' },
uid: data.uid,
folder: folder,
});
file.delete(pathToUpload); file.delete(pathToUpload);
data.thumb = uploadedFile.url; data.thumb = uploadedFile.url;
} catch (err) { } catch (err) {

Loading…
Cancel
Save