@@ -7,6 +7,7 @@ const cloudinary = require('cloudinary').v2;
7
7
/**
8
8
* TODO
9
9
* - Track uploads to avoid uploading same image multiple times
10
+ * - Handle srcset
10
11
*/
11
12
12
13
module . exports = {
@@ -29,18 +30,28 @@ module.exports = {
29
30
api_secret : apiSecret
30
31
} ) ;
31
32
33
+ // Find all HTML source files in the publish directory
34
+
32
35
const pages = glob . sync ( `${ PUBLISH_DIR } /**/*.html` ) ;
36
+ const errors = [ ] ;
33
37
34
38
for ( const page of pages ) {
35
39
const html = await fs . readFile ( page , 'utf-8' ) ;
36
40
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
+
37
45
const images = Array . from ( dom . window . document . querySelectorAll ( 'img' ) ) ;
38
46
39
47
for ( const $img of images ) {
40
48
let imgSrc = $img . getAttribute ( 'src' ) ;
41
49
let cloudinarySrc ;
42
50
43
51
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
+
44
55
imgSrc = determineRemoteUrl ( imgSrc ) ;
45
56
46
57
cloudinarySrc = cloudinary . url ( imgSrc , {
@@ -54,6 +65,12 @@ module.exports = {
54
65
]
55
66
} ) ;
56
67
} 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
57
74
58
75
if ( ! isRemoteUrl ( imgSrc ) ) {
59
76
imgSrc = path . join ( PUBLISH_DIR , imgSrc ) ;
@@ -62,21 +79,38 @@ module.exports = {
62
79
let results ;
63
80
64
81
if ( apiKey && apiSecret ) {
82
+ // We need an API Key and Secret to use signed uploading
83
+
65
84
try {
66
85
results = await cloudinary . uploader . upload ( imgSrc ) ;
67
86
} catch ( e ) {
68
- console . log ( 'e' , e )
87
+ const { error } = e ;
88
+ errors . push ( {
89
+ imgSrc,
90
+ message : error . message
91
+ } ) ;
92
+ continue ;
69
93
}
70
94
} 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
+
71
98
try {
72
99
results = await cloudinary . uploader . unsigned_upload ( imgSrc , uploadPreset ) ;
73
100
} catch ( e ) {
74
- console . log ( 'e' , e )
101
+ const { error } = e ;
102
+ errors . push ( {
103
+ imgSrc,
104
+ message : error . message
105
+ } ) ;
106
+ continue ;
75
107
}
76
108
} else {
77
109
throw new Error ( `To use deliveryType ${ deliveryType } , please use an uploadPreset for unsigned requests or an API Key and Secret for signed requests.` ) ;
78
110
}
79
111
112
+ // Finally use the stored public ID to grab the image URL
113
+
80
114
const { public_id } = results ;
81
115
82
116
cloudinarySrc = cloudinary . url ( public_id , {
@@ -96,6 +130,13 @@ module.exports = {
96
130
await fs . writeFile ( page , dom . serialize ( ) ) ;
97
131
}
98
132
}
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
+ }
99
140
}
100
141
101
142
}
0 commit comments