Skip to content

Commit 25e04e6

Browse files
authored
Updated Sample Projects (#698)
* Updated basic sample project -> Made changes to its README -> Updated the necessary dependencies -> Optimized the code to current standards * Updated photo album app * Improved code consistency and readability -> Used template literals -> Improved indentations around promise handling -> Switched from 'var' to 'const'
1 parent 265ccb8 commit 25e04e6

File tree

18 files changed

+781
-422
lines changed

18 files changed

+781
-422
lines changed

samples/basic/basic.js

Lines changed: 161 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,176 @@
1-
require('dotenv').load();
2-
var fs = require('fs');
3-
var cloudinary = require('cloudinary').v2;
1+
require('dotenv').config();
2+
const fs = require('fs');
3+
const cloudinary = require('cloudinary').v2;
44

5-
var uploads = {};
5+
let uploads = {};
66

7-
// set your env variable CLOUDINARY_URL or set the following configuration
8-
/* cloudinary.config({
9-
cloud_name: '',
10-
api_key: '',
11-
api_secret: ''
12-
}); */
7+
/*
8+
Set your environment variable CLOUDINARY_URL or set the following configuration
9+
cloudinary.config({
10+
cloud_name: '',
11+
api_key: '',
12+
api_secret: ''
13+
});
14+
*/
1315

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

1618
// File upload
17-
cloudinary.uploader.upload('pizza.jpg', { tags: 'basic_sample' }, function (err, image) {
18-
console.log();
19-
console.log("** File Upload");
20-
if (err) { console.warn(err); }
21-
console.log("* public_id for the uploaded image is generated by Cloudinary's service.");
22-
console.log("* " + image.public_id);
23-
console.log("* " + image.url);
24-
waitForAllUploads("pizza", err, image);
25-
});
19+
cloudinary.uploader.upload('pizza.jpg', { tags: 'basic_sample' })
20+
.then((image) => {
21+
console.log();
22+
console.log("** File Upload");
23+
console.log("* public_id for the uploaded image is generated by Cloudinary's service.");
24+
console.log(`* ${image.public_id}`);
25+
console.log(`* ${image.url}`);
26+
waitForAllUploads("pizza", image);
27+
})
28+
.catch((err) => {
29+
console.warn(err);
30+
});
2631

2732

2833
// Stream upload
29-
var upload_stream = cloudinary.uploader.upload_stream({ tags: 'basic_sample' }, function (err, image) {
30-
console.log();
31-
console.log("** Stream Upload");
32-
if (err) { console.warn(err); }
33-
console.log("* Same image, uploaded via stream");
34-
console.log("* " + image.public_id);
35-
console.log("* " + image.url);
36-
waitForAllUploads("pizza3", err, image);
37-
});
38-
fs.createReadStream('pizza.jpg').pipe(upload_stream);
34+
async function uploadStreamWithPromise(filePath, uploadOptions) {
35+
try {
36+
const byteArrayBuffer = fs.readFileSync(filePath);
37+
38+
const uploadResult = await new Promise((resolve, reject) => {
39+
const stream = cloudinary.uploader.upload_stream(uploadOptions, (err, result) => {
40+
if (err) {
41+
return reject(err);
42+
}
43+
resolve(result);
44+
});
45+
stream.end(byteArrayBuffer);
46+
});
47+
48+
console.log();
49+
console.log("** Stream Upload");
50+
console.log(`* public_id for the uploaded image is: ${uploadResult.public_id}`);
51+
console.log(`* URL: ${uploadResult.url}`);
52+
53+
waitForAllUploads("pizza3", uploadResult);
54+
55+
} catch (error) {
56+
console.error("Error during stream upload: ", error);
57+
}
58+
}
3959

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

41-
// File upload (example for promise api)
42-
cloudinary.uploader.upload('pizza.jpg', { tags: 'basic_sample' })
43-
.then(function (image) {
62+
63+
64+
// File upload (example for async/await)
65+
(async () => {
66+
try {
67+
const image = await cloudinary.uploader.upload('pizza.jpg', { tags: 'basic_sample' });
4468
console.log();
45-
console.log("** File Upload (Promise)");
69+
console.log("** File Upload (Async/Await)");
4670
console.log("* public_id for the uploaded image is generated by Cloudinary's service.");
47-
console.log("* " + image.public_id);
48-
console.log("* " + image.url);
49-
})
50-
.catch(function (err) {
71+
console.log(`* ${image.public_id}`);
72+
console.log(`* ${image.url}`);
73+
} catch (err) {
74+
console.error("Error in File Upload (Async/Await): ", err);
75+
}
76+
})();
77+
78+
79+
// Files can also be uploaded with a specified Public id
80+
cloudinary.uploader.upload('pizza.jpg', { tags: 'basic_sample', public_id: 'my_favorite_pizza' })
81+
.then((image) => {
5182
console.log();
52-
console.log("** File Upload (Promise)");
53-
if (err) { console.warn(err); }
83+
console.log("** Public Id");
84+
console.log("* Same image, uploaded with a custom public_id");
85+
console.log(`* ${image.public_id}`);
86+
console.log(`* ${image.url}`);
87+
waitForAllUploads("pizza2", image);
88+
})
89+
.catch((err) => {
90+
console.warn(err);
5491
});
5592

5693

57-
// Public Id
58-
cloudinary.uploader.upload('pizza.jpg', { tags: 'basic_sample', public_id: 'my_favorite_pizza' }, function (err, image) {
59-
console.log();
60-
console.log("** Public Id");
61-
if (err) { console.warn(err); }
62-
console.log("* Same image, uploaded with a custom public_id");
63-
console.log("* " + image.public_id);
64-
console.log("* " + image.url);
65-
waitForAllUploads("pizza2", err, image);
66-
});
67-
68-
69-
// Eager Transformations:
70-
// Applied as soon as the file is uploaded, instead of lazily applying them when accessed by your site's visitors.
71-
var eager_options = {
94+
/* Eager Transformations:
95+
Applied as soon as the file is uploaded, instead of lazily applying them when accessed by your site's visitors.
96+
*/
97+
const eager_options = {
7298
width: 200, height: 150, crop: 'scale', format: 'jpg'
7399
};
74-
cloudinary.uploader.upload("lake.jpg", { tags: "basic_sample", public_id: "blue_lake", eager: eager_options }, function (err, image) {
75-
// "eager" parameter accepts a hash (or just a single item). You can pass
76-
// named transformations or transformation parameters as we do here.
77-
console.log();
78-
console.log("** Eager Transformations");
79-
if (err) { console.warn(err); }
80-
console.log("* " + image.public_id);
81-
console.log("* " + image.eager[0].url);
82-
waitForAllUploads("lake", err, image);
83-
});
84-
85-
86-
// Remote URL:
87-
// In the two following examples, the file is fetched from a remote URL and stored in Cloudinary.
88-
// This allows you to apply transformations and take advantage of Cloudinary's CDN layer.
89-
cloudinary.uploader.upload('http://res.cloudinary.com/demo/image/upload/couple.jpg', { tags: "basic_sample" }, function (err, image) {
90-
console.log();
91-
console.log("** Remote Url");
92-
if (err) { console.warn(err); }
93-
console.log("* " + image.public_id);
94-
console.log("* " + image.url);
95-
waitForAllUploads("couple", err, image);
96-
});
97-
98-
99-
// Here, the transformation is applied to the uploaded image BEFORE storing it on the cloud.
100-
// The original uploaded image is discarded.
101-
cloudinary.uploader.upload('http://res.cloudinary.com/demo/image/upload/couple.jpg',
102-
{ "tags": "basic_sample", "width": 500, "height": 500, "crop": "fit", "effect": "saturation:-70" },
103-
function (err, image) {
100+
101+
cloudinary.uploader.upload("lake.jpg", { tags: "basic_sample", public_id: "blue_lake", eager: eager_options })
102+
/*
103+
"eager" parameter accepts a hash (or just a single item). You can pass
104+
named transformations or transformation parameters as we do here.
105+
*/
106+
.then((image) => {
107+
108+
console.log();
109+
console.log("** Eager Transformations");
110+
console.log(`* ${image.public_id}`);
111+
console.log(`* ${image.eager[0].url}`);
112+
waitForAllUploads("lake", image);
113+
})
114+
.catch((err) => {
115+
console.warn(err);
116+
});
117+
118+
119+
/*
120+
Remote URL:
121+
In the two following examples, the file is fetched from a remote URL and stored in Cloudinary.
122+
This allows you to apply transformations and take advantage of Cloudinary's CDN layer.
123+
*/
124+
cloudinary.uploader.upload('http://res.cloudinary.com/demo/image/upload/couple.jpg', { tags: "basic_sample" })
125+
.then((image) => {
104126
console.log();
105127
console.log("** Remote Url");
106-
if (err) { console.warn(err); }
107-
console.log("* " + image.public_id);
108-
console.log("* " + image.url);
109-
waitForAllUploads("couple2", err, image);
128+
console.log(`* ${image.public_id}`);
129+
console.log(`* ${image.url}`);
130+
waitForAllUploads("couple", image);
131+
})
132+
.catch((err) => {
133+
console.warn(err);
110134
});
111135

112136

113-
function waitForAllUploads(id, err, image) {
137+
/*
138+
Here, the transformation is applied to the uploaded image BEFORE storing it on the cloud.
139+
The original uploaded image is discarded.
140+
This is being done using async/await to demonstrate its functionality with Remote URLs
141+
*/
142+
(async () => {
143+
try {
144+
const image = await cloudinary.uploader.upload(
145+
'http://res.cloudinary.com/demo/image/upload/couple.jpg',
146+
{
147+
tags: "basic_sample",
148+
width: 500,
149+
height: 500,
150+
crop: "fit",
151+
effect: "saturation:-70"
152+
}
153+
);
154+
155+
console.log();
156+
console.log("** Remote Url using Async/Await");
157+
console.log(`* ${image.public_id}`);
158+
console.log(`* ${image.url}`);
159+
waitForAllUploads("couple2", image);
160+
161+
} catch (err) {
162+
console.warn(err);
163+
}
164+
})();
165+
166+
167+
168+
function waitForAllUploads(id, image) {
114169
uploads[id] = image;
115-
var ids = Object.keys(uploads);
170+
let ids = Object.keys(uploads);
116171
if (ids.length === 6) {
117172
console.log();
118-
console.log('** uploaded all files (' + ids.join(',') + ') to cloudinary');
173+
console.log(`** uploaded all files (${ids.join(',')}) to cloudinary`);
119174
performTransformations();
120175
}
121176
}
@@ -127,19 +182,30 @@ function performTransformations() {
127182
console.log(">> >> >> >> >> >> >> >> >> >> Transformations << << << << << << << << << <<");
128183
console.log();
129184
console.log("> Fit into 200x150");
130-
console.log("> " + cloudinary.url(uploads.pizza2.public_id, { width: 200, height: 150, crop: "fit", format: "jpg" }));
185+
console.log(`> ${cloudinary.url(uploads.pizza2.public_id, { width: 200, height: 150, crop: "fit", format: "jpg" })}`);
131186

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

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

140195
console.log();
141196
console.log("> Fill 200x150, round corners, apply the sepia effect");
142-
console.log("> " + cloudinary.url(uploads.couple2.public_id, { width: 200, height: 150, crop: "fill", gravity: "face", radius: 10, effect: "sepia", format: "jpg" }));
197+
console.log(`> ${cloudinary.url(uploads.couple2.public_id, { width: 200, height: 150, crop: "fill", gravity: "face", radius: 10, effect: "sepia", format: "jpg" })}`);
198+
199+
console.log();
200+
console.log("> Optimisation of image quality and file format to minimize file size and maintain required quality level");
201+
console.log(`> ${cloudinary.url(uploads.lake.public_id, {transformation: [ {width: 500, crop: "scale"}, {quality: "auto", fetch_format: "auto"} ]})}`);
202+
203+
console.log();
204+
console.log("> Returning images that fit the size and device pixel ratio(dpr) of a user's device");
205+
console.log(`> ${cloudinary.url(uploads.lake.public_id, {transformation: [
206+
{ dpr: "auto", responsive: true, width: "auto", crop: "scale" },
207+
{ effect: "art:daguerre", border: "3px_solid_rgb:00390b", radius: 20 }
208+
]})}`);
143209

144210
console.log();
145211
console.log("> That's it. You can now open the URLs above in a browser");

samples/basic/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "basic",
33
"version": "0.0.0",
4-
"description": "basic node sample of cloudinary npm",
4+
"description": "Basic node sample of Cloudinary npm",
55
"main": "basic.js",
66
"dependencies": {
7-
"cloudinary": "^1.3.0",
8-
"dotenv": "1.x"
7+
"cloudinary": "^2.5.1",
8+
"dotenv": "16.x"
99
},
1010
"scripts": {
11-
"test": "echo \"Error: no test specified\" && exit 1"
11+
"test": "echo \"Error: no test specified\" && exit 1",
12+
"start": "node basic.js"
1213
},
1314
"author": "",
1415
"license": "ISC"

samples/photo_album/config/schema.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
var Schema = require('jugglingdb').Schema;
1+
const Schema = require('jugglingdb').Schema;
22

3-
var schema = new Schema('memory');
3+
const schema = new Schema('memory');
44
// Uncomment if you want to use mongodb adapter
5-
// var schema = new Schema('mongodb');
5+
// const schema = new Schema('mongodb');
66

77
// Define models
88
schema.define('Photo', {

0 commit comments

Comments
 (0)