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-direct

v1.0.4

Published

A cloudinary direct call API utility which can help you upload images , videos and audio file .

Downloads

6

Readme

Cloudirect - cloudinary direct caller for uploading files .

npm npm Build Status

What is this thing ?

A cloudinary direct call API utility which can help you upload images , videos and audio file . More options will be added in the future along with support and examples for multiple frameworks .

The package was created to simplify the process of uploading file to cloudinary. You can upload image, video / .mp4 and audio / .mp3 files via this .

This package can even work with multiple platforms . You can use Express js in the backend and use cloudinary-direct to upload packages , requires Multer middleware . You can use React-Dropzone in the frontend .

Install

Installing this package is very easy , it has very few dependency .

Install with yarn .

yarn add cloudinary-direct

Install with npm .

npm i cloudinary-direct --save

How to use ?

It has four properties for authentication . cloudName Which you can find in your cloudinary console / dashboard api_key Which you can find in your cloudinary console / dashboard . For uploading file . api_secret Which you can also find in your cloudinary console / dashboard . For uploading file . upload_preset Which you can find in your cloudinary settings .

Example for auth

const Cloud = require("cloudinary-direct");
Cloud.config({
  cloudName: "your_cloudname",
  api_key: "your_api_key",
  api_secret: "your_api_secret"
})

Functions

Here are the functions that will provide you the power to upload files of different types.

// file[0] was retrieved from React DropZone , req.files fro express.js
// For image uploading
Cloud.imageUploader(file[0], (resp)=> {
  const imageURL = resp.secure_url;
  // Save that url in the database
});

// For audio / .mp3 file uploading
Cloud.audioUploader(file[0], (resp)=> {
  const audioURL = resp.secure_url;
  // Save that url in the database
})

// For video / .mp4 file uploading
Cloud.videoUploader(file[0], (resp)=> {
  const videoURL = resp.secure_url;
  // Save that url in the database
})

You can choose which file to upload . This package will upload your file but it can't handle http/multipart . So you need to use React-Dropzone for react . The examples are shown in them . There is also a HOC -> High Order Component , you can use that as well .

Image Uploading :

Express js and multer:

Here is an example with express js and multer . Multer is the mostly used for uploading files and handling all the http/multipart . Here is an example express and multer with cloudinary direct :

const express = require("express");
const multer = require("multer");
const Cloud = require("cloudinary-direct");

const app = express();

const storage = multer.diskStorage({
  destination: './userUploads/',
  filename: function(req, file, cb){
    cb(null,file.fieldname + '-' + Date.now() + path.extname(file.originalname));
  }
});

// Init Upload
const upload = multer({
  storage: storage,
  limits:{fileSize: 1000000},
}).single('userImage');

app.post('/photo/upload', function(req, res){
  upload(req, res, function(){
    Cloud.config({
      cloudName: "your_cloudname",
      api_key: "your_api_key",
      api_secret: "your_api_secret",
      upload_preset: "your_upload_preset"
    });
    Cloud.express.uploadViaMulter(req, function(result){
      console.log(result.url);
    })
  })
})

React js with react dropzone

Here is an example with React . Cloudniary direct can be used both in frontend frameworks and backend frameworks . I am using react dropzone for handling upload .

Note that upload_preset is needed for frontend usage .

import React from 'react';
import Cloud from 'cloudniary-direct';
import DropZone from 'react-dropzone';

class Sender extends React.Component{
  constructor(){
    super();
    this.handler = this.handler.bind(this);
  }
  handler(file){
    Cloud.config({
      cloudName: "your_cloudname",
      api_key: "your_api_key",
      api_secret: "your_api_secret",
      upload_preset: "your_upload_preset"
    })
    Cloud.imageUploader(file[0], (resp)=> {
      const imageURL = resp.secure_url;
      // Save that url in the database
    })
  }
  render(){
    return (
      <div>
        <DropZone onDrop={this.handler} />
      </div>
    )
  }
}

More support will come in the future . I will try to implement features like async await for more faster and better response .