如何完全停止Node下载文件

3

我正在尝试作为学习的一部分,在React和Node中完成一个业余项目。在这个项目中,我试图将http和https链接转换为Torrent。我遇到的问题是当我提交一个链接时,它会将完整的文件下载到系统中,然后再进行转换。如何避免这种情况。以下是我的代码:

最初的回答:

var http = require('http');
var webtorrentify = require('webtorrentify-link');
var fs = require('fs');
var url = require('url');
var path = require('path');
var validUrl = require('valid-url');
var express = require('express');
var getUrls = require('get-urls');
var remote = require('remote-file-size');
var app = express();

var downloadLink;
var fileName;
var fileSize;
var server;
var parsed;
var param;
var link;
var port;
port = process.env.PORT || 80;

app.get('/favicon.ico', function (req, res) {
  console.log('favicon request recived');
});
app.get('*', function (req, res) {
  if (req.url === '/') {
    // app.use('/public/html', express.static(path.join(__dirname)));
    fs.readFile('public/html/index.html', function (err, data) {
      res.write(data);
    });
  } else if (req.url === '/l?thelink=') {
    fs.readFile('public/html/emptyRequest.html', function (err, data) {
      res.write(data);
      res.end();
    });
  } else {
    // ---------Reciving Url--------------------
    console.log(req.query.thelink);
    downloadLink = req.query.thelink;
    // -----------------------------------------

    // ------------checking for valid url-------
    if (validUrl.isUri(downloadLink)) {
      console.log('Looks like an URI');
      // -----------------------------------------

      // ----------Extracting filename-------------
      parsed = url.parse(downloadLink);
      fileName = path.basename(parsed.pathname);
      console.log(path.basename(parsed.pathname));
      // -------------------------------------------

      // ----------Finding File size----------------
      remote(downloadLink, function (err, o) {
        fileSize = (o / 1024) / 1024;
        console.log('size of ' + fileName + ' = ' + fileSize + ' MB');
        // -------------------------------------------
        if (fileSize < 501) {
          /// ////////////Creating Torrent////////////////////
          webtorrentify(downloadLink)
            .then(function (buffer) {
              console.log('creating the torrent');
              // res.send('what is');
              // -------------------------------------------
              res.setHeader('Content-Type', 'application/x-bittorrent');
              res.setHeader('Content-Disposition', `inline; filename="${fileName}.torrent"`);
              res.setHeader('Cache-Control', 'public, max-age=2592000'); // 30 days
              res.send(buffer);
              console.log(fileName + '.torrent created');
              res.end();
              // -------------------------------------------
            });
          /// /////////////////////////////////////////////
        } else {
          console.log('More than 500 MB');
          res.send('<h4> More than 500 MB or invalid URL </h4>');
        }
      });
    } else {
      console.log('not url');
      fs.readFile('public/html/404.html', function (err, data) {
        res.write(data);
        res.end();
      });
    }
  }
});

app.listen(port);

console.log('server up and running', port);
1个回答

1
这种行为在 webtorrentify-link 的文档 中有所记录,属于正常现象:

该模块将接收下载 URL 并将其转换为 .torrent 文件。警告:此过程需要下载 URL。

从技术上讲,没有任何模块或代码能够在不下载的情况下生成 torrent。这是因为 torrent 文件结构 要求每个文件块的哈希值,只有完整的文件才能获得和计算。

谢谢回复。有没有办法显示文件的下载进度,然后开始制作种子? - Savad
你可以寻找能够下载和流式传输文件输出并带有进度的代码片段,然后使用 create-torrent 生成种子文件,基于此可以使用 webtorrentify-link。 - Eric Wong
当我尝试转换较大的文件(例如1GB文件)时,应用程序会崩溃...请问您如何解决这个问题? - Savad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接