From b950829de3dfe2aaa676c1133c8412724f7890a1 Mon Sep 17 00:00:00 2001 From: DC Date: Fri, 15 Jul 2016 02:31:59 -0700 Subject: [PATCH] TorrentSummary.getFileOrFolder --- renderer/controllers/torrent-list-controller.js | 4 ++-- renderer/lib/torrent-summary.js | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/renderer/controllers/torrent-list-controller.js b/renderer/controllers/torrent-list-controller.js index ffa18591..27be6156 100644 --- a/renderer/controllers/torrent-list-controller.js +++ b/renderer/controllers/torrent-list-controller.js @@ -245,7 +245,7 @@ function deleteFile (path) { // Delete all files in a torrent function moveItemToTrash (torrentSummary) { - var filePath = path.join(torrentSummary.path, torrentSummary.files[0].path.split('/')[0]) + var filePath = TorrentSummary.getFileOrFolder(torrentSummary) ipcRenderer.send('moveItemToTrash', filePath) } @@ -255,7 +255,7 @@ function showItemInFolder (torrentSummary) { function saveTorrentFileAs (torrentSummary) { var downloadPath = this.state.saved.prefs.downloadPath - var newFileName = `${path.parse(torrentSummary.name).name}.torrent` + var newFileName = path.parse(torrentSummary.name).name + '.torrent' var opts = { title: 'Save Torrent File', defaultPath: path.join(downloadPath, newFileName), diff --git a/renderer/lib/torrent-summary.js b/renderer/lib/torrent-summary.js index 56e048ff..fd5b0d80 100644 --- a/renderer/lib/torrent-summary.js +++ b/renderer/lib/torrent-summary.js @@ -2,7 +2,8 @@ module.exports = { getPosterPath, getTorrentPath, getByKey, - getTorrentID + getTorrentID, + getFileOrFolder } var path = require('path') @@ -43,3 +44,13 @@ function getByKey (state, torrentKey) { return state.saved.torrents.find((x) => x.torrentKey === torrentKey || x.infoHash === torrentKey) } + +// Returns the path to either the file (in a single-file torrent) or the root +// folder (in multi-file torrent) +// WARNING: assumes that multi-file torrents consist of a SINGLE folder. +// TODO: make this assumption explicit, enforce it in the `create-torrent` +// module. Store root folder explicitly to avoid hacky path processing below. +function getFileOrFolder (torrentSummary) { + var ts = torrentSummary + return path.join(ts.path, ts.files[0].path.split('/')[0]) +}