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

multer-storage-cloudinary

v4.0.0

Published

A Cloudinary multer storage engine

Downloads

40,618

Readme

Multer Storage Cloudinary

A multer storage engine for Cloudinary. Also consult the Cloudinary API.

Installation

npm install multer-storage-cloudinary

Usage

const cloudinary = require('cloudinary').v2;
const { CloudinaryStorage } = require('multer-storage-cloudinary');
const express = require('express');
const multer = require('multer');

const app = express();

const storage = new CloudinaryStorage({
  cloudinary: cloudinary,
  params: {
    folder: 'some-folder-name',
    format: async (req, file) => 'png', // supports promises as well
    public_id: (req, file) => 'computed-filename-using-request',
  },
});

const parser = multer({ storage: storage });

app.post('/upload', parser.single('image'), function (req, res) {
  res.json(req.file);
});

File properties

File objects will expose the following properties mapped from the Cloudinary API:

| Key | Description | | ---------- | ----------------------------------- | | filename | public_id of the file on cloudinary | | path | A URL for fetching the file | | size | Size of the file in bytes |

Options

Storage can be configured using the options argument passed to the CloudinaryStorage constructor.

const { CloudinaryStorage } = require('multer-storage-cloudinary');

const storage = new CloudinaryStorage({
  cloudinary: cloudinary,
  params: {
    // upload paramters
  },
});

All parameters are optional except the configured Cloudinary API object:

| Parameter | Description | Type | | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------- | | options.cloudinary | A Cloudinary API object The API must be configured by the user | object required | | options.params | An object or a function that resolves to an object which can contain any/all properties described in the Cloudinary upload API docs. Read below for more information | object or function |

Each property in the params object (either directly or resolved from the function) can either be a static value or an async function that resolves to the required value. All upload parameters specified in the Cloudinary docs are supported.

Note: public_id is different in that it must always be a functional parameter

Functional parameters are called on every request and can be used in the following way:

const cloudinary = require('cloudinary').v2;
const { CloudinaryStorage } = require('multer-storage-cloudinary');

const storage = new CloudinaryStorage({
  cloudinary: cloudinary,
  params: {
    folder: (req, file) => 'folder_name',
    format: async (req, file) => {
      // async code using `req` and `file`
      // ...
      return 'jpeg';
    },
    public_id: (req, file) => 'some_unique_id',
  },
});

You can also provide all params using a single function

const cloudinary = require('cloudinary').v2;
const { CloudinaryStorage } = require('multer-storage-cloudinary');

const storage = new CloudinaryStorage({
  cloudinary: cloudinary,
  params: async (req, file) => {
    // async code using `req` and `file`
    // ...
    return {
      folder: 'folder_name',
      format: 'jpeg',
      public_id: 'some_unique_id',
    };
  },
});

Typescript

This library is written is typescript and so provides all types necessary for use in a typescript project.

Testing

The Cloudinary API must be configured using the CLOUDINARY_URL environment variable in order to run the tests. All test files are stored in a seperate Cloudinary folder, which is deleted after tests finish.

npm test