From 31ef283e7b086ca2b0b2d4a662dff6ec1509e026 Mon Sep 17 00:00:00 2001 From: DC Date: Wed, 20 Apr 2016 22:25:56 -0700 Subject: [PATCH] Create Torrent dialog --- main/index.js | 2 +- main/ipc.js | 2 +- main/menu.js | 14 ++-- renderer/index.js | 101 ++++++++++--------------- renderer/views/create-torrent-modal.js | 17 ++++- 5 files changed, 60 insertions(+), 76 deletions(-) diff --git a/main/index.js b/main/index.js index 3d36c66f..c880386b 100644 --- a/main/index.js +++ b/main/index.js @@ -120,7 +120,7 @@ function sliceArgv (argv) { function processArgv (argv) { argv.forEach(function (arg) { if (arg === '-n') { - windows.main.send('dispatch', 'showCreateTorrent') + windows.main.send('dispatch', 'showOpenSeedFiles') } else if (arg === '-o') { windows.main.send('dispatch', 'showOpenTorrentFile') } else if (arg === '-u') { diff --git a/main/ipc.js b/main/ipc.js index 79bdd928..155e0d60 100644 --- a/main/ipc.js +++ b/main/ipc.js @@ -36,7 +36,7 @@ function init () { }) ipcMain.on('showOpenTorrentFile', menu.showOpenTorrentFile) - ipcMain.on('showCreateTorrent', menu.showCreateTorrent) + ipcMain.on('showOpenSeedFiles', menu.showOpenSeedFiles) ipcMain.on('setBounds', function (e, bounds, maximize) { setBounds(bounds, maximize) diff --git a/main/menu.js b/main/menu.js index 868d2258..5b06d8e0 100644 --- a/main/menu.js +++ b/main/menu.js @@ -5,7 +5,7 @@ module.exports = { onWindowShow, onPlayerOpen, onPlayerClose, - showCreateTorrent, + showOpenSeedFiles, showOpenTorrentFile, toggleFullScreen } @@ -109,7 +109,7 @@ function getMenuItem (label) { } // Prompts the user for a file or folder, then makes a torrent out of the data -function showCreateTorrent () { +function showOpenSeedFiles () { // Allow only a single selection // To create a multi-file torrent, the user must select a folder electron.dialog.showOpenDialog({ @@ -117,10 +117,8 @@ function showCreateTorrent () { properties: [ 'openFile', 'openDirectory' ] }, function (filenames) { if (!Array.isArray(filenames)) return - var options = { - files: filenames[0] - } - windows.main.send('dispatch', 'createTorrent', options) + var fileOrFolder = filenames[0] + windows.main.send('dispatch', 'showCreateTorrent', fileOrFolder) }) } @@ -148,7 +146,7 @@ function getAppMenuTemplate () { { label: 'Create New Torrent...', accelerator: 'CmdOrCtrl+N', - click: showCreateTorrent + click: showOpenSeedFiles }, { label: 'Open Torrent File...', @@ -373,7 +371,7 @@ function getDockMenuTemplate () { { label: 'Create New Torrent...', accelerator: 'CmdOrCtrl+N', - click: showCreateTorrent + click: showOpenSeedFiles }, { label: 'Open Torrent File...', diff --git a/renderer/index.js b/renderer/index.js index ab6206de..ef19400d 100644 --- a/renderer/index.js +++ b/renderer/index.js @@ -211,12 +211,15 @@ function dispatch (action, ...args) { if (action === 'addTorrent') { addTorrent(args[0] /* torrent */) } - if (action === 'showCreateTorrent') { - ipcRenderer.send('showCreateTorrent') /* open file or folder to seed */ + if (action === 'showOpenSeedFiles') { + ipcRenderer.send('showOpenSeedFiles') /* open file or folder to seed */ } if (action === 'showOpenTorrentFile') { ipcRenderer.send('showOpenTorrentFile') /* open torrent file */ } + if (action === 'showCreateTorrent') { + showCreateTorrent(args[0] /* fileOrFolder */) + } if (action === 'createTorrent') { createTorrent(args[0] /* options */) } @@ -521,7 +524,10 @@ function onOpen (files) { // everything else = seed these files var rest = files.filter(not(isTorrent)).filter(not(isSubtitle)) if (rest.length > 0) { - createTorrentFromFileObjects(rest) + state.modal = { + id: 'create-torrent-modal', + files: rest + } } } @@ -626,62 +632,43 @@ function startTorrentingSummary (torrentSummary) { ipcRenderer.send('wt-start-torrenting', s.torrentKey, torrentID, path, s.fileModtimes) } -// TODO: maybe have a "create torrent" modal in the future, with options like -// custom trackers, private flag, and so on? -// -// Right now create-torrent-modal is v basic, only user input is OK / Cancel -// -// Also, if you uncomment below below, creating a torrent thru -// File > Create New Torrent will still create a new torrent directly, while -// dragging files or folders onto the app opens the create-torrent-modal -// -// That's because the former gets a single string and the latter gets a list -// of W3C File objects. We should fix this inconsistency, ideally without -// duping this code in the drag-drop module: -// https://github.com/feross/drag-drop/blob/master/index.js -// -// function showCreateTorrentModal (files) { -// if (files.length === 0) return -// state.modal = { -// id: 'create-torrent-modal', -// files: files -// } -// } - // // TORRENT MANAGEMENT // Send commands to the WebTorrent process, handle events // -// Creates a new torrent from a drag-dropped file or folder -function createTorrentFromFileObjects (files) { - var filePaths = files.map((x) => x.path) - - // Single-file torrents are easy. Multi-file torrents require special handling - // make sure WebTorrent seeds all files in place, without copying to /tmp - if (filePaths.length === 1) { - return createTorrent({files: filePaths[0]}) +// Shows the Create Torrent page with options to seed a given file or folder +function showCreateTorrent (files) { + if (Array.isArray(files)) { + state.modal = { + id: 'create-torrent-modal', + files: files + } + return } - // First, extract the base folder that the files are all in - var pathPrefix = files.map((x) => x.path).reduce(findCommonPrefix) - if (files.length > 0 && !pathPrefix.endsWith('/') && !pathPrefix.endsWith('\\')) { - pathPrefix = path.dirname(pathPrefix) - } - - // Then, use the name of the base folder (or sole file, for a single file torrent) - // as the default name. Show all files relative to the base folder. - var defaultName = path.basename(pathPrefix) - var basePath = path.dirname(pathPrefix) - var options = { - // TODO: we can't let the user choose their own name if we want WebTorrent - // to use the files in place rather than creating a new folder. - name: defaultName, - path: basePath, - files: filePaths - } - - createTorrent(options) + var fileOrFolder = files + fs.stat(fileOrFolder, function (err, stat) { + if (err) return onError(err) + if (stat.isDirectory()) { + fs.readdir(fileOrFolder, function (err, fileNames) { + if (err) return onError(err) + // TODO: support nested folders + var fileObjs = fileNames.map(function (fileName) { + return { + name: fileName, + path: path.join(fileOrFolder, fileName) + } + }) + showCreateTorrent(fileObjs) + }) + } else { + showCreateTorrent([{ + name: path.basename(fileOrFolder), + path: fileOrFolder + }]) + } + }) } // Creates a new torrent and start seeeding @@ -1078,16 +1065,6 @@ function showDoneNotification (torrent) { sound.play('DONE') } -// Finds the longest common prefix -function findCommonPrefix (a, b) { - for (var i = 0; i < a.length && i < b.length; i++) { - if (a.charCodeAt(i) !== b.charCodeAt(i)) break - } - if (i === a.length) return a - if (i === b.length) return b - return a.substring(0, i) -} - // Hide player controls while playing video, if the mouse stays still for a while // Never hide the controls when: // * The mouse is over the controls or we're scrubbing (see CSS) diff --git a/renderer/views/create-torrent-modal.js b/renderer/views/create-torrent-modal.js index c6176bfa..d085f811 100644 --- a/renderer/views/create-torrent-modal.js +++ b/renderer/views/create-torrent-modal.js @@ -9,11 +9,20 @@ var path = require('path') var {dispatch} = require('../lib/dispatcher') function UpdateAvailableModal (state) { + var info = state.modal + // First, extract the base folder that the files are all in - var files = state.modal.files - var pathPrefix = files.map((x) => x.path).reduce(findCommonPrefix) - if (files.length > 0 && !pathPrefix.endsWith('/') && !pathPrefix.endsWith('\\')) { - pathPrefix = path.dirname(pathPrefix) + var files = info.files + var pathPrefix = info.folderPath + if (!pathPrefix) { + if (files.length > 0) { + pathPrefix = files.map((x) => x.path).reduce(findCommonPrefix) + if (!pathPrefix.endsWith('/') && !pathPrefix.endsWith('\\')) { + pathPrefix = path.dirname(pathPrefix) + } + } else { + pathPrefix = files[0] + } } // Then, use the name of the base folder (or sole file, for a single file torrent)