Seed in place, don't copy to /tmp

Fixes https://github.com/feross/webtorrent-desktop/issues/254
This commit is contained in:
DC
2016-04-01 03:37:22 -07:00
parent c975f2f2d0
commit 20157f39ee
5 changed files with 133 additions and 13 deletions

View File

@@ -117,12 +117,14 @@ function getMenuItem (label) {
// Prompts the user for a file or folder, then makes a torrent out of the data
function showCreateTorrent () {
// Allow only a single selection
// To create a multi-file torrent, the user must select a folder
electron.dialog.showOpenDialog({
title: 'Select a file or folder for the torrent file.',
properties: [ 'openFile', 'openDirectory', 'multiSelections' ]
properties: [ 'openFile', 'openDirectory' ]
}, function (filenames) {
if (!Array.isArray(filenames)) return
windows.main.send('dispatch', 'seed', filenames)
windows.main.send('dispatch', 'seed', filenames[0])
})
}

View File

@@ -271,10 +271,32 @@ i:not(.disabled):hover {
padding: 20px;
}
.create-torrent-modal input,
.open-torrent-address-modal input {
width: calc(100% - 100px)
}
.create-torrent-modal .torrent-attribute {
white-space: nowrap;
}
.create-torrent-modal .torrent-attribute>* {
display: inline-block;
}
.create-torrent-modal .torrent-attribute label {
width: 60px;
margin-right: 10px;
vertical-align: top;
}
.create-torrent-modal .torrent-attribute div {
font-family: Consolas, monospace;
white-space: nowrap;
}
/*
* BUTTONS
*/

View File

@@ -1,7 +1,7 @@
console.time('init')
var cfg = require('application-config')('WebTorrent')
var createTorrent = require('create-torrent')
var defaultAnnounceList = require('create-torrent').announceList
var dragDrop = require('drag-drop')
var electron = require('electron')
var EventEmitter = require('events')
@@ -42,7 +42,7 @@ var dialog = remote.require('dialog')
var state = global.state = require('./state')
// Force use of webtorrent trackers on all torrents
global.WEBTORRENT_ANNOUNCE = createTorrent.announceList
global.WEBTORRENT_ANNOUNCE = defaultAnnounceList
.map((arr) => arr[0])
.filter((url) => url.indexOf('wss://') === 0 || url.indexOf('ws://') === 0)
@@ -207,13 +207,21 @@ function dispatch (action, ...args) {
addTorrent(args[0] /* torrent */)
}
if (action === 'showCreateTorrent') {
ipcRenderer.send('showCreateTorrent')
ipcRenderer.send('showCreateTorrent') /* open file or folder to seed */
}
if (action === 'showOpenTorrentFile') {
ipcRenderer.send('showOpenTorrentFile')
ipcRenderer.send('showOpenTorrentFile') /* open torrent file */
}
if (action === 'seed') {
seed(args[0] /* files */)
// TODO: right now, creating a torrent thru File > Create New Torrent
// creates and starts seeding 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 inconsitency, ideally without
// duping this code in the drag-drop module:
// https://github.com/feross/drag-drop/blob/master/index.js
createTorrent({files: args[0]} /* file or folder path */)
}
if (action === 'openFile') {
openFile(args[0] /* infoHash */, args[1] /* index */)
@@ -316,6 +324,9 @@ function dispatch (action, ...args) {
if (action === 'saveState') {
saveState()
}
if (action === 'createTorrent') {
createTorrent(args[0] /* options */)
}
// Update the virtual-dom, unless it's just a mouse move event
if (action !== 'mediaMouseMoved') {
@@ -445,7 +456,7 @@ function onOpen (files) {
})
// everything else = seed these files
seed(files.filter(isNotTorrent))
showCreateTorrentModal(files.filter(isNotTorrent))
}
function onPaste (e) {
@@ -553,10 +564,18 @@ function stopTorrenting (infoHash) {
if (torrent) torrent.destroy()
}
// Creates a torrent for a local file and starts seeding it
function seed (files) {
// Prompts the user to create a torrent for a local file or folder
function showCreateTorrentModal (files) {
if (files.length === 0) return
var torrent = lazyLoadClient().seed(files)
state.modal = {
id: 'create-torrent-modal',
files: files
}
}
// Creates a new torrent and start seeeding
function createTorrent (options) {
var torrent = state.client.seed(options.files, options)
addTorrentToList(torrent)
addTorrentEvents(torrent)
}

View File

@@ -9,7 +9,8 @@ var Player = require('./player')
var TorrentList = require('./torrent-list')
var Modals = {
'open-torrent-address-modal': require('./open-torrent-address-modal'),
'update-available-modal': require('./update-available-modal')
'update-available-modal': require('./update-available-modal'),
'create-torrent-modal': require('./create-torrent-modal')
}
function App (state, dispatch) {
@@ -67,7 +68,7 @@ function App (state, dispatch) {
return hx`
<div class='modal'>
<div class='modal-background'></div>
<div class='modal-content add-file-modal'>
<div class='modal-content'>
${contents}
</div>
</div>

View File

@@ -0,0 +1,76 @@
module.exports = UpdateAvailableModal
var h = require('virtual-dom/h')
var hyperx = require('hyperx')
var hx = hyperx(h)
var path = require('path')
var {dispatch} = require('../lib/dispatcher')
function UpdateAvailableModal (state) {
// 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)
}
// 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 fileElems = files.map(function (file) {
var relativePath = files.length === 0 ? file.name : path.relative(pathPrefix, file.path)
return hx`<div>${relativePath}</div>`
})
return hx`
<div class='create-torrent-modal'>
<p><strong>Create New Torrent</strong></p>
<p class='torrent-attribute'>
<label>Name:</label>
<div class='torrent-attribute'>${defaultName}</div>
</p>
<p class='torrent-attribute'>
<label>Path:</label>
<div class='torrent-attribute'>${pathPrefix}</div>
</p>
<p class='torrent-attribute'>
<label>Files:</label>
<div>${fileElems}</div>
</p>
<p>
<button class='primary' onclick=${handleOK}>Create Torrent</button>
<button class='cancel' onclick=${handleCancel}>Cancel</button>
</p>
</div>
`
function handleOK () {
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: document.querySelector('.torrent-name').value
name: defaultName,
path: basePath,
files: files
}
dispatch('createTorrent', options)
dispatch('exitModal')
}
function handleCancel () {
dispatch('exitModal')
}
}
// 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)
}