qgustavor / mega

Unofficial JavaScript SDK for MEGA
https://mega.js.org/
MIT License
161 stars 43 forks source link

Upload to specific folder example #104

Closed 0-don closed 2 years ago

0-don commented 2 years ago

I would like to know how to upload a file to specific folder?

  const storage = await new Storage({
    email: process.env.MEGA_EMAIL,
    password: process.env.MEGA_PASSWORD,
  }).ready;

  let folder = storage.root.children?.find(
    (e) => e.name === process.env.MEGA_DIR
  );

  if (!folder) folder = await storage.mkdir({ name: process.env.MEGA_DIR });

  const file = await storage.upload(fileName, jsonFile)
    .complete;

  await file.moveTo(folder);

  const link = await file.link({ noKey: false });
  return link;

currently I am doing it like this but I wanted to ask if there is an easier way?

it would be nice to do something like this so it parses the string for the filename and creates the missing folders if they dont exist.

const file = await storage.upload("./folder/folder1/" + fileName, jsonFile)