From 955fe76c3c8b96c32fa69e5efbcfe2dd4ffed763 Mon Sep 17 00:00:00 2001 From: DC Date: Thu, 26 May 2016 00:50:45 -0700 Subject: [PATCH] Allow dropping files on dock icon Fixes #584 --- main/index.js | 11 ++++++++++- renderer/index.js | 46 ++++++++++++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/main/index.js b/main/index.js index 57451c59..5ba8dd2d 100644 --- a/main/index.js +++ b/main/index.js @@ -130,6 +130,7 @@ function sliceArgv (argv) { } function processArgv (argv) { + var pathsToOpen = [] argv.forEach(function (arg) { if (arg === '-n') { menu.showOpenSeedFiles() @@ -141,7 +142,15 @@ function processArgv (argv) { // Ignore OS X launchd "process serial number" argument // More: https://github.com/feross/webtorrent-desktop/issues/214 } else { - windows.main.send('dispatch', 'onOpen', arg) + pathsToOpen.push(arg) } }) + if (pathsToOpen.length > 0) openFilePaths(pathsToOpen) +} + +// Convert paths to {name, path, size} objects, then send to renderer process +// Opening files means either adding torrents, creating and seeding a torrent +// from files, or adding subtitles +function openFilePaths (paths) { + windows.main.send('dispatch', 'onOpen', paths) } diff --git a/renderer/index.js b/renderer/index.js index b218a1bc..b2e4c18f 100644 --- a/renderer/index.js +++ b/renderer/index.js @@ -804,20 +804,38 @@ function startTorrentingSummary (torrentSummary) { // Shows the Create Torrent page with options to seed a given file or folder function showCreateTorrent (files) { if (Array.isArray(files)) { - state.location.go({ - url: 'create-torrent', - files: files - }) - return + if (files.length === 0 || typeof files[0] !== 'string') { + state.location.go({ + url: 'create-torrent', + files: files + }) + return + } + } else { + files = [files] } - var fileOrFolder = files - findFilesRecursive(fileOrFolder, showCreateTorrent) + findFilesRecursive(files, showCreateTorrent) } // Recursively finds {name, path, size} for all files in a folder // Calls `cb` on success, calls `onError` on failure -function findFilesRecursive (fileOrFolder, cb) { +function findFilesRecursive (paths, cb) { + if (paths.length > 1) { + var numComplete = 0 + var ret = [] + paths.forEach(function (path) { + findFilesRecursive([path], function (fileObjs) { + ret = ret.concat(fileObjs) + if (++numComplete === paths.length) { + cb(ret) + } + }) + }) + return + } + + var fileOrFolder = paths[0] fs.stat(fileOrFolder, function (err, stat) { if (err) return onError(err) @@ -835,16 +853,8 @@ function findFilesRecursive (fileOrFolder, cb) { var folderPath = fileOrFolder fs.readdir(folderPath, function (err, fileNames) { if (err) return onError(err) - var numComplete = 0 - var ret = [] - fileNames.forEach(function (fileName) { - findFilesRecursive(path.join(folderPath, fileName), function (fileObjs) { - ret = ret.concat(fileObjs) - if (++numComplete === fileNames.length) { - cb(ret) - } - }) - }) + var paths = fileNames.map((fileName) => path.join(folderPath, fileName)) + findFilesRecursive(paths, cb) }) }) }