putdotio / putio-js

JavaScript SDK for interacting with the put.io API.
MIT License
21 stars 2 forks source link

upload method doesn't work without filename parameter #22

Closed kaktus40 closed 1 year ago

kaktus40 commented 3 years ago

Hello, following PR15 and this filename should be optional for Files.Upload function. If I omit this parameter, I have an issue with FormData. When I configure the filename parameter with anything, axios indicate error_message: 'getaddrinfo ENOTFOUND upload.put.io' Any help?

kaktus40 commented 3 years ago

It seems that upload.put.io is unreachable...

kaktus40 commented 3 years ago

OK I changed my DNS server and upload.put.io is reachable. Now axios returns this error: 'Cannot parse form file: "file"\n'

kaktus40 commented 3 years ago

These pieces of code worked for me:

import { uploadTorrent } from "../components/put";
import { ensureDirSync, removeSync } from "fs-extra";
import { resolve } from "path";

let tempPath = resolve(__dirname, "tempDownload");
ensureDirSync(tempPath);

export async function post(req, res) {
  const files = req.files;
  for (let item in files) {
    const fileUploaded = files[item];
    const chemin = resolve(tempPath, fileUploaded.name);
    await fileUploaded.mv(chemin);
    let temp = await uploadTorrent(chemin, fileUploaded.size);
    removeSync(chemin);
  }
  return res.end(JSON.stringify({ status: "OK" }));
}
export async function uploadTorrent(
  file: string,
  size: number
): Promise<number> {
  const form = new formData();
  form.append("file", createReadStream(file), { knownLength: size });
  form.append("parent_id", mainFolderId + "");
  form.append("filename", "");
  const formHeaders = form.getHeaders();
  const headers = {
    ...formHeaders,
    "Content-Length": form.getLengthSync(),
    "Content-Type": "multipart/form-data",
    accept: "application/json",
    Authorization: `Bearer ${putToken}`,
  };

  const temp = await axios({
    url: "https://upload.put.io/v2/files/upload",
    method: "POST",
    data: form,
    headers,
  });

  return temp.status;
}

thanks to mcdado

BatuAksoy commented 3 years ago

Hey @kaktus40 I will do necessary inspection and then make sure that method works as it should.

Thank you for the report & explanation.

altaywtf commented 1 year ago

should be fixed in v8.35.1, sorry for the late response...