From 01b3d2ef1cc37ab8ac200401ded597a8a655dca2 Mon Sep 17 00:00:00 2001 From: OldHawk Date: Fri, 7 Apr 2017 23:29:32 +0800 Subject: [PATCH] user can download torrent file now --- modules/core/client/app/config.js | 2 +- modules/core/client/app/trans-string-cn.js | 2 ++ modules/core/client/app/trans-string-en.js | 2 ++ .../controllers/torrents.client.controller.js | 25 +++++++++++-- .../client/views/movie-list.client.view.html | 2 +- .../controllers/torrents.server.controller.js | 35 +++++++++++++++++++ .../server/routes/torrents.server.routes.js | 3 ++ 7 files changed, 67 insertions(+), 4 deletions(-) diff --git a/modules/core/client/app/config.js b/modules/core/client/app/config.js index f9ed4c68..c38f9e46 100644 --- a/modules/core/client/app/config.js +++ b/modules/core/client/app/config.js @@ -7,7 +7,7 @@ applicationEnvironment: window.env, applicationModuleName: applicationModuleName, applicationModuleVendorDependencies: ['ngResource', 'ngAnimate', 'ngMessages', 'ui.router', 'ui.bootstrap', 'ngFileUpload', 'ui-notification', - 'pascalprecht.translate', 'angularMoment'], + 'pascalprecht.translate', 'angularMoment', 'ngFileSaver'], registerModule: registerModule }; diff --git a/modules/core/client/app/trans-string-cn.js b/modules/core/client/app/trans-string-cn.js index 03e9ec5c..95e3f699 100644 --- a/modules/core/client/app/trans-string-cn.js +++ b/modules/core/client/app/trans-string-cn.js @@ -90,6 +90,8 @@ MORE_TAGS: '显示更多标签', CA_RESET: '重置条件', CA_DOWNLOAD: '下载种子', + TORRENT_DOWNLOAD_ERROR: '种子文件下载失败', + TORRENTS_DOWNLOAD_SUCCESSFULLY: '种子文件下载成功', //TorrentsUploadsController TORRENTS_UPLOAD_SUCCESSFULLY: '文件上传成功', diff --git a/modules/core/client/app/trans-string-en.js b/modules/core/client/app/trans-string-en.js index 50d2549c..6b9864e7 100644 --- a/modules/core/client/app/trans-string-en.js +++ b/modules/core/client/app/trans-string-en.js @@ -90,6 +90,8 @@ MORE_TAGS: 'More Tags', CA_RESET: 'Reset', CA_DOWNLOAD: 'Download', + TORRENT_DOWNLOAD_ERROR: 'Torrent file download faild', + TORRENTS_DOWNLOAD_SUCCESSFULLY: 'Torrents file download successfully', //TorrentsUploadsController & views TORRENTS_UPLOAD_SUCCESSFULLY: 'Successfully uploads file', diff --git a/modules/torrents/client/controllers/torrents.client.controller.js b/modules/torrents/client/controllers/torrents.client.controller.js index b6d6d1b7..f12aa2ab 100644 --- a/modules/torrents/client/controllers/torrents.client.controller.js +++ b/modules/torrents/client/controllers/torrents.client.controller.js @@ -6,9 +6,10 @@ .controller('TorrentsController', TorrentsController); TorrentsController.$inject = ['$scope', '$state', '$translate', '$timeout', 'Authentication', 'Notification', 'TorrentsService', - 'MeanTorrentConfig']; + 'MeanTorrentConfig', 'DownloadService']; - function TorrentsController($scope, $state, $translate, $timeout, Authentication, Notification, TorrentsService, MeanTorrentConfig) { + function TorrentsController($scope, $state, $translate, $timeout, Authentication, Notification, TorrentsService, MeanTorrentConfig, + DownloadService) { var vm = this; vm.user = Authentication.user; vm.tmdbConfig = MeanTorrentConfig.meanTorrentConfig.tmdbConfig; @@ -194,5 +195,25 @@ i.removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-up'); } }; + + /** + * downloadTorrent + * @param id + */ + vm.downloadTorrent = function (id) { + var url = '/api/torrents/download/' + id; + DownloadService.downloadTorrentFile(url, null, function (status) { + if (status === 200) { + Notification.success({ + message: ' ' + $translate.instant('TORRENTS_DOWNLOAD_SUCCESSFULLY') + }); + } + }, function (err) { + Notification.error({ + message: err.data.message, + title: ' ' + $translate.instant('TORRENT_DOWNLOAD_ERROR') + }); + }); + }; } }()); diff --git a/modules/torrents/client/views/movie-list.client.view.html b/modules/torrents/client/views/movie-list.client.view.html index e2d1be79..0473168b 100644 --- a/modules/torrents/client/views/movie-list.client.view.html +++ b/modules/torrents/client/views/movie-list.client.view.html @@ -121,7 +121,7 @@ / {{item.torrent_title}} + ng-click="vm.downloadTorrent(item._id);"> {{ 'CA_DOWNLOAD' | translate}} diff --git a/modules/torrents/server/controllers/torrents.server.controller.js b/modules/torrents/server/controllers/torrents.server.controller.js index 8a534f69..b7a9cd0c 100644 --- a/modules/torrents/server/controllers/torrents.server.controller.js +++ b/modules/torrents/server/controllers/torrents.server.controller.js @@ -168,6 +168,41 @@ exports.upload = function (req, res) { }; +/** + * download a torrent file + * @param req + * @param res + */ +exports.download = function (req, res) { + var filePath = config.uploads.torrent.file.dest + req.torrent.torrent_filename; + var stat = fs.statSync(filePath); + + fs.exists(filePath, function (exists) { + if (exists) { + var options = { + root: path.join(__dirname, '../../../../'), + headers: { + 'Content-Type': 'application/x-bittorrent', + 'Content-Disposition': 'attachment; filename=' + config.meanTorrentConfig.announce.announce_prefix + req.torrent.torrent_filename, + 'Content-Length': stat.size + } + }; + res.sendFile(filePath, options); + + //res.writeHead(200, { + // 'Content-Type': 'application/octet-stream', + // 'Content-Disposition': 'attachment; filename=' + config.meanTorrentConfig.announce.announce_prefix + req.torrent.torrent_filename, + // 'Content-Length': stat.size + //}); + //fs.createReadStream(filePath).pipe(res); + } else { + res.status(401).send({ + message: 'FILE_DOES_NOT_EXISTS' + }); + } + }); +}; + /** * create a torrent * @param req diff --git a/modules/torrents/server/routes/torrents.server.routes.js b/modules/torrents/server/routes/torrents.server.routes.js index 38567082..5cd3ddda 100644 --- a/modules/torrents/server/routes/torrents.server.routes.js +++ b/modules/torrents/server/routes/torrents.server.routes.js @@ -13,6 +13,9 @@ module.exports = function (app) { app.route('/api/torrents/upload') .post(torrents.upload); + app.route('/api/torrents/download/:torrentId') + .get(torrents.download); + app.route('/api/torrents') .get(torrents.list) .post(torrents.create);