-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.ts
More file actions
47 lines (40 loc) · 1.14 KB
/
service.ts
File metadata and controls
47 lines (40 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import ImageKitJs from 'imagekit-javascript';
export class ImagekitService {
imagekit: ImageKitJs;
constructor() {
this.imagekit = this.client;
}
get client() {
return new ImageKitJs({
publicKey: 'public_******',
urlEndpoint: 'https://ik.imagekit.io/******',
});
}
upload(file: File): Promise<string> {
const self = this;
const fileName = file.name;
const uploadOptions = {
file,
fileName,
tags: ['tag1'],
};
return new Promise((resolve, reject) => {
this.imagekit.upload(uploadOptions, function (err: any, result: any) {
console.log(`🚀 => ImagekitService => upload => arguments:`, arguments);
// error
if (err) {
console.log(`🚀 => ImagekitService => upload => err:`, err);
return reject(err);
}
// get download url
const url = self.imagekit.url({
src: result.url,
transformation: [{ height: '300', width: '400' }],
});
console.log(`🚀 => ImagekitService => upload => url:`, url);
// return download url
return resolve(url);
});
});
}
}