我尝试用electronic和webtorrent编写一个小的torrent客户端。乍一看一切都很好,但是有时当torrent下载完成时,结果文件并未写入磁盘。
我的项目是通过SimulatedGREG / electron-vue样板设置的。
这是我的洪流客户端类的代码:
const WebTorrent = require('webtorrent');
const client = new WebTorrent();
export default class TorrentClient {
download (downloadInfo) {
console.log('download torrent from magnet link:', downloadInfo.magnetLink);
let torrent = client.add(downloadInfo.infoHash);
torrent.on('download', function (bytes) {
console.log('just downloaded: ' + bytes);
console.log('total downloaded: ' + torrent.downloaded);
console.log('download speed: ' + torrent.downloadSpeed);
console.log('progress: ' + torrent.progress);
});
torrent.on('done', function () {
console.log('done...');
});
}
}
更新:我解决了问题。
调试了webtorrent使用的fs-chunk-store后,我发现在将数据写入磁盘之前就抛出了错误。当调用fs.mkdir为下载目标创建新路径时发生错误(ENOENT:无此类文件或目录,mkdir'path / to / folder')。没有回调函数作为参数,该错误将不会输出到stdout。
我现在的解决方案是使用fs-chunk-store的自定义实现,该实现允许递归创建文件夹。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句