1.1.0 • Published 1 year ago

@jb_fmanager/node-utils v1.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago
npm i @jb_fmanager/node-utils

yarn add @jb_fmanager/node-utils

get map

Maps the provided path, returns a parent / child tree structure

const { map } = require("@jb_fmanager/node-utils");

app.get("/api/fm/map", async (request, response) => {
  try {
    // expects root folder argument, the directory in the filesystem that you want to map
    // result is the formatted file tree
    // throws error

    const result = await map(root);

    // response has to include result as json
  } catch (e) {
    console.error(e);
  }
});

get create_folder

Creates new directory in the specified path

const { create_folder } = require("@jb_fmanager/node-utils");

app.get("/api/fm/create_folder", async (request, response) => {
  try {
    // expects path and name arguments from the query
    // void
    // throws error

    await create_folder(request.query.path, request.query.name);

    // any response
  } catch (e) {
    console.error(e);
  }
});

get rename

Renames a file

const { rename } = require("@jb_fmanager/node-utils");

app.get("/api/fm/rename", async (request, response) => {
  try {
    // expects oldPath and newPath arguments from the query
    // void
    // throws error

    await rename(request.query.oldPath, request.query.newPath);

    // any response
  } catch (e) {
    console.error(e);
  }
});

post remove

Removes a number of files recursively

const { remove } = require("@jb_fmanager/node-utils");

app.post("/api/fm/remove", async (request, response) => {
  try {
    // expects the request body
    // void
    // throws error

    await remove(request.body);

    // any response
  } catch (e) {
    console.error(e);
  }
});

post copy

Copies a number of files from one path to another

const { copy } = require("@jb_fmanager/node-utils");

app.post("/api/fm/copy", async (request, response) => {
  try {
    // expects target argument from query and request body
    // void
    // throws error

    await copy(request.query.target, request.body);

    // any response
  } catch (e) {
    console.error(e);
  }
});

post move

Moves a number of files from one path to another

const { move } = require("@jb_fmanager/node-utils");

app.post("/api/fm/move", async (request, response) => {
  try {
    // expects target argument from query and request body
    // void
    // throws error

    await move(request.query.target, request.body);

    // any response
  } catch (e) {
    console.error(e);
  }
});

post upload

Saves a number of files into the given destination

const { upload } = require("@jb_fmanager/node-utils");

app.post("/api/fm/upload", async (request, response) => {
  try {
    // expects request, response, destination and max_size (optional) arguments
    // result is an object with two array properties, one containing succesful and the other containing failed uploads
    // throws error

    const result = await upload(
      request,
      response,
      request.query.destination,
      request.query.max_size
    );

    // response should include the result in json
  } catch (e) {
    console.error(e);
  }
});
// Next.js - blocking default parser on "api/fm/upload" route example

export const config = {
  api: {
    bodyParser: false,
  },
};

// process upload
// fastify - adding a custom multipart/form-data parser which will pass the request onwards

const fastify = require("fastify")({});

fastify.addContentTypeParser(
  "multipart/form-data",
  function (request, payload, done) {
    done(null, payload);
  }
);

// then inside your route

upload(request.raw, response.raw, ...)

// in fastify the instance of IncomingMessage can be found under request.raw,
1.1.0

1 year ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago