`
-
- function getErrorPopover () {
- var now = new Date().getTime()
- var recentErrors = state.errors.filter((x) => now - x.time < 5000)
-
- var errorElems = recentErrors.map(function (error) {
- return hx`
${error.message}
`
- })
- return hx`
-
-
Error
- ${errorElems}
-
- `
- }
-
- function getModal () {
- if (state.modal) {
- var contents = Modals[state.modal.id](state, dispatch)
- return hx`
-
-
-
- ${contents}
-
-
- `
- }
- }
-
- function getView () {
- if (state.location.current().url === 'home') {
- return TorrentList(state, dispatch)
- } else if (state.location.current().url === 'player') {
- return Player(state, dispatch)
- }
- }
+}
+
+function getErrorPopover (state) {
+ var now = new Date().getTime()
+ var recentErrors = state.errors.filter((x) => now - x.time < 5000)
+
+ var errorElems = recentErrors.map(function (error) {
+ return hx`
+ `
+}
+
+function getView (state) {
+ var url = state.location.current().url
+ return Views[url](state)
}
diff --git a/renderer/views/create-torrent-modal.js b/renderer/views/create-torrent-modal.js
deleted file mode 100644
index d085f811..00000000
--- a/renderer/views/create-torrent-modal.js
+++ /dev/null
@@ -1,85 +0,0 @@
-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) {
- var info = state.modal
-
- // First, extract the base folder that the files are all in
- 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)
- // 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`
${relativePath}
`
- })
-
- return hx`
-
-
Create New Torrent
-
-
-
${defaultName}
-
-
-
-
${pathPrefix}
-
-
-
-
${fileElems}
-
-
-
-
-
-
- `
-
- 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)
-}
diff --git a/renderer/views/create-torrent-page.js b/renderer/views/create-torrent-page.js
new file mode 100644
index 00000000..a6ef0fab
--- /dev/null
+++ b/renderer/views/create-torrent-page.js
@@ -0,0 +1,133 @@
+module.exports = CreateTorrentPage
+
+var h = require('virtual-dom/h')
+var hyperx = require('hyperx')
+var hx = hyperx(h)
+
+var createTorrent = require('create-torrent')
+var path = require('path')
+var prettyBytes = require('prettier-bytes')
+
+var {dispatch} = require('../lib/dispatcher')
+
+function CreateTorrentPage (state) {
+ var info = state.location.current()
+
+ // First, extract the base folder that the files are all in
+ 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]
+ }
+ }
+
+ // Sanity check: show the number of files and total size
+ var numFiles = files.length
+ var totalBytes = files
+ .map((f) => f.length)
+ .reduce((a, b) => a + b, 0)
+ var torrentInfo = `${numFiles} files, ${prettyBytes(totalBytes)}`
+
+ // 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 maxFileElems = 100
+ var fileElems = files.slice(0, maxFileElems).map(function (file) {
+ var relativePath = files.length === 0 ? file.name : path.relative(pathPrefix, file.path)
+ return hx`
+ `
+
+ function handleOK () {
+ var announceList = document.querySelector('.torrent-trackers').value
+ .split('\n')
+ .map((s) => s.trim())
+ .filter((s) => s !== '')
+ var isPrivate = document.querySelector('.torrent-is-private').checked
+ var comment = document.querySelector('.torrent-comment').value.trim()
+ var options = {
+ // 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.
+ // If we ever want to add support for that:
+ // name: document.querySelector('.torrent-name').value
+ name: defaultName,
+ path: basePath,
+ files: files,
+ announce: announceList,
+ private: isPrivate,
+ comment: comment
+ }
+ dispatch('createTorrent', options)
+ dispatch('backToList')
+ }
+
+ function handleCancel () {
+ dispatch('backToList')
+ }
+
+ function handleToggleShowAdvanced () {
+ // TODO: what's the clean way to handle this?
+ // Should every button on every screen have its own dispatch()?
+ info.showAdvanced = !info.showAdvanced
+ dispatch('update')
+ }
+}
+
+// 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)
+}
diff --git a/renderer/webtorrent.js b/renderer/webtorrent.js
index 48a769bb..26338ac2 100644
--- a/renderer/webtorrent.js
+++ b/renderer/webtorrent.js
@@ -95,8 +95,9 @@ function stopTorrenting (infoHash) {
// Create a new torrent, start seeding
function createTorrent (torrentKey, options) {
- console.log('creating torrent %s', torrentKey, options)
- var torrent = client.seed(options.files, options)
+ console.log('creating torrent', torrentKey, options)
+ var paths = options.files.map((f) => f.path)
+ var torrent = client.seed(paths, options)
torrent.key = torrentKey
addTorrentEvents(torrent)
ipc.send('wt-new-torrent')