-
Notifications
You must be signed in to change notification settings - Fork 323
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
aarya-16
wants to merge
3
commits into
cloudinary:master
Choose a base branch
from
aarya-16:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,116 +1,171 @@ | ||||||
require('dotenv').load(); | ||||||
require('dotenv').config(); | ||||||
var fs = require('fs'); | ||||||
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) => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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) { | ||||||
|
@@ -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."); | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that in other files
var
's were replaced withconst
's, so please include these changes here as well.