From 6efad86a17ff64cdd2816ee2f9995dfc7830195f Mon Sep 17 00:00:00 2001 From: OldHawk Date: Thu, 28 Sep 2017 15:28:15 +0800 Subject: [PATCH] feat(torrents): make torrent image uploads folder --- config/env/default.js | 14 ++++++++ config/lib/multer.js | 79 ++++++++++++++++++++++++++++++++++++++++++- gulpfile.js | 5 +++ 3 files changed, 97 insertions(+), 1 deletion(-) diff --git a/config/env/default.js b/config/env/default.js index 70c21070..eab01b9c 100644 --- a/config/env/default.js +++ b/config/env/default.js @@ -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: { diff --git a/config/lib/multer.js b/config/lib/multer.js index 373cf3e7..5227fc10 100644 --- a/config/lib/multer.js +++ b/config/lib/multer.js @@ -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); +}; diff --git a/gulpfile.js b/gulpfile.js index f3016ced..9dd2505b 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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; });