feat(downloading): ability to choose files which you wanna download
This commit is contained in:
@ -57,6 +57,7 @@ module.exports = async ({
|
||||
removeOnDone: download.removeOnDone,
|
||||
paused: torrent.paused || torrent._paused
|
||||
}
|
||||
torrent.filesList = download.files.length > 0 ? downloadFilesList(download) : torrent.filesList
|
||||
}
|
||||
}
|
||||
|
||||
@ -81,6 +82,14 @@ module.exports = async ({
|
||||
Fn(...rest, (data) => callback(mergeTorrentsWithDownloads(data, copy)))
|
||||
}
|
||||
|
||||
const downloadFilesList = (torrent) => torrent.files.map((file, index) => ({
|
||||
path: file.path.replace(/\\/g, '/'),
|
||||
size: file.length,
|
||||
downloadIndex: index,
|
||||
downloadSelected: file.selected
|
||||
}))
|
||||
|
||||
|
||||
recive('recentTorrents', function(callback)
|
||||
{
|
||||
if(typeof callback != 'function')
|
||||
@ -692,6 +701,7 @@ module.exports = async ({
|
||||
delete torrent._paused
|
||||
torrent._pause()
|
||||
}
|
||||
send('filesReady', torrent.infoHash, downloadFilesList(torrent))
|
||||
})
|
||||
|
||||
torrent.on('done', () => {
|
||||
@ -762,6 +772,49 @@ module.exports = async ({
|
||||
return _destroy.call(torrent, ...args)
|
||||
}
|
||||
|
||||
torrent.selectFiles = (selection) => {
|
||||
if(Array.isArray(selection))
|
||||
{
|
||||
if(selection.length !== torrent.files.length)
|
||||
{
|
||||
logTE('downloader', 'selection map not full', torrent.files.length, selection.length)
|
||||
return
|
||||
}
|
||||
for(let i = 0; i < selection.length; i++)
|
||||
{
|
||||
torrent.files[i].selected = !!selection[i]
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(let fileId in selection)
|
||||
{
|
||||
fileId = parseInt(fileId)
|
||||
if(fileId >= torrent.files.length)
|
||||
{
|
||||
logTE('downloader', 'selection map wrong', selection)
|
||||
return
|
||||
}
|
||||
torrent.files[fileId].selected = !!selection[fileId]
|
||||
}
|
||||
}
|
||||
torrent.updateFilesSelection()
|
||||
}
|
||||
|
||||
torrent.updateFilesSelection = () => {
|
||||
torrent.deselect(0, torrent.pieces.length - 1, false)
|
||||
|
||||
for(const file of torrent.files)
|
||||
{
|
||||
const {selected} = file
|
||||
if(typeof selected === 'undefined' || selected)
|
||||
file.select()
|
||||
else
|
||||
file.deselect()
|
||||
}
|
||||
logT('downloader', 'selection updated')
|
||||
}
|
||||
|
||||
|
||||
if(callback)
|
||||
callback(true)
|
||||
@ -834,6 +887,31 @@ module.exports = async ({
|
||||
})
|
||||
})
|
||||
|
||||
recive('downloadSelectFiles', ({hash}, files, callback) =>
|
||||
{
|
||||
logT('downloader', 'call update selection', hash, files.length)
|
||||
const id = torrentClientHashMap[hash]
|
||||
if(!id)
|
||||
{
|
||||
logT('downloader', 'cant find torrent for selection', hash)
|
||||
if(callback)
|
||||
callback(false)
|
||||
return
|
||||
}
|
||||
|
||||
const torrent = torrentClient.get(id)
|
||||
if(!torrent) {
|
||||
logT('downloader', 'no torrent for selection founded')
|
||||
return
|
||||
}
|
||||
|
||||
torrent.selectFiles(files)
|
||||
send('filesReady', torrent.infoHash, downloadFilesList(torrent))
|
||||
|
||||
if(callback)
|
||||
callback(true)
|
||||
})
|
||||
|
||||
recive('downloads', (callback) =>
|
||||
{
|
||||
callback(torrentClient.torrents.map(torrent => ({
|
||||
|
@ -10,7 +10,8 @@ torrentClient.saveSession = (sessionFile) => {
|
||||
torrent: torrent.torrentObject,
|
||||
|
||||
removeOnDone: torrent.removeOnDone,
|
||||
paused: torrent.paused || torrent._paused
|
||||
paused: torrent.paused || torrent._paused,
|
||||
selection: torrent.files.map(file => typeof file.selected === 'undefined' || file.selected)
|
||||
}))
|
||||
}, null, 4), 'utf8');
|
||||
}
|
||||
@ -34,7 +35,7 @@ torrentClient.loadSession = (sessionFile) => {
|
||||
return
|
||||
}
|
||||
const {torrents} = obj
|
||||
torrents.forEach(({torrent, infoHash, path, removeOnDone, paused}) => {
|
||||
torrents.forEach(({torrent, infoHash, path, removeOnDone, paused, selection}) => {
|
||||
if(!torrent || !infoHash || !path)
|
||||
{
|
||||
logT('downloader', 'no info for starting download this torrent')
|
||||
@ -51,6 +52,13 @@ torrentClient.loadSession = (sessionFile) => {
|
||||
{
|
||||
download._paused = true
|
||||
}
|
||||
if(selection)
|
||||
{
|
||||
download.on('metadata', () => {
|
||||
logT('downloader', 'load torrent selection from session')
|
||||
download.selectFiles(selection)
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user