Get file detail

On this page, we will show how to get file detail for both encrypted and unencrypted files.

To do that, we need 3 methods:

Example

import {
  getDetailFile,
  getDetailEncryptedFile,
  getDetailUnencryptedFile
} from "@eueno/lib-browser";
  
  const getFile = async (id, token) => {
    if (!id || !token) return;
    const fileDetail = await getDetailFile(id);
    if (!fileDetail) {
      console.error("File not found");
      return;
    }

    let bufferData;
    if (fileDetail.encryptKey) {
      bufferData = await getDetailEncryptedFile(
        fileDetail.torrentUrl,
        fileDetail.encryptKey,
        token,
        isShare ? true : false,
        [fileDetail.url, fileDetail.backupUrl],
        account,
        (torrent, remaining) => {
          dispatch(
            setTorrentInfo({
              download: torrent.downloadSpeed,
              upload: torrent.uploadSpeed,
              remain: remaining,
            })
          );
        }
      );
    } else {
      bufferData = await getDetailUnencryptedFile(
        token,
        fileDetail.torrentUrl,
        (torrent, remaining) => {
          dispatch(
            setTorrentInfo({
              download: torrent.downloadSpeed,
              upload: torrent.uploadSpeed,
              remain: remaining,
              peers: torrent.numPeers,
              progress: torrent.progress,
            })
          );
        }
      );
    }
    
    if (!bufferData) {
      console.error("Decrypt failed !");
      return;
    }

    //Render buffer data
    const output = document.getElementById("output");
    const filePreview = {
      name: fileDetail.name,
      createReadStream: function (opts) {
        if (!opts) opts = {};
        return From([
          bufferData?.slice(opts.start || 0, opts.end || bufferData.length),
        ]);
      },
    };
    if (output) {
      render.append(filePreview, output, function (err, elem) {
        if (err) {
          console.log(err);
          setMessage("This file preview is not supported!");
        }
      });
    }
  };

Last updated