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 2 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
181 changes: 124 additions & 57 deletions samples/basic/basic.js
Original file line number Diff line number Diff line change
@@ -1,116 +1,171 @@
require('dotenv').load();
require('dotenv').config();
var fs = require('fs');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var fs = require('fs');
const fs = require('fs');

I see that in other files var's were replaced with const's, so please include these changes here as well.

var cloudinary = require('cloudinary').v2;

var 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) {
cloudinary.uploader.upload('pizza.jpg', { tags: 'basic_sample' })
.then((image) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.then((image) => {
.then((image) => {

and continue with 2-spaces indentation throughout the file please where you're using promises.

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);
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();
console.log("** File Upload (Promise)");
if (err) { console.warn(err); }
});
} catch (err) {
console.error("Error in File Upload (Async/Await): ", err);
}
})();


// Public Id
cloudinary.uploader.upload('pizza.jpg', { tags: 'basic_sample', public_id: 'my_favorite_pizza' }, function (err, image) {
// 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("** 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);
waitForAllUploads("pizza2", image);
})
.catch((err) => {
console.warn(err);
});


// Eager Transformations:
// Applied as soon as the file is uploaded, instead of lazily applying them when accessed by your site's visitors.
/* 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 = {
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.

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");
if (err) { console.warn(err); }
console.log("* " + image.public_id);
console.log("* " + image.eager[0].url);
waitForAllUploads("lake", err, image);
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" }, function (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" })
.then((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);
waitForAllUploads("couple", image);
})
.catch((err) => {
console.warn(err);
});


// 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) {
/*
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");
if (err) { console.warn(err); }
console.log("** Remote Url using Async/Await");
console.log("* " + image.public_id);
console.log("* " + image.url);
waitForAllUploads("couple2", err, image);
});
waitForAllUploads("couple2", image);

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



function waitForAllUploads(id, err, image) {
function waitForAllUploads(id, image) {
uploads[id] = image;
var ids = Object.keys(uploads);
if (ids.length === 6) {
Expand Down Expand Up @@ -141,6 +196,18 @@ function performTransformations() {
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();
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");
console.log("> and check out the generated images.");
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