Skip to content

Instantly share code, notes, and snippets.

@smhatre59
Last active May 4, 2017 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smhatre59/0ed7d4a79f6de343881205da2dd74919 to your computer and use it in GitHub Desktop.
Save smhatre59/0ed7d4a79f6de343881205da2dd74919 to your computer and use it in GitHub Desktop.
Nodejs script to upload files to cloudinary cdn
/*
Purpose:Upload files to cloudinary cdn from nodejs
Tutorial:http://wp.me/p82htC-eX
*/
const express = require('express');
let app = express();
const chokidar = require('chokidar');
const filepath = '/home/saurabh/Pictures/Apps/';
let watcher = chokidar.watch(filepath, {
ignored: /[\/\\]\./, persistent: true
});
let log = console.log.bind(console);
let scanComplete = false;
const cloudinary = require('cloudinary');
cloudinary.config({
cloud_name: 'cloudname',
api_key: 'api_key',
api_secret: 'api_secret'
});
watcher
.on('add', function (path) {
if (scanComplete) {
let pathArray = path.split('/');
if (!pathArray[pathArray.length - 1].includes("crdownload")) {
log('File', path, 'has been added');
// console.log(pathArray.length, pathArray[pathArray.length - 2]);
let destfolder = pathArray[pathArray.length - 2];
let destfileName = pathArray[pathArray.length - 1];
cloudinary.v2.uploader.upload(path, {
folder: destfolder,
use_filename:true,
tags:[destfolder]
}, function (error, result) {
if (error) {
console.log("error ocurred", error);
}
else {
console.log("result of upload \n", result.secure_url,"\n insecure url: \n",result.url);
}
});
}
}
})
.on('addDir', function (path) {
// log('Directory', path, 'has been added');
})
.on('error', function (error) { log('Error happened', error); })
.on('ready', function () {
log('Initial scan complete. Ready for changes.');
scanComplete = true;
})
.on('raw', function (event, path, details) {
// log('Raw event info:', event, path, details);
})
let port = process.env.PORT || 5000;
app.listen(port);
console.log("serve listening on port", port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment