Skip to content

Commit ea51975

Browse files
committed
error handling and comments
1 parent ce81639 commit ea51975

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "netlify-plugin-cloudinary",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "",
55
"main": "src/index.js",
66
"scripts": {

src/index.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const cloudinary = require('cloudinary').v2;
77
/**
88
* TODO
99
* - Track uploads to avoid uploading same image multiple times
10+
* - Handle srcset
1011
*/
1112

1213
module.exports = {
@@ -29,18 +30,28 @@ module.exports = {
2930
api_secret: apiSecret
3031
});
3132

33+
// Find all HTML source files in the publish directory
34+
3235
const pages = glob.sync(`${PUBLISH_DIR}/**/*.html`);
36+
const errors = [];
3337

3438
for ( const page of pages ) {
3539
const html = await fs.readFile(page, 'utf-8');
3640
const dom = new JSDOM(html);
41+
42+
// Loop through all images found in the DOM and swap the source with
43+
// a Cloudinary URL
44+
3745
const images = Array.from(dom.window.document.querySelectorAll('img'));
3846

3947
for ( const $img of images ) {
4048
let imgSrc = $img.getAttribute('src');
4149
let cloudinarySrc;
4250

4351
if ( deliveryType === 'fetch' ) {
52+
// fetch allows us to pass in a remote URL to the Cloudinary API
53+
// which it will cache and serve from the CDN, but not store
54+
4455
imgSrc = determineRemoteUrl(imgSrc);
4556

4657
cloudinarySrc = cloudinary.url(imgSrc, {
@@ -54,6 +65,12 @@ module.exports = {
5465
]
5566
});
5667
} else if ( deliveryType === 'upload' ) {
68+
// upload will actually store the image in the Cloudinary account
69+
// and subsequently serve that stored image
70+
71+
// If our image is locally sourced, we need to obtain the full
72+
// local relative path so that we can tell Cloudinary where
73+
// to upload from
5774

5875
if ( !isRemoteUrl(imgSrc) ) {
5976
imgSrc = path.join(PUBLISH_DIR, imgSrc);
@@ -62,21 +79,38 @@ module.exports = {
6279
let results;
6380

6481
if ( apiKey && apiSecret ) {
82+
// We need an API Key and Secret to use signed uploading
83+
6584
try {
6685
results = await cloudinary.uploader.upload(imgSrc);
6786
} catch(e) {
68-
console.log('e', e)
87+
const { error } = e;
88+
errors.push({
89+
imgSrc,
90+
message: error.message
91+
});
92+
continue;
6993
}
7094
} else if ( uploadPreset ) {
95+
// If we want to avoid signing our uploads, we don't need our API Key and Secret,
96+
// however, we need to provide an uploadPreset
97+
7198
try {
7299
results = await cloudinary.uploader.unsigned_upload(imgSrc, uploadPreset);
73100
} catch(e) {
74-
console.log('e', e)
101+
const { error } = e;
102+
errors.push({
103+
imgSrc,
104+
message: error.message
105+
});
106+
continue;
75107
}
76108
} else {
77109
throw new Error(`To use deliveryType ${deliveryType}, please use an uploadPreset for unsigned requests or an API Key and Secret for signed requests.`);
78110
}
79111

112+
// Finally use the stored public ID to grab the image URL
113+
80114
const { public_id } = results;
81115

82116
cloudinarySrc = cloudinary.url(public_id, {
@@ -96,6 +130,13 @@ module.exports = {
96130
await fs.writeFile(page, dom.serialize());
97131
}
98132
}
133+
134+
if ( errors.length > 0) {
135+
console.log(`Done with ${errors.length} errors...`);
136+
console.log(JSON.stringify(errors, null, 2));
137+
} else {
138+
console.log('Done.');
139+
}
99140
}
100141

101142
}

0 commit comments

Comments
 (0)