Skip to content

Commit b46559a

Browse files
authored
Merge pull request #7 from colbyfayock/feature/4-folder
Add default folder and option to configure
2 parents 2739df8 + 40cc3e9 commit b46559a

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

manifest.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ inputs:
77
required: false
88
description: "The method in which Cloudinary stores and delivers images (Ex: fetch, upload)"
99
default: "fetch"
10+
- name: folder
11+
required: false
12+
description: "Folder all media will be stored in. Defaults to Netlify site name"
1013
- name: uploadPreset
1114
required: false
1215
description: "Defined set of asset upload defaults in Cloudinary"

src/index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ module.exports = {
1616

1717
async onPostBuild({ constants, inputs }) {
1818
const { PUBLISH_DIR } = constants;
19-
const { deliveryType, uploadPreset } = inputs;
19+
const {
20+
deliveryType,
21+
uploadPreset,
22+
folder = process.env.SITE_NAME
23+
} = inputs;
2024

2125
const cloudName = process.env.CLOUDINARY_CLOUD_NAME || inputs.cloudName;
2226
const apiKey = process.env.CLOUDINARY_API_KEY;
@@ -90,14 +94,19 @@ module.exports = {
9094

9195
const id = `${imgName}-${hash}`;
9296

97+
const uploadOptions = {
98+
folder,
99+
public_id: id
100+
}
101+
93102
let results;
94-
103+
95104
if ( apiKey && apiSecret ) {
96105
// We need an API Key and Secret to use signed uploading
97106

98107
try {
99108
results = await cloudinary.uploader.upload(imgSrc, {
100-
public_id: id,
109+
...uploadOptions,
101110
overwrite: false
102111
});
103112
} catch(e) {
@@ -114,7 +123,7 @@ module.exports = {
114123

115124
try {
116125
results = await cloudinary.uploader.unsigned_upload(imgSrc, uploadPreset, {
117-
public_id: id,
126+
...uploadOptions
118127
// Unsigned uploads default to overwrite: false
119128
});
120129
} catch(e) {
@@ -169,7 +178,7 @@ function isRemoteUrl(path) {
169178
return path.startsWith('http');
170179
}
171180

172-
/**
181+
/**
173182
* determineRemoteUrl
174183
*/
175184

@@ -181,7 +190,7 @@ function determineRemoteUrl(path) {
181190
if ( !path.startsWith('/') ) {
182191
url = `/${url}`;
183192
}
184-
193+
185194
url = `${process.env.DEPLOY_PRIME_URL}${url}`;
186195

187196
return url;

0 commit comments

Comments
 (0)