user can download torrent file now

This commit is contained in:
OldHawk
2017-04-07 23:29:32 +08:00
parent 4d81462cb2
commit 01b3d2ef1c
7 changed files with 67 additions and 4 deletions

View File

@@ -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
};

View File

@@ -90,6 +90,8 @@
MORE_TAGS: '显示更多标签',
CA_RESET: '重置条件',
CA_DOWNLOAD: '下载种子',
TORRENT_DOWNLOAD_ERROR: '种子文件下载失败',
TORRENTS_DOWNLOAD_SUCCESSFULLY: '种子文件下载成功',
//TorrentsUploadsController
TORRENTS_UPLOAD_SUCCESSFULLY: '文件上传成功',

View File

@@ -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',

View File

@@ -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: '<i class="glyphicon glyphicon-ok"></i> ' + $translate.instant('TORRENTS_DOWNLOAD_SUCCESSFULLY')
});
}
}, function (err) {
Notification.error({
message: err.data.message,
title: '<i class="glyphicon glyphicon-remove"></i> ' + $translate.instant('TORRENT_DOWNLOAD_ERROR')
});
});
};
}
}());

View File

@@ -121,7 +121,7 @@
<span ng-show="item.torrent_original_title!=item.torrent_title"> / {{item.torrent_title}}</span>
<span class="label label-download text-uppercase"
title="{{ 'TITLE_ALT.DOWNLOAD_TORRENT' | translate}}"
ng-click="vm.downloadTorrent();">
ng-click="vm.downloadTorrent(item._id);">
<i class="glyphicon glyphicon-arrow-right"></i> {{ 'CA_DOWNLOAD' | translate}}
</span>
</h5>

View File

@@ -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

View File

@@ -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);