more cleanup

v1.18.x
barisusakli 11 years ago
parent 2befa06a33
commit 6e6c5688d9

@ -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) {
db.getSortedSetRevRange('uid:' + uid + ':favourites', start, end, function(err, pids) {
if (err) {

@ -15,12 +15,19 @@ var path = require('path'),
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) {
var files = req.files.files;
if(!req.user) {
deleteTempFiles(files);
return res.json(403, {message:'not allowed'});
}
var files = req.files.files;
if(!Array.isArray(files)) {
return res.json(500, {message: 'invalid files'});
@ -30,14 +37,8 @@ function upload(req, res, filesIterator, next) {
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) {
deleteTempFiles();
deleteTempFiles(files);
if(err) {
return res.send(500, err.message);
@ -52,23 +53,76 @@ function upload(req, res, filesIterator, next) {
function uploadPost(req, res, next) {
upload(req, res, function(file, next) {
if(file.type.match(/image./)) {
posts.uploadPostImage(file, next);
uploadImage(file, next);
} else {
posts.uploadPostFile(file, next);
uploadFile(file, next);
}
}, 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) {
if(file.type.match(/image./)) {
topics.uploadTopicThumb(file, next);
uploadImage(file, next);
} else {
res.json(500, {message: 'Invalid File'});
next(new Error('Invalid File'));
}
}, 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) {
categories.getModerators(req.params.cid, function(err, 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) {
posts.getPostFields(pid, ['content', 'deleted'], function(err, data) {
if(err) {

@ -371,9 +371,9 @@ var async = require('async'),
}
topic.user = {
username: userData.username || 'Anonymous',
userslug: userData.userslug || '',
picture: userData.picture || gravatar.url('', {}, true)
username: userData.username || 'Anonymous',
userslug: userData.userslug || '',
picture: userData.picture || gravatar.url('', {}, true)
};
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) {
db.delete('tid:' + tid + ':read_by_uid', function(err) {
if(err) {
@ -996,31 +974,25 @@ var async = require('async'),
};
Topics.increasePostCount = function(tid, callback) {
db.incrObjectField('topic:' + tid, 'postcount', function(err, value) {
if(err) {
return callback(err);
}
db.sortedSetAdd('topics:posts', value, tid, callback);
});
incrementFieldAndUpdateSortedSet(tid, 'postcount', 1, 'topics:posts', callback);
};
Topics.decreasePostCount = function(tid, callback) {
db.decrObjectField('topic:' + tid, 'postcount', function(err, value) {
if(err) {
return callback(err);
}
db.sortedSetAdd('topics:posts', value, tid, callback);
});
incrementFieldAndUpdateSortedSet(tid, 'postcount', -1, 'topics:posts', 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) {
return callback(err);
}
db.sortedSetAdd('topics:views', value, tid, callback);
db.sortedSetAdd(set, value, tid, callback);
});
};
}
Topics.isLocked = function(tid, callback) {
Topics.getTopicField(tid, 'locked', function(err, locked) {

Loading…
Cancel
Save