Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Sample Projects #698

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
256 changes: 161 additions & 95 deletions samples/basic/basic.js
Original file line number Diff line number Diff line change
@@ -1,121 +1,176 @@
require('dotenv').load();
var fs = require('fs');
var cloudinary = require('cloudinary').v2;
require('dotenv').config();
const fs = require('fs');
const cloudinary = require('cloudinary').v2;

var uploads = {};
let uploads = {};

// set your env variable CLOUDINARY_URL or set the following configuration
/* cloudinary.config({
cloud_name: '',
api_key: '',
api_secret: ''
}); */
/*
Set your environment variable CLOUDINARY_URL or set the following configuration
cloudinary.config({
cloud_name: '',
api_key: '',
api_secret: ''
});
*/

console.log("** ** ** ** ** ** ** ** ** Uploads ** ** ** ** ** ** ** ** ** **");

// File upload
cloudinary.uploader.upload('pizza.jpg', { tags: 'basic_sample' }, function (err, image) {
console.log();
console.log("** File Upload");
if (err) { console.warn(err); }
console.log("* public_id for the uploaded image is generated by Cloudinary's service.");
console.log("* " + image.public_id);
console.log("* " + image.url);
waitForAllUploads("pizza", err, image);
});
cloudinary.uploader.upload('pizza.jpg', { tags: 'basic_sample' })
.then((image) => {
console.log();
console.log("** File Upload");
console.log("* public_id for the uploaded image is generated by Cloudinary's service.");
console.log(`* ${image.public_id}`);
console.log(`* ${image.url}`);
waitForAllUploads("pizza", image);
})
.catch((err) => {
console.warn(err);
});


// Stream upload
var upload_stream = cloudinary.uploader.upload_stream({ tags: 'basic_sample' }, function (err, image) {
console.log();
console.log("** Stream Upload");
if (err) { console.warn(err); }
console.log("* Same image, uploaded via stream");
console.log("* " + image.public_id);
console.log("* " + image.url);
waitForAllUploads("pizza3", err, image);
});
fs.createReadStream('pizza.jpg').pipe(upload_stream);
async function uploadStreamWithPromise(filePath, uploadOptions) {
try {
const byteArrayBuffer = fs.readFileSync(filePath);

const uploadResult = await new Promise((resolve, reject) => {
const stream = cloudinary.uploader.upload_stream(uploadOptions, (err, result) => {
if (err) {
return reject(err);
}
resolve(result);
});
stream.end(byteArrayBuffer);
});

console.log();
console.log("** Stream Upload");
console.log(`* public_id for the uploaded image is: ${uploadResult.public_id}`);
console.log(`* URL: ${uploadResult.url}`);

waitForAllUploads("pizza3", uploadResult);

} catch (error) {
console.error("Error during stream upload: ", error);
}
}

uploadStreamWithPromise('pizza.jpg', { tags: 'basic_sample' });

// File upload (example for promise api)
cloudinary.uploader.upload('pizza.jpg', { tags: 'basic_sample' })
.then(function (image) {


// File upload (example for async/await)
(async () => {
try {
const image = await cloudinary.uploader.upload('pizza.jpg', { tags: 'basic_sample' });
console.log();
console.log("** File Upload (Promise)");
console.log("** File Upload (Async/Await)");
console.log("* public_id for the uploaded image is generated by Cloudinary's service.");
console.log("* " + image.public_id);
console.log("* " + image.url);
})
.catch(function (err) {
console.log(`* ${image.public_id}`);
console.log(`* ${image.url}`);
} catch (err) {
console.error("Error in File Upload (Async/Await): ", err);
}
})();


// Files can also be uploaded with a specified Public id
cloudinary.uploader.upload('pizza.jpg', { tags: 'basic_sample', public_id: 'my_favorite_pizza' })
.then((image) => {
console.log();
console.log("** File Upload (Promise)");
if (err) { console.warn(err); }
console.log("** Public Id");
console.log("* Same image, uploaded with a custom public_id");
console.log(`* ${image.public_id}`);
console.log(`* ${image.url}`);
waitForAllUploads("pizza2", image);
})
.catch((err) => {
console.warn(err);
});


// Public Id
cloudinary.uploader.upload('pizza.jpg', { tags: 'basic_sample', public_id: 'my_favorite_pizza' }, function (err, image) {
console.log();
console.log("** Public Id");
if (err) { console.warn(err); }
console.log("* Same image, uploaded with a custom public_id");
console.log("* " + image.public_id);
console.log("* " + image.url);
waitForAllUploads("pizza2", err, image);
});


// Eager Transformations:
// Applied as soon as the file is uploaded, instead of lazily applying them when accessed by your site's visitors.
var eager_options = {
/* Eager Transformations:
Applied as soon as the file is uploaded, instead of lazily applying them when accessed by your site's visitors.
*/
const eager_options = {
width: 200, height: 150, crop: 'scale', format: 'jpg'
};
cloudinary.uploader.upload("lake.jpg", { tags: "basic_sample", public_id: "blue_lake", eager: eager_options }, function (err, image) {
// "eager" parameter accepts a hash (or just a single item). You can pass
// named transformations or transformation parameters as we do here.
console.log();
console.log("** Eager Transformations");
if (err) { console.warn(err); }
console.log("* " + image.public_id);
console.log("* " + image.eager[0].url);
waitForAllUploads("lake", err, image);
});


// Remote URL:
// In the two following examples, the file is fetched from a remote URL and stored in Cloudinary.
// This allows you to apply transformations and take advantage of Cloudinary's CDN layer.
cloudinary.uploader.upload('http://res.cloudinary.com/demo/image/upload/couple.jpg', { tags: "basic_sample" }, function (err, image) {
console.log();
console.log("** Remote Url");
if (err) { console.warn(err); }
console.log("* " + image.public_id);
console.log("* " + image.url);
waitForAllUploads("couple", err, image);
});


// Here, the transformation is applied to the uploaded image BEFORE storing it on the cloud.
// The original uploaded image is discarded.
cloudinary.uploader.upload('http://res.cloudinary.com/demo/image/upload/couple.jpg',
{ "tags": "basic_sample", "width": 500, "height": 500, "crop": "fit", "effect": "saturation:-70" },
function (err, image) {

cloudinary.uploader.upload("lake.jpg", { tags: "basic_sample", public_id: "blue_lake", eager: eager_options })
/*
"eager" parameter accepts a hash (or just a single item). You can pass
named transformations or transformation parameters as we do here.
*/
.then((image) => {

console.log();
console.log("** Eager Transformations");
console.log(`* ${image.public_id}`);
console.log(`* ${image.eager[0].url}`);
waitForAllUploads("lake", image);
})
.catch((err) => {
console.warn(err);
});


/*
Remote URL:
In the two following examples, the file is fetched from a remote URL and stored in Cloudinary.
This allows you to apply transformations and take advantage of Cloudinary's CDN layer.
*/
cloudinary.uploader.upload('http://res.cloudinary.com/demo/image/upload/couple.jpg', { tags: "basic_sample" })
.then((image) => {
console.log();
console.log("** Remote Url");
if (err) { console.warn(err); }
console.log("* " + image.public_id);
console.log("* " + image.url);
waitForAllUploads("couple2", err, image);
console.log(`* ${image.public_id}`);
console.log(`* ${image.url}`);
waitForAllUploads("couple", image);
})
.catch((err) => {
console.warn(err);
});


function waitForAllUploads(id, err, image) {
/*
Here, the transformation is applied to the uploaded image BEFORE storing it on the cloud.
The original uploaded image is discarded.
This is being done using async/await to demonstrate its functionality with Remote URLs
*/
(async () => {
try {
const image = await cloudinary.uploader.upload(
'http://res.cloudinary.com/demo/image/upload/couple.jpg',
{
tags: "basic_sample",
width: 500,
height: 500,
crop: "fit",
effect: "saturation:-70"
}
);

console.log();
console.log("** Remote Url using Async/Await");
console.log(`* ${image.public_id}`);
console.log(`* ${image.url}`);
waitForAllUploads("couple2", image);

} catch (err) {
console.warn(err);
}
})();



function waitForAllUploads(id, image) {
uploads[id] = image;
var ids = Object.keys(uploads);
let ids = Object.keys(uploads);
if (ids.length === 6) {
console.log();
console.log('** uploaded all files (' + ids.join(',') + ') to cloudinary');
console.log(`** uploaded all files (${ids.join(',')}) to cloudinary`);
performTransformations();
}
}
Expand All @@ -127,19 +182,30 @@ function performTransformations() {
console.log(">> >> >> >> >> >> >> >> >> >> Transformations << << << << << << << << << <<");
console.log();
console.log("> Fit into 200x150");
console.log("> " + cloudinary.url(uploads.pizza2.public_id, { width: 200, height: 150, crop: "fit", format: "jpg" }));
console.log(`> ${cloudinary.url(uploads.pizza2.public_id, { width: 200, height: 150, crop: "fit", format: "jpg" })}`);

console.log();
console.log("> Eager transformation of scaling to 200x150");
console.log("> " + cloudinary.url(uploads.lake.public_id, eager_options));
console.log(`> ${cloudinary.url(uploads.lake.public_id, eager_options)}`);

console.log();
console.log("> Face detection based 200x150 thumbnail");
console.log("> " + cloudinary.url(uploads.couple.public_id, { width: 200, height: 150, crop: "thumb", gravity: "faces", format: "jpg" }));
console.log(`> ${cloudinary.url(uploads.couple.public_id, { width: 200, height: 150, crop: "thumb", gravity: "faces", format: "jpg" })}`);

console.log();
console.log("> Fill 200x150, round corners, apply the sepia effect");
console.log("> " + cloudinary.url(uploads.couple2.public_id, { width: 200, height: 150, crop: "fill", gravity: "face", radius: 10, effect: "sepia", format: "jpg" }));
console.log(`> ${cloudinary.url(uploads.couple2.public_id, { width: 200, height: 150, crop: "fill", gravity: "face", radius: 10, effect: "sepia", format: "jpg" })}`);

console.log();
console.log("> Optimisation of image quality and file format to minimize file size and maintain required quality level");
console.log(`> ${cloudinary.url(uploads.lake.public_id, {transformation: [ {width: 500, crop: "scale"}, {quality: "auto", fetch_format: "auto"} ]})}`);

console.log();
console.log("> Returning images that fit the size and device pixel ratio(dpr) of a user's device");
console.log(`> ${cloudinary.url(uploads.lake.public_id, {transformation: [
{ dpr: "auto", responsive: true, width: "auto", crop: "scale" },
{ effect: "art:daguerre", border: "3px_solid_rgb:00390b", radius: 20 }
]})}`);

console.log();
console.log("> That's it. You can now open the URLs above in a browser");
Expand Down
9 changes: 5 additions & 4 deletions samples/basic/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "basic",
"version": "0.0.0",
"description": "basic node sample of cloudinary npm",
"description": "Basic node sample of Cloudinary npm",
"main": "basic.js",
"dependencies": {
"cloudinary": "^1.3.0",
"dotenv": "1.x"
"cloudinary": "^2.5.1",
"dotenv": "16.x"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node basic.js"
},
"author": "",
"license": "ISC"
Expand Down
6 changes: 3 additions & 3 deletions samples/photo_album/config/schema.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var Schema = require('jugglingdb').Schema;
const Schema = require('jugglingdb').Schema;

var schema = new Schema('memory');
const schema = new Schema('memory');
// Uncomment if you want to use mongodb adapter
// var schema = new Schema('mongodb');
// const schema = new Schema('mongodb');

// Define models
schema.define('Photo', {
Expand Down
Loading