Skip to content

Commit a2c6a55

Browse files
author
utkace
committed
helpers: add upload helper
1 parent c26b17b commit a2c6a55

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/helpers/upload.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import ImageKit from 'imagekit-javascript';
2+
3+
export const uploadImage = ({ e, file, fileName, useUniqueFileName, tags, folder, isPrivateFile, customCoordinates, responseFields, publicKey, urlEndpoint, authenticationEndpoint }) => {
4+
5+
if (!publicKey) {
6+
throw new Error("Missing publicKey during initialization");
7+
}
8+
9+
if (!urlEndpoint) {
10+
throw new Error("Missing urlEndpoint during initialization");
11+
}
12+
13+
if (!authenticationEndpoint) {
14+
throw new Error("Missing authenticationEndpoint during initialization");
15+
}
16+
17+
let onError = (e, err) => {
18+
e.insertAdjacentHTML(
19+
"afterend",
20+
`<div>${err}</div>`
21+
);
22+
};
23+
24+
let onSuccess = (e) => {
25+
e.insertAdjacentHTML(
26+
"afterend",
27+
`<div>Image Uploaded</div>`
28+
);
29+
};
30+
31+
const ik = new ImageKit({
32+
publicKey: publicKey,
33+
urlEndpoint: urlEndpoint,
34+
authenticationEndpoint: authenticationEndpoint
35+
});
36+
37+
const params = {
38+
file: file,
39+
fileName: fileName,
40+
useUniqueFileName: useUniqueFileName,
41+
isPrivateFile: isPrivateFile,
42+
folder: folder,
43+
}
44+
if (tags) {
45+
Object.assign(params, { tags: tags });
46+
}
47+
48+
if (customCoordinates) {
49+
Object.assign(params, { customCoordinates: customCoordinates });
50+
}
51+
52+
if (responseFields) {
53+
Object.assign(params, { responseFields: responseFields });
54+
}
55+
56+
ik.upload(params
57+
, function (err, result) {
58+
if (err) {
59+
onError(e, err);
60+
} else {
61+
onSuccess(e);
62+
}
63+
});
64+
}

0 commit comments

Comments
 (0)