-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
318 lines (267 loc) · 7.81 KB
/
types.ts
File metadata and controls
318 lines (267 loc) · 7.81 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import { AdminUser, ImageGenerationAdapter, StorageAdapter, HttpExtra } from "adminforth";
export type PluginOptions = {
/**
* The name of the column where the path to the uploaded file is stored.
* On place of this column, a file upload field will be shown.
*/
pathColumnName: string;
/**
* the list of allowed file extensions
*/
allowedFileExtensions?: string[]; // allowed file extensions
/**
* the maximum file size in bytes
*/
maxFileSize?: number;
/**
* The path where the file will be uploaded to the S3 bucket, same path will be stored in the database
* in the column specified in {@link pathColumnName}
*
* example:
*
* ```typescript
* s3Path: ({originalFilename}) => `/aparts/${originalFilename}`
* ```
*
*/
filePath: ({originalFilename, originalExtension, contentType, record }: {
originalFilename: string,
originalExtension: string,
contentType: string,
record?: any,
}) => string,
preview?: {
/**
* Whether to use preview components provided by the plugin. BY default true
*/
usePreviewComponents?: boolean,
/**
* Maximum width of the preview image
*/
maxWidth?: string,
/**
* Maximum width of the preview image in list view
*/
maxListWidth?: string,
/**
* Maximum width of the preview image in show view
*/
maxShowWidth?: string,
/**
* Minimum width of the preview image
*/
minWidth?: string,
/**
* Minimum width of the preview image in list view
*/
minListWidth?: string,
/**
* Minimum width of the preview image in show view
*/
minShowWidth?: string,
/**
* Used to display preview (if it is image) in list and show views.
* Defaulted to the AWS S3 presigned URL if resource is private or public URL if resource is public.
* Can be used to generate custom e.g. CDN(e.g. Cloudflare) URL to worm up cache and deliver preview faster.
*
* Example:
*
* ```typescript
* previewUrl: ({record, path}) => `https://my-bucket.s3.amazonaws.com/${path}`,
* ```
*
*/
previewUrl?: ({filePath}) => string,
}
/**
* AI image generation options
*/
generation?: {
adapter: ImageGenerationAdapter,
/**
* The size of the generated image.
*/
outputSize?: string,
/**
* The number of images to generate
* in one request
*/
countToGenerate: number,
/**
* Prompt which will be suggested to user during image generation. You can use record fields with mustache brackets:
* E.g. 'Generate a photo of car {{ model }} from {{ brand }} in {{ color }} color of {{ year }} year'. For now plugin get's these fields from open create/edit form
* so they should be present in the form.
*
* Reserved variables:
* - {{field}} - label of resource
* - {{resource}} - label of resource
*/
generationPrompt?: string,
/**
* If you want to use some image as reference for generation, you can use this function to get the path to the image.
*/
attachFiles?: ({ record, adminUser }: {
record: any,
adminUser: AdminUser,
}) => string[] | Promise<string[]>,
/**
* Since AI generation can be expensive, we can limit the number of requests per IP.
*/
rateLimit?: {
/**
* E.g. 5/1d - 5 requests per day
* 3/1h - 3 requests per hour
*/
limit: string,
/**
* !Not used now
* Message shown to user when rate limit is reached
*/
errorMessage: string,
},
}
/**
* The adapter used to store the files.
* For now only S3 adapter is supported.
*/
storageAdapter: StorageAdapter,
}
/**
* Parameters for the UploadPlugin.uploadFromBuffer API.
* Used to upload a binary file buffer, create a record in the resource,
* and return the stored path and preview URL.
*/
export type UploadFromBufferParams = {
/**
* Original file name including extension, used to derive storage path
* and validate the file extension. Example: "photo.png".
*/
filename: string;
/**
* MIME type of the uploaded file. Will be used as Content-Type
* when uploading to the storage adapter. Example: "image/png".
*/
contentType: string;
/**
* Binary contents of the file. Can be a Node.js Buffer, Uint8Array,
* or ArrayBuffer. The plugin uploads this directly without converting
* to base64.
*/
buffer: Buffer | Uint8Array | ArrayBuffer;
/**
* Authenticated admin user on whose behalf the record is created.
* Passed through to AdminForth createResourceRecord hooks.
*/
adminUser: AdminUser;
/**
* Optional HTTP context (headers, IP, etc.) forwarded to AdminForth
* createResourceRecord hooks. Needed to execute hooks on creation resource record for the upload.
*/
extra?: HttpExtra;
/**
* Optional additional attributes to set on the created record
* together with the path column managed by the plugin.
* Values here do NOT affect the generated storage path.
*/
recordAttributes?: Record<string, any>;
};
/**
* Parameters for the UploadPlugin.uploadFromBufferToExistingRecord API.
* Used to upload a binary file buffer and update the path column
* of an already existing record identified by its primary key.
*/
export type UploadFromBufferToExistingRecordParams = UploadFromBufferParams & {
/**
* Primary key value of the existing record whose file path
* should be replaced.
*/
recordId: any;
};
/**
* Parameters for generating an upload URL for an existing record
* that will be uploaded directly from the browser.
*/
export type GetUploadUrlParams = {
/**
* Primary key of the record whose file is being replaced.
*/
recordId?: any;
/**
* Full file name including extension, as provided by the browser.
*/
filename: string;
/**
* MIME type reported by the browser, used as Content-Type for storage.
*/
contentType: string;
/**
* Optional file size in bytes. If provided, it will be validated
* against {@link PluginOptions.maxFileSize}.
*/
size?: number;
};
/**
* Parameters for generating an upload URL for a new record
* that will be uploaded directly from the browser.
*/
export type GetUploadUrlForNewRecordParams = {
/**
* Full file name including extension, as provided by the browser.
*/
filename: string;
/**
* MIME type reported by the browser, used as Content-Type for storage.
*/
contentType: string;
/**
* Optional file size in bytes. If provided, it will be validated
* against {@link PluginOptions.maxFileSize}.
*/
size?: number;
};
/**
* Parameters for committing a previously generated upload URL
* to an existing record. This is used after the browser finished
* uploading directly to the storage provider.
*/
export type CommitUrlToUpdateExistingRecordParams = {
/**
* Primary key of the record whose file is being replaced.
*/
recordId: any;
/**
* Storage path (key) that was returned by getUploadUrlForExistingRecord.
*/
filePath: string;
/**
* Authenticated admin user on whose behalf the record is updated.
*/
adminUser: AdminUser;
/**
* Optional HTTP context (headers, IP, etc.).
*/
extra?: HttpExtra;
};
/**
* Parameters for committing a previously generated upload URL
* to a new record. This is used after the browser finished
* uploading directly to the storage provider.
*/
export type CommitUrlToNewRecordParams = {
/**
* Storage path (key) that was returned by getUploadUrlForNewRecord.
*/
filePath: string;
/**
* Authenticated admin user on whose behalf the record is created.
*/
adminUser: AdminUser;
/**
* Optional HTTP context (headers, IP, etc.).
*/
extra?: HttpExtra;
/**
* Optional additional attributes for the new record.
*/
recordAttributes?: Record<string, any>;
};