feat(forums): add attach download route & method

This commit is contained in:
OldHawk
2017-07-14 12:29:57 +08:00
parent bf70c7eebe
commit 5419d2eaa0
3 changed files with 224 additions and 0 deletions

View File

@@ -7,6 +7,8 @@ var path = require('path'),
config = require(path.resolve('./config/config')),
mongoose = require('mongoose'),
errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller')),
multer = require('multer'),
fs = require('fs'),
moment = require('moment'),
User = mongoose.model('User'),
Forum = mongoose.model('Forum'),
@@ -156,6 +158,31 @@ exports.postNewTopic = function (req, res) {
topic.forum = forum;
topic.user = req.user;
//move temp torrent file to dest directory
req.body._uImage.forEach(function (f) {
var oldPath = config.uploads.attach.file.temp + f.filename;
var newPath = config.uploads.attach.file.dest + f.filename;
move(oldPath, newPath, function (err) {
if (err) {
console.log(err);
}
});
});
req.body._attach.forEach(function (f) {
var oldPath = config.uploads.attach.file.temp + f.filename;
var newPath = config.uploads.attach.file.dest + f.filename;
move(oldPath, newPath, function (err) {
if (err) {
console.log(err);
}
});
});
//replace content path
var regex = new RegExp('/modules/forums/client/attach/temp/', 'g');
topic.content = topic.content.replace(regex, '/modules/forums/client/attach/');
//save topic
topic.save(function (err) {
if (err) {
return res.status(422).send({
@@ -172,6 +199,39 @@ exports.postNewTopic = function (req, res) {
});
};
/**
* move file
* @param oldPath
* @param newPath
* @param callback
*/
function move(oldPath, newPath, callback) {
fs.rename(oldPath, newPath, function (err) {
if (err) {
if (err.code === 'EXDEV') {
copy();
} else {
callback(err);
}
return;
}
callback();
});
function copy() {
var readStream = fs.createReadStream(oldPath);
var writeStream = fs.createWriteStream(newPath);
readStream.on('error', callback);
writeStream.on('error', callback);
readStream.on('close', function () {
fs.unlink(oldPath, callback);
});
readStream.pipe(writeStream);
}
}
/**
* read readTopic
* @param req
@@ -338,11 +398,36 @@ exports.postNewReply = function (req, res) {
var reply = new Reply(req.body);
reply.user = req.user;
//replace content path
var regex = new RegExp('/modules/forums/client/attach/temp/', 'g');
reply.content = reply.content.replace(regex, '/modules/forums/client/attach/');
topic._replies.push(reply);
topic.replyCount++;
topic.lastReplyAt = Date.now();
topic.lastUser = req.user;
//move temp torrent file to dest directory
req.body._uImage.forEach(function (f) {
var oldPath = config.uploads.attach.file.temp + f.filename;
var newPath = config.uploads.attach.file.dest + f.filename;
move(oldPath, newPath, function (err) {
if (err) {
console.log(err);
}
});
});
req.body._attach.forEach(function (f) {
var oldPath = config.uploads.attach.file.temp + f.filename;
var newPath = config.uploads.attach.file.dest + f.filename;
move(oldPath, newPath, function (err) {
if (err) {
console.log(err);
}
});
});
//save topic
topic.save(function (err) {
if (err) {
return res.status(422).send({
@@ -424,6 +509,138 @@ exports.deleteReply = function (req, res) {
});
};
/**
* attachUpload
* @param req
* @param res
*/
exports.attachUpload = function (req, res) {
var user = req.user;
var createUploadAttachFilename = require(path.resolve('./config/lib/multer')).createUploadAttachFilename;
var getUploadAttachDestination = require(path.resolve('./config/lib/multer')).getUploadAttachDestination;
var attachInfo = {};
var storage = multer.diskStorage({
destination: getUploadAttachDestination,
filename: createUploadAttachFilename
});
var upload = multer({
storage: storage,
//fileFilter: fileFilter,
limits: config.uploads.attach.file.limits
}).single('newAttachFile');
if (user) {
uploadFile()
.then(function () {
res.status(200).send(attachInfo);
})
.catch(function (err) {
res.status(422).send(err);
console.log(err);
if (req.file && req.file.filename) {
var newfile = config.uploads.attach.file.temp + req.file.filename;
if (fs.existsSync(newfile)) {
console.log(err);
console.log('ERROR: DELETE TEMP ATTACH FILE: ' + newfile);
fs.unlinkSync(newfile);
}
}
});
} else {
res.status(401).send({
message: 'User is not signed in'
});
}
function uploadFile() {
return new Promise(function (resolve, reject) {
upload(req, res, function (uploadError) {
if (uploadError) {
var message = errorHandler.getErrorMessage(uploadError);
if (uploadError.code === 'LIMIT_FILE_SIZE') {
message = 'Attach file too large. Maximum size allowed is ' + (config.uploads.attach.file.limits.fileSize / (1024 * 1024)).toFixed(2) + ' Mb files.';
}
reject(message);
} else {
attachInfo.filename = req.file.filename;
resolve();
}
});
});
}
};
/**
* attachDownload
* @param req
* @param res
*/
exports.attachDownload = function (req, res) {
var topic = req.topic;
var filePath = undefined;
if (req.params.replyId) {
topic._replies.forEach(function (rep) {
if (rep._id.equals(req.params.replyId)) {
rep._attach.forEach(function (at) {
if (at._id.equals(req.query.attachId)) {
at.downCount++;
filePath = config.uploads.attach.file.dest + at.filename;
topic.markModified('_replies');
topic.save();
}
});
}
});
} else {
topic._attach.forEach(function (at) {
if (at._id.equals(req.query.attachId)) {
at.downCount++;
filePath = config.uploads.attach.file.dest + at.filename;
topic.save();
}
});
}
return downFile(filePath);
function downFile(filePath) {
if (!filePath) {
res.status(422).send({
message: 'FILE_DOES_NOT_FINDED'
});
}
fs.exists(filePath, function (exists) {
if (exists) {
var stat = fs.statSync(filePath);
try {
//res.set('Content-Type', 'application/x-bittorrent');
res.set('Content-Disposition', 'attachment; filename=' + req.params.filename);
res.set('Content-Length', stat.size);
fs.createReadStream(filePath).pipe(res);
} catch (err) {
res.status(422).send({
message: 'DOWNLOAD_FAILED'
});
}
} else {
res.status(422).send({
message: 'FILE_DOES_NOT_EXISTS'
});
}
});
}
};
/**
* Invitation middleware
*/