npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cloudinary-multer

v1.0.2

Published

A custom multer store for uploading assets to cloudinary.

Downloads

73

Readme

Cloudinary-Multer

cloudinary-multer

A custom multer storage engine to upload assets to cloudinary.

Installation

cloudinary-multer can be installed easily via npm

npm install cloudinary-multer

Usage

Cloudinary Configuration

const cloudinary = require("cloudinary").v2;

cloudinary.config({
  cloud_name: "XXXXXXXXX",
  api_key: "XXXXXXXXX",
  api_secret: "XXXXXXXXX",
});

Storage Engine

const cloudinaryStorage = require("cloudinary-multer");

const storage = cloudinaryStorage({
  cloudinary: cloudinary,
});

const upload = multer({
  storage: storage,
});

Options

Custom Storage Engine has 4 options namely:-

  1. cloudinary (required)

The cloudinary library instance to be used for uploading assets. Configuration options can be found here

Example:-

cloudinary.config({
  cloud_name: "XXXXXXXXX",
  api_key: "XXXXXXXXX",
  api_secret: "XXXXXXXXX",
});
  1. validator (optional)

Since req.body is not available prior to the multer middleware, it seemed to be a good idea to include a validator to validate the request body. validator is supposed to be function that would return false for valid body and an error message/true otherwise.

The order of fields in req.body is very important. Make sure that the field of type "file" must be last. All other fields that need to be validated should be on top.

Example:-

const validator = (body) => {
  /**
   * req.body would be passed as a parameter(body) here
   * Order of req.body is very important for validating.
   * Make sure that file is the LAST FIELD in req.body.
   * example:-
        req.body: {
            email: "[email protected]",
            username: "test username"
            file: {FILE}
        }
   */
  const schema = Joi.object({
    email: Joi.string().email().required(),
    username: Joi.string().max(40).required(),
  });
  const { error } = schema.validate(body);
  if (error) return error;
  return false;
};
  1. uploadOptions (optional)

These are the options that needed to be applied to upload methord in cloudinary. Know that the methord used under the hood is upload_stream. More information can be found here and here.

  1. destroyOptions (optional)

These are the options that needed to be applied to upload methord in cloudinary. More information can be found here.

Usage in Route

app.post("/upload", upload.single("file"), async (req, res) => {});

A simple complete example of the same could be found here.

Working

cloudinary-multer a simple custom storage engine to directly upload assets to cloudinary. _handleFile function in multer storage engine gives the file data in form of a readable stream (file.stream). This file stream is then uploaded directly to cloudinary if the validation is successfull. Uploading the stream directly is a great advantage as the file is not stored in your disk/server unlike traditional diskStorage(stores file in your disk) and memoryStorage(stores file as buffer in server memory).

Contributing

Feel free to report a bug, suggest a change etc. You are welcome to raise an issue and/or create a pull request. Before making any major upgrade, kindly raise an issue to discuss further. Make sure to run npm run format and npm run lint before commiting your changes/generating a PR.

Read Further

License

MIT