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() // 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})) // First, extract the base folder that the files are all in 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 console.log('FILES', files) 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 = files.length === 1 ? files[0].name : 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`
${torrentInfo}