save reverse association of md5 of upload to pid, #6455

This commit is contained in:
Julian Lam
2018-04-16 16:22:20 -04:00
parent d25ab31427
commit 2e125293e6
2 changed files with 35 additions and 4 deletions

View File

@@ -1,12 +1,15 @@
'use strict';
var async = require('async');
var crypto = require('crypto');
var db = require('../database');
module.exports = function (Posts) {
Posts.uploads = {};
const md5 = filename => crypto.createHash('md5').update(filename).digest('hex');
Posts.uploads.sync = function (pid, callback) {
// Scans a post and updates sorted set of uploads
const searchRegex = /\/assets\/uploads\/files\/([^\s")]+\.?[\w]*)/g;
@@ -51,14 +54,24 @@ module.exports = function (Posts) {
const now = Date.now();
filePaths = !Array.isArray(filePaths) ? [filePaths] : filePaths;
const scores = filePaths.map(() => now);
let methods = [async.apply(db.sortedSetAdd.bind(db), 'post:' + pid + ':uploads', scores, filePaths)];
methods = methods.concat(filePaths.map(path => async.apply(db.sortedSetAdd.bind(db), 'upload:' + md5(path) + ':pids', now, pid)));
db.sortedSetAdd('post:' + pid + ':uploads', scores, filePaths, callback);
async.parallel(methods, function (err) {
// Strictly return only err
callback(err);
});
};
Posts.uploads.dissociate = function (pid, filePaths, callback) {
// Removes an upload from a post's sorted set of uploads
filePaths = !Array.isArray(filePaths) ? [filePaths] : filePaths;
let methods = [async.apply(db.sortedSetRemove.bind(db), 'post:' + pid + ':uploads', filePaths)];
methods = methods.concat(filePaths.map(path => async.apply(db.sortedSetRemove.bind(db), 'upload:' + md5(path) + ':pids', pid)));
db.sortedSetRemove('post:' + pid + ':uploads', filePaths, callback);
async.parallel(methods, function (err) {
// Strictly return only err
callback(err);
});
};
};