diff --git a/public/src/client/account/uploads.js b/public/src/client/account/uploads.js index 96754db74c..222e30e9a0 100644 --- a/public/src/client/account/uploads.js +++ b/public/src/client/account/uploads.js @@ -7,10 +7,10 @@ define('forum/account/uploads', ['forum/account/header'], function (header) { header.init(); $('[data-action="delete"]').on('click', function () { - var el = $(this).parents('[data-url]'); - var url = el.attr('data-url'); + var el = $(this).parents('[data-name]'); + var name = el.attr('data-name'); - socket.emit('user.deleteUpload', { url: url, uid: ajaxify.data.uid }, function (err) { + socket.emit('user.deleteUpload', { name: name, uid: ajaxify.data.uid }, function (err) { if (err) { return app.alertError(err.message); } diff --git a/src/controllers/accounts/uploads.js b/src/controllers/accounts/uploads.js index 6f0ff29fff..21a5287550 100644 --- a/src/controllers/accounts/uploads.js +++ b/src/controllers/accounts/uploads.js @@ -2,6 +2,7 @@ var async = require('async'); +var nconf = require('nconf'); var db = require('../../database'); var helpers = require('../helpers'); @@ -33,15 +34,16 @@ uploadsController.get = function (req, res, callback) { itemCount: function (next) { db.sortedSetCard('uid:' + userData.uid + ':uploads', next); }, - uploadUrls: function (next) { + uploadNames: function (next) { db.getSortedSetRevRange('uid:' + userData.uid + ':uploads', start, stop, next); }, }, next); }, function (results) { - userData.uploads = results.uploadUrls.map(function (url) { + userData.uploads = results.uploadNames.map(function (uploadName) { return { - url: url, + name: uploadName, + url: nconf.get('upload_url') + uploadName, }; }); var pageCount = Math.ceil(results.itemCount / itemsPerPage); diff --git a/src/controllers/uploads.js b/src/controllers/uploads.js index 60366be026..487332542e 100644 --- a/src/controllers/uploads.js +++ b/src/controllers/uploads.js @@ -241,7 +241,8 @@ function saveFileToLocal(uid, uploadedFile, callback) { name: uploadedFile.name, }; - db.sortedSetAdd('uid:' + uid + ':uploads', Date.now(), upload.url, next); + var fileKey = upload.url.replace(nconf.get('upload_url'), ''); + db.sortedSetAdd('uid:' + uid + ':uploads', Date.now(), fileKey, next); }, function (next) { plugins.fireHook('filter:uploadStored', { uid: uid, uploadedFile: uploadedFile, storedFile: storedFile }, next); diff --git a/src/file.js b/src/file.js index 2c3f8d71ab..2fbd15dbdf 100644 --- a/src/file.js +++ b/src/file.js @@ -88,13 +88,6 @@ file.saveFileToLocal = function (filename, folder, tempPath, callback) { }); }; -file.uploadUrlToPath = function (url) { - if (typeof url !== 'string') { - return ''; - } - return path.join(nconf.get('upload_path'), url.replace(nconf.get('upload_url'), '')); -}; - file.base64ToLocal = function (imageData, uploadPath, callback) { var buffer = Buffer.from(imageData.slice(imageData.indexOf('base64') + 7), 'base64'); uploadPath = path.join(nconf.get('upload_path'), uploadPath); diff --git a/src/socket.io/user.js b/src/socket.io/user.js index 25df8f7931..4f16e82eda 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -342,10 +342,10 @@ SocketUser.setModerationNote = function (socket, data, callback) { }; SocketUser.deleteUpload = function (socket, data, callback) { - if (!data || !data.url || !data.uid) { + if (!data || !data.name || !data.uid) { return callback(new Error('[[error:invalid-data]]')); } - user.deleteUpload(socket.uid, data.uid, data.url, callback); + user.deleteUpload(socket.uid, data.uid, data.name, callback); }; SocketUser.gdpr = {}; diff --git a/src/user/delete.js b/src/user/delete.js index d56c9749ac..6712569634 100644 --- a/src/user/delete.js +++ b/src/user/delete.js @@ -2,7 +2,8 @@ var async = require('async'); var _ = require('lodash'); - +var path = require('path'); +var nconf = require('nconf'); var db = require('../database'); var posts = require('../posts'); @@ -52,15 +53,15 @@ module.exports = function (User) { } function deleteUploads(uid, callback) { - batch.processSortedSet('uid:' + uid + ':uploads', function (urls, next) { + batch.processSortedSet('uid:' + uid + ':uploads', function (uploadNames, next) { async.waterfall([ function (next) { - async.each(urls, function (url, next) { - file.delete(file.uploadUrlToPath(url), next); + async.each(uploadNames, function (uploadName, next) { + file.delete(path.join(nconf.get('upload_path'), uploadName), next); }, next); }, function (next) { - db.sortedSetRemove('uid:' + uid + ':uploads', urls, next); + db.sortedSetRemove('uid:' + uid + ':uploads', uploadNames, next); }, ], next); }, { alwaysStartAt: 0 }, callback); diff --git a/src/user/uploads.js b/src/user/uploads.js index f5e840a9f2..faec37fce8 100644 --- a/src/user/uploads.js +++ b/src/user/uploads.js @@ -1,17 +1,19 @@ 'use strict'; var async = require('async'); +var path = require('path'); +var nconf = require('nconf'); var db = require('../database'); var file = require('../file'); module.exports = function (User) { - User.deleteUpload = function (callerUid, uid, url, callback) { + User.deleteUpload = function (callerUid, uid, uploadName, callback) { async.waterfall([ function (next) { async.parallel({ isUsersUpload: function (next) { - db.isSortedSetMember('uid:' + callerUid + ':uploads', url, next); + db.isSortedSetMember('uid:' + callerUid + ':uploads', uploadName, next); }, isAdminOrGlobalMod: function (next) { User.isAdminOrGlobalMod(callerUid, next); @@ -23,10 +25,10 @@ module.exports = function (User) { return next(new Error('[[error:no-privileges]]')); } - file.delete(file.uploadUrlToPath(url), next); + file.delete(path.join(nconf.get('upload_path'), uploadName), next); }, function (next) { - db.sortedSetRemove('uid:' + uid + ':uploads', url, next); + db.sortedSetRemove('uid:' + uid + ':uploads', uploadName, next); }, ], callback); };