mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-28 17:46:16 +01:00
more cleanup
This commit is contained in:
46
src/posts.js
46
src/posts.js
@@ -421,52 +421,6 @@ var db = require('./database'),
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Posts.uploadPostImage = function(image, callback) {
|
|
||||||
|
|
||||||
if(plugins.hasListeners('filter:uploadImage')) {
|
|
||||||
plugins.fireHook('filter:uploadImage', image, callback);
|
|
||||||
} else {
|
|
||||||
|
|
||||||
if (meta.config.allowFileUploads) {
|
|
||||||
Posts.uploadPostFile(image, callback);
|
|
||||||
} else {
|
|
||||||
callback(new Error('Uploads are disabled!'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Posts.uploadPostFile = function(file, callback) {
|
|
||||||
|
|
||||||
if(plugins.hasListeners('filter:uploadFile')) {
|
|
||||||
plugins.fireHook('filter:uploadFile', file, callback);
|
|
||||||
} else {
|
|
||||||
|
|
||||||
if(!meta.config.allowFileUploads) {
|
|
||||||
return callback(new Error('File uploads are not allowed'));
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!file) {
|
|
||||||
return callback(new Error('invalid file'));
|
|
||||||
}
|
|
||||||
|
|
||||||
if(file.size > parseInt(meta.config.maximumFileSize, 10) * 1024) {
|
|
||||||
return callback(new Error('File too big'));
|
|
||||||
}
|
|
||||||
|
|
||||||
var filename = 'upload-' + utils.generateUUID() + path.extname(file.name);
|
|
||||||
require('./file').saveFileToLocal(filename, file.path, function(err, upload) {
|
|
||||||
if(err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
callback(null, {
|
|
||||||
url: upload.url,
|
|
||||||
name: file.name
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Posts.getFavourites = function(uid, start, end, callback) {
|
Posts.getFavourites = function(uid, start, end, callback) {
|
||||||
db.getSortedSetRevRange('uid:' + uid + ':favourites', start, end, function(err, pids) {
|
db.getSortedSetRevRange('uid:' + uid + ':favourites', start, end, function(err, pids) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|||||||
@@ -15,12 +15,19 @@ var path = require('path'),
|
|||||||
pkg = require('./../../package.json');
|
pkg = require('./../../package.json');
|
||||||
|
|
||||||
|
|
||||||
|
function deleteTempFiles(files) {
|
||||||
|
for(var i=0; i<files.length; ++i) {
|
||||||
|
fs.unlink(files[i].path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function upload(req, res, filesIterator, next) {
|
function upload(req, res, filesIterator, next) {
|
||||||
|
var files = req.files.files;
|
||||||
|
|
||||||
if(!req.user) {
|
if(!req.user) {
|
||||||
|
deleteTempFiles(files);
|
||||||
return res.json(403, {message:'not allowed'});
|
return res.json(403, {message:'not allowed'});
|
||||||
}
|
}
|
||||||
var files = req.files.files;
|
|
||||||
|
|
||||||
if(!Array.isArray(files)) {
|
if(!Array.isArray(files)) {
|
||||||
return res.json(500, {message: 'invalid files'});
|
return res.json(500, {message: 'invalid files'});
|
||||||
@@ -30,14 +37,8 @@ function upload(req, res, filesIterator, next) {
|
|||||||
files = files[0];
|
files = files[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteTempFiles() {
|
|
||||||
for(var i=0; i<files.length; ++i) {
|
|
||||||
fs.unlink(files[i].path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async.map(files, filesIterator, function(err, images) {
|
async.map(files, filesIterator, function(err, images) {
|
||||||
deleteTempFiles();
|
deleteTempFiles(files);
|
||||||
|
|
||||||
if(err) {
|
if(err) {
|
||||||
return res.send(500, err.message);
|
return res.send(500, err.message);
|
||||||
@@ -52,23 +53,76 @@ function upload(req, res, filesIterator, next) {
|
|||||||
function uploadPost(req, res, next) {
|
function uploadPost(req, res, next) {
|
||||||
upload(req, res, function(file, next) {
|
upload(req, res, function(file, next) {
|
||||||
if(file.type.match(/image./)) {
|
if(file.type.match(/image./)) {
|
||||||
posts.uploadPostImage(file, next);
|
uploadImage(file, next);
|
||||||
} else {
|
} else {
|
||||||
posts.uploadPostFile(file, next);
|
uploadFile(file, next);
|
||||||
}
|
}
|
||||||
}, next);
|
}, next);
|
||||||
}
|
}
|
||||||
|
|
||||||
function uploadThumb(req, res, next) {
|
function uploadThumb(req, res, next) {
|
||||||
|
if (!meta.config.allowTopicsThumbnail) {
|
||||||
|
deleteTempFiles(req.files.files);
|
||||||
|
return callback(new Error('Topic Thumbnails are disabled!'));
|
||||||
|
}
|
||||||
|
|
||||||
upload(req, res, function(file, next) {
|
upload(req, res, function(file, next) {
|
||||||
if(file.type.match(/image./)) {
|
if(file.type.match(/image./)) {
|
||||||
topics.uploadTopicThumb(file, next);
|
uploadImage(file, next);
|
||||||
} else {
|
} else {
|
||||||
res.json(500, {message: 'Invalid File'});
|
next(new Error('Invalid File'));
|
||||||
}
|
}
|
||||||
}, next);
|
}, next);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function uploadImage(image, callback) {
|
||||||
|
|
||||||
|
if(plugins.hasListeners('filter:uploadImage')) {
|
||||||
|
plugins.fireHook('filter:uploadImage', image, callback);
|
||||||
|
} else {
|
||||||
|
|
||||||
|
if (meta.config.allowFileUploads) {
|
||||||
|
Posts.uploadPostFile(image, callback);
|
||||||
|
} else {
|
||||||
|
callback(new Error('Uploads are disabled!'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function uploadFile(file, callback) {
|
||||||
|
|
||||||
|
if(plugins.hasListeners('filter:uploadFile')) {
|
||||||
|
plugins.fireHook('filter:uploadFile', file, callback);
|
||||||
|
} else {
|
||||||
|
|
||||||
|
if(!meta.config.allowFileUploads) {
|
||||||
|
return callback(new Error('File uploads are not allowed'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!file) {
|
||||||
|
return callback(new Error('invalid file'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(file.size > parseInt(meta.config.maximumFileSize, 10) * 1024) {
|
||||||
|
return callback(new Error('File too big'));
|
||||||
|
}
|
||||||
|
|
||||||
|
var filename = 'upload-' + utils.generateUUID() + path.extname(file.name);
|
||||||
|
require('./file').saveFileToLocal(filename, file.path, function(err, upload) {
|
||||||
|
if(err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(null, {
|
||||||
|
url: upload.url,
|
||||||
|
name: file.name
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function getModerators(req, res, next) {
|
function getModerators(req, res, next) {
|
||||||
categories.getModerators(req.params.cid, function(err, moderators) {
|
categories.getModerators(req.params.cid, function(err, moderators) {
|
||||||
res.json({moderators: moderators});
|
res.json({moderators: moderators});
|
||||||
|
|||||||
@@ -104,18 +104,6 @@ function favouriteCommand(command, socket, data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SocketPosts.uploadImage = function(socket, data, callback) {
|
|
||||||
if(data) {
|
|
||||||
posts.uploadPostImage(data, callback);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
SocketPosts.uploadFile = function(socket, data, callback) {
|
|
||||||
if(data) {
|
|
||||||
posts.uploadPostFile(data, callback);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
SocketPosts.getRawPost = function(socket, pid, callback) {
|
SocketPosts.getRawPost = function(socket, pid, callback) {
|
||||||
posts.getPostFields(pid, ['content', 'deleted'], function(err, data) {
|
posts.getPostFields(pid, ['content', 'deleted'], function(err, data) {
|
||||||
if(err) {
|
if(err) {
|
||||||
|
|||||||
@@ -371,9 +371,9 @@ var async = require('async'),
|
|||||||
}
|
}
|
||||||
|
|
||||||
topic.user = {
|
topic.user = {
|
||||||
username: userData.username || 'Anonymous',
|
username: userData.username || 'Anonymous',
|
||||||
userslug: userData.userslug || '',
|
userslug: userData.userslug || '',
|
||||||
picture: userData.picture || gravatar.url('', {}, true)
|
picture: userData.picture || gravatar.url('', {}, true)
|
||||||
};
|
};
|
||||||
|
|
||||||
callback(null, topic);
|
callback(null, topic);
|
||||||
@@ -846,28 +846,6 @@ var async = require('async'),
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Topics.uploadTopicThumb = function(image, callback) {
|
|
||||||
|
|
||||||
if(plugins.hasListeners('filter:uploadImage')) {
|
|
||||||
plugins.fireHook('filter:uploadImage', image, callback);
|
|
||||||
} else {
|
|
||||||
if (meta.config.allowTopicsThumbnail) {
|
|
||||||
var filename = 'upload-' + utils.generateUUID() + path.extname(image.name);
|
|
||||||
require('./file').saveFileToLocal(filename, image.path, function(err, upload) {
|
|
||||||
if(err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
callback(null, {
|
|
||||||
url: upload.url,
|
|
||||||
name: image.name
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
callback(new Error('Topic Thumbnails are disabled!'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Topics.markAsUnreadForAll = function(tid, callback) {
|
Topics.markAsUnreadForAll = function(tid, callback) {
|
||||||
db.delete('tid:' + tid + ':read_by_uid', function(err) {
|
db.delete('tid:' + tid + ':read_by_uid', function(err) {
|
||||||
if(err) {
|
if(err) {
|
||||||
@@ -996,31 +974,25 @@ var async = require('async'),
|
|||||||
};
|
};
|
||||||
|
|
||||||
Topics.increasePostCount = function(tid, callback) {
|
Topics.increasePostCount = function(tid, callback) {
|
||||||
db.incrObjectField('topic:' + tid, 'postcount', function(err, value) {
|
incrementFieldAndUpdateSortedSet(tid, 'postcount', 1, 'topics:posts', callback);
|
||||||
if(err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
db.sortedSetAdd('topics:posts', value, tid, callback);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Topics.decreasePostCount = function(tid, callback) {
|
Topics.decreasePostCount = function(tid, callback) {
|
||||||
db.decrObjectField('topic:' + tid, 'postcount', function(err, value) {
|
incrementFieldAndUpdateSortedSet(tid, 'postcount', -1, 'topics:posts', callback);
|
||||||
if(err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
db.sortedSetAdd('topics:posts', value, tid, callback);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Topics.increaseViewCount = function(tid, callback) {
|
Topics.increaseViewCount = function(tid, callback) {
|
||||||
db.incrObjectField('topic:' + tid, 'viewcount', function(err, value) {
|
incrementFieldAndUpdateSortedSet(tid, 'viewcount', 1, 'topics:views', callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
function incrementFieldAndUpdateSortedSet(tid, field, by, set, callback) {
|
||||||
|
db.incrObjectFieldBy('topic:' + tid, field, by, function(err, value) {
|
||||||
if(err) {
|
if(err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
db.sortedSetAdd('topics:views', value, tid, callback);
|
db.sortedSetAdd(set, value, tid, callback);
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
Topics.isLocked = function(tid, callback) {
|
Topics.isLocked = function(tid, callback) {
|
||||||
Topics.getTopicField(tid, 'locked', function(err, locked) {
|
Topics.getTopicField(tid, 'locked', function(err, locked) {
|
||||||
|
|||||||
Reference in New Issue
Block a user