module.exports = CreateTorrentPage var createTorrent = require('create-torrent') var path = require('path') var prettyBytes = require('prettier-bytes') var {dispatch, dispatcher} = require('../lib/dispatcher') var hx = require('../lib/hx') function CreateTorrentPage (state) { var info = state.location.current() // Preprocess: exclude .DS_Store and other dotfiles var files = info.files .filter((f) => !f.name.startsWith('.')) .map((f) => ({name: f.name, path: f.path, size: f.size})) if (files.length === 0) return CreateTorrentErrorPage() // First, extract the base folder that the files are all in var pathPrefix = info.folderPath if (!pathPrefix) { pathPrefix = files.map((x) => x.path).reduce(findCommonPrefix) if (!pathPrefix.endsWith('/') && !pathPrefix.endsWith('\\')) { pathPrefix = path.dirname(pathPrefix) } } // Sanity check: show the number of files and total size var numFiles = files.length var totalBytes = files .map((f) => f.size) .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, basePath if (files.length === 1) { // Single file torrent: /a/b/foo.jpg -> torrent name "foo.jpg", path "/a/b" defaultName = files[0].name basePath = pathPrefix } else { // Multi file torrent: /a/b/{foo, bar}.jpg -> torrent name "b", path "/a" defaultName = path.basename(pathPrefix) 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`
${torrentInfo}
Sorry, you must select at least one file that is not a hidden file.
Hidden files, starting with a . character, are not included.