From 752ea28bde34fe169c57133ba438fbf2f36d3e84 Mon Sep 17 00:00:00 2001 From: OldHawk Date: Wed, 11 Oct 2017 17:17:58 +0800 Subject: [PATCH] fix(torrents): move image files when create new torrent --- .../controllers/uploads.client.controller.js | 8 +++-- .../controllers/torrents.server.controller.js | 31 +++++++++++++++---- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/modules/torrents/client/controllers/uploads.client.controller.js b/modules/torrents/client/controllers/uploads.client.controller.js index 9236edf5..5f1b082f 100644 --- a/modules/torrents/client/controllers/uploads.client.controller.js +++ b/modules/torrents/client/controllers/uploads.client.controller.js @@ -199,6 +199,10 @@ vm.tvinfo = undefined; vm.tmdb_id = undefined; + vm.tags = []; + vm.videoNfo = ''; + vm.customTorrent = {}; + vm.showVideoNfo = false; vm.showAgreeAndSubmit = false; }; @@ -441,9 +445,7 @@ var uimg = []; angular.forEach($scope.uImages, function (f) { mtDebug.info(f); - uimg.push({ - filename: f.name - }); + uimg.push(f.name); }); var torrent = new TorrentsService({ diff --git a/modules/torrents/server/controllers/torrents.server.controller.js b/modules/torrents/server/controllers/torrents.server.controller.js index 875b8f25..d95223f3 100644 --- a/modules/torrents/server/controllers/torrents.server.controller.js +++ b/modules/torrents/server/controllers/torrents.server.controller.js @@ -526,18 +526,24 @@ exports.create = function (req, res) { } else { //move temp cover image file to dest directory var cv = req.body.resource_detail_info.cover; - var o = config.uploads.torrent.cover.temp + cv; - var n = config.uploads.torrent.cover.dest + cv; - move(o, n, function (err) { + var oc = config.uploads.torrent.cover.temp + cv; + var nc = config.uploads.torrent.cover.dest + cv; + copy(oc, nc, function (err) { if (err) { mtDebug.debugRed(err); } + + if (req.body._uImage.indexOf(cv) < 0) { + fs.unlink(oc); + } }); + + //move temp torrent image file to dest directory req.body._uImage.forEach(function (f) { - o = config.uploads.torrent.image.temp + f.filename; - n = config.uploads.torrent.image.dest + f.filename; - move(o, n, function (err) { + var oi = config.uploads.torrent.image.temp + f; + var ni = config.uploads.torrent.image.dest + f; + move(oi, ni, function (err) { if (err) { mtDebug.debugRed(err); } @@ -568,6 +574,19 @@ exports.create = function (req, res) { } }); + function copy(oldPath, newPath, callback) { + var readStream = fs.createReadStream(oldPath); + var writeStream = fs.createWriteStream(newPath); + + readStream.on('error', callback); + writeStream.on('error', callback); + + readStream.on('close', function () { + callback(); + }); + readStream.pipe(writeStream); + } + function move(oldPath, newPath, callback) { fs.rename(oldPath, newPath, function (err) { if (err) {