-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
cloudinary.ts
159 lines (142 loc) · 4.13 KB
/
cloudinary.ts
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import fs from 'fs';
import cloudinary from 'cloudinary';
export type File = { id: string; filename: string; _meta: cloudinary.UploadApiResponse };
export type CloudinaryImageFormat = {
prettyName?: string | null;
width?: string | null;
height?: string | null;
crop?: string | null;
aspect_ratio?: string | null;
gravity?: string | null;
zoom?: string | null;
x?: string | null;
y?: string | null;
format?: string | null;
fetch_format?: string | null;
quality?: string | null;
radius?: string | null;
angle?: string | null;
effect?: string | null;
opacity?: string | null;
border?: string | null;
background?: string | null;
overlay?: string | null;
underlay?: string | null;
default_image?: string | null;
delay?: string | null;
color?: string | null;
color_space?: string | null;
dpr?: string | null;
page?: string | null;
density?: string | null;
flags?: string | null;
transformation?: string | null;
};
function uploadStream(
stream: fs.ReadStream,
options: cloudinary.UploadApiOptions
): Promise<cloudinary.UploadApiResponse> {
return new Promise((resolve, reject) => {
const cloudinaryStream = cloudinary.v2.uploader.upload_stream(options, (error, result) => {
if (error || !result) {
return reject(error);
}
resolve(result);
});
stream.pipe(cloudinaryStream);
});
}
export class CloudinaryAdapter {
cloudName: string;
apiKey: string;
apiSecret: string;
folder?: string;
constructor({
cloudName,
apiKey,
apiSecret,
folder,
}: {
cloudName: string;
apiKey: string;
apiSecret: string;
folder?: string;
}) {
this.cloudName = cloudName;
this.apiKey = apiKey;
this.apiSecret = apiSecret;
this.folder = folder || undefined;
}
ready() {
return this.cloudName.length > 0 && this.apiKey.length > 0 && this.apiSecret.length > 0;
}
/**
* Params: { stream, filename, id }
*/
save({ stream, filename, id }: { stream: fs.ReadStream; filename: string; id: string }) {
if (!this.ready()) throw new Error('Cloudinary adapter is not ready');
// push to cloudinary
return uploadStream(stream, {
public_id: id,
folder: this.folder,
api_key: this.apiKey,
api_secret: this.apiSecret,
cloud_name: this.cloudName,
}).then(result => ({
// return the relevant data for the file api
id,
filename,
_meta: result,
}));
}
/**
* Deletes the given file from cloudinary
* @param file File field data
* @param options Delete options passed to cloudinary.
* For available options refer to the [Cloudinary destroy API](https://cloudinary.com/documentation/image_upload_api_reference#destroy_method).
*/
delete(file?: File, options = {}) {
if (!this.ready()) throw new Error('Cloudinary adapter is not ready');
const destroyOptions = {
// Auth
api_key: this.apiKey,
api_secret: this.apiSecret,
cloud_name: this.cloudName,
...options,
};
return new Promise((resolve, reject) => {
if (file) {
// @ts-ignore
cloudinary.v2.uploader.destroy(file._meta.public_id, destroyOptions, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
} else {
reject(new Error("Missing required argument 'file'."));
}
});
}
publicUrl(file?: File) {
return file?._meta?.secure_url || null;
}
publicUrlTransformed(file: File, options: CloudinaryImageFormat = {}) {
if (!file._meta) return null;
const { prettyName, ...transformation } = options;
// No formatting options provided, return the publicUrl field
if (!Object.keys(transformation).length) return this.publicUrl(file);
const { public_id, format } = file._meta;
// ref https://github.com/cloudinary/cloudinary_npm/blob/439586eac73cee7f2803cf19f885e98f237183b3/src/utils.coffee#L472
// @ts-ignore
return cloudinary.url(public_id, {
type: 'upload',
format,
secure: true,
url_suffix: prettyName,
transformation,
cloud_name: this.cloudName,
});
}
}