Merge remote-tracking branch 'refs/remotes/origin/master' into develop

This commit is contained in:
Barış Soner Uşaklı
2018-01-26 18:58:01 -05:00
35 changed files with 359 additions and 102 deletions

View File

@@ -106,7 +106,6 @@ helpers.getUserDataByUserSlug = function (userslug, callerUID, callback) {
userData.moderationNote = undefined;
}
userData.uid = userData.uid;
userData.yourid = callerUID;
userData.theirid = userData.uid;
userData.isTargetAdmin = results.isTargetAdmin;

View File

@@ -4,16 +4,109 @@ var path = require('path');
var async = require('async');
var nconf = require('nconf');
var mime = require('mime');
var fs = require('fs');
var meta = require('../../meta');
var file = require('../../file');
var image = require('../../image');
var plugins = require('../../plugins');
var pagination = require('../../pagination');
var allowedImageTypes = ['image/png', 'image/jpeg', 'image/pjpeg', 'image/jpg', 'image/gif', 'image/svg+xml'];
var uploadsController = module.exports;
uploadsController.get = function (req, res, next) {
var currentFolder = path.join(nconf.get('upload_path'), req.query.dir || '');
if (!currentFolder.startsWith(nconf.get('upload_path'))) {
return next(new Error('[[error:invalid-path]]'));
}
var itemsPerPage = 20;
var itemCount = 0;
var page = parseInt(req.query.page, 10) || 1;
async.waterfall([
function (next) {
fs.readdir(currentFolder, next);
},
function (files, next) {
files = files.filter(function (filename) {
return filename !== '.gitignore';
});
itemCount = files.length;
var start = Math.max(0, (page - 1) * itemsPerPage);
var stop = start + itemsPerPage;
files = files.slice(start, stop);
filesToData(currentFolder, files, next);
},
function (files) {
files.sort(function (a, b) {
if (a.isDirectory && !b.isDirectory) {
return -1;
} else if (!a.isDirectory && b.isDirectory) {
return 1;
}
return 0;
});
res.render('admin/manage/uploads', {
currentFolder: currentFolder.replace(nconf.get('upload_path'), ''),
files: files,
breadcrumbs: buildBreadcrumbs(currentFolder),
pagination: pagination.create(page, Math.ceil(itemCount / itemsPerPage), req.query),
});
},
], next);
};
function buildBreadcrumbs(currentFolder) {
var crumbs = [];
var parts = currentFolder.replace(nconf.get('upload_path'), '').split(path.sep);
var currentPath = '';
parts.forEach(function (part) {
var dir = path.join(currentPath, part);
crumbs.push({
text: part || 'Uploads',
url: part ? '/admin/manage/uploads?dir=' + dir : '/admin/manage/uploads',
});
currentPath = dir;
});
return crumbs;
}
function filesToData(currentDir, files, callback) {
async.map(files, function (file, next) {
var stat;
async.waterfall([
function (next) {
fs.stat(path.join(currentDir, file), next);
},
function (_stat, next) {
stat = _stat;
if (stat.isDirectory()) {
fs.readdir(path.join(currentDir, file), next);
} else {
next(null, []);
}
},
function (filesInDir, next) {
var url = nconf.get('upload_url') + currentDir.replace(nconf.get('upload_path'), '') + '/' + file;
next(null, {
name: file,
path: path.join(currentDir, file).replace(nconf.get('upload_path'), ''),
url: url,
fileCount: filesInDir.length - 1, // ignore .gitignore
size: stat.size,
sizeHumanReadable: (stat.size / 1024).toFixed(1) + 'KiB',
isDirectory: stat.isDirectory(),
isFile: stat.isFile(),
});
},
], next);
}, callback);
}
uploadsController.uploadCategoryPicture = function (req, res, next) {
var uploadedFile = req.files.files[0];
var params = null;
@@ -110,6 +203,25 @@ uploadsController.uploadSound = function (req, res, next) {
});
};
uploadsController.uploadFile = function (req, res, next) {
var uploadedFile = req.files.files[0];
var params;
try {
params = JSON.parse(req.body.params);
} catch (e) {
file.delete(uploadedFile.path);
return next(new Error('[[error:invalid-json]]'));
}
file.saveFileToLocal(uploadedFile.name, params.folder, uploadedFile.path, function (err, data) {
file.delete(uploadedFile.path);
if (err) {
return next(err);
}
res.json([{ url: data.url }]);
});
};
uploadsController.uploadDefaultAvatar = function (req, res, next) {
upload('avatar-default', req, res, next);
};
@@ -173,3 +285,4 @@ function uploadImage(filename, folder, uploadedFile, req, res, next) {
res.json([{ name: uploadedFile.name, url: image.url.startsWith('http') ? image.url : nconf.get('relative_path') + image.url }]);
});
}

View File

@@ -11,6 +11,7 @@ var helpers = require('./helpers');
var Controllers = module.exports;
Controllers.ping = require('./ping');
Controllers.home = require('./home');
Controllers.topics = require('./topics');
Controllers.posts = require('./posts');

15
src/controllers/ping.js Normal file
View File

@@ -0,0 +1,15 @@
'use strict';
var async = require('async');
var db = require('../database');
module.exports.ping = function (req, res, next) {
async.waterfall([
function (next) {
db.getObject('config', next);
},
function () {
res.status(200).send(req.path === '/sping' ? 'healthy' : '200');
},
], next);
};