feat(torrents): make torrent image uploads folder

This commit is contained in:
OldHawk
2017-09-28 15:28:15 +08:00
parent c92f5adc9a
commit 6efad86a17
3 changed files with 97 additions and 1 deletions

14
config/env/default.js vendored
View File

@@ -61,6 +61,20 @@ module.exports = {
limits: {
fileSize: 1 * 1024 * 1024 // Max file size in bytes (1 MB)
}
},
cover: {
dest: './modules/torrents/client/uploads/cover/',
temp: './modules/torrents/client/uploads/temp/',
limits: {
fileSize: 1 * 1024 * 1024 // Max file size in bytes (1 MB)
}
},
image: {
dest: './modules/torrents/client/uploads/image/',
temp: './modules/torrents/client/uploads/temp/',
limits: {
fileSize: 1 * 1024 * 1024 // Max file size in bytes (1 MB)
}
}
},
attach: {

View File

@@ -5,7 +5,7 @@ var path = require('path'),
config = require(path.resolve('./config/config'));
module.exports.imageFileFilter = function (req, file, callback) {
if (file.mimetype !== 'image/png' && file.mimetype !== 'image/jpg' && file.mimetype !== 'image/jpeg' && file.mimetype !== 'image/gif') {
if (file.mimetype !== 'image/png' && file.mimetype !== 'image/jpg' && file.mimetype !== 'image/jpeg' && file.mimetype !== 'image/gif' && file.mimetype !== 'image/bmp') {
var err = new Error();
err.code = 'UNSUPPORTED_MEDIA_TYPE';
return callback(err, false);
@@ -125,3 +125,80 @@ module.exports.createUploadSubtitleFilename = function (req, file, cb) {
module.exports.getUploadSubtitleDestination = function (req, file, cb) {
cb(null, config.uploads.subtitle.file.dest);
};
module.exports.createUploadCoverImageFilename = function (req, file, cb) {
var RexStr = /\(|\)|\[|\]|\,/g;
var filename = file.originalname.replace(RexStr, function (MatchStr) {
switch (MatchStr) {
case '(':
return '<';
case ')':
return '>';
case '[':
return '{';
case ']':
return '}';
case ',':
return ' ';
default:
break;
}
});
if (fs.existsSync(config.uploads.torrent.cover.temp + filename)) {
fs.unlinkSync(config.uploads.torrent.cover.temp + filename);
}
if (fs.existsSync(config.uploads.torrent.cover.dest + filename)) {
var ext = file.originalname.replace(/^.+\./, '');
var regex = new RegExp(ext, 'g');
filename = filename.replace(regex, Date.now() + '.' + ext);
cb(null, filename);
} else {
cb(null, filename);
}
};
module.exports.getUploadCoverImageDestination = function (req, file, cb) {
cb(null, config.uploads.torrent.cover.temp);
};
module.exports.createUploadTorrentImageFilename = function (req, file, cb) {
var RexStr = /\(|\)|\[|\]|\,/g;
var filename = file.originalname.replace(RexStr, function (MatchStr) {
switch (MatchStr) {
case '(':
return '<';
case ')':
return '>';
case '[':
return '{';
case ']':
return '}';
case ',':
return ' ';
default:
break;
}
});
if (fs.existsSync(config.uploads.torrent.image.temp + filename)) {
fs.unlinkSync(config.uploads.torrent.image.temp + filename);
}
if (fs.existsSync(config.uploads.torrent.image.dest + filename)) {
var ext = file.originalname.replace(/^.+\./, '');
var regex = new RegExp(ext, 'g');
filename = filename.replace(regex, Date.now() + '.' + ext);
cb(null, filename);
} else {
cb(null, filename);
}
};
module.exports.getUploadTorrentImageDestination = function (req, file, cb) {
cb(null, config.uploads.torrent.image.temp);
};

View File

@@ -289,6 +289,11 @@ gulp.task('makeUploadsDir', function () {
console.error(err);
}
});
fs.mkdir('modules/torrents/client/uploads/image', function (err) {
if (err && err.code !== 'EEXIST') {
console.error(err);
}
});
return;
});