-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
1099 lines (937 loc) · 36.8 KB
/
index.ts
File metadata and controls
1099 lines (937 loc) · 36.8 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
PluginOptions,
UploadFromBufferParams,
UploadFromBufferToExistingRecordParams,
CommitUrlToUpdateExistingRecordParams,
CommitUrlToNewRecordParams,
GetUploadUrlParams,
} from './types.js';
import { AdminForthPlugin, AdminForthResource, Filters, IAdminForth, IHttpServer, suggestIfTypo, RateLimiter } from "adminforth";
import { Readable } from "stream";
import { randomUUID } from "crypto";
import { interpretResource, ActionCheckSource } from 'adminforth';
const ADMINFORTH_NOT_YET_USED_TAG = 'adminforth-candidate-for-cleanup';
const jobs = new Map();
export default class UploadPlugin extends AdminForthPlugin {
options: PluginOptions;
adminforth!: IAdminForth;
totalCalls: number;
totalDuration: number;
resourceConfig: AdminForthResource;
rateLimiter: RateLimiter;
getFileDownloadUrl: ((path: string) => Promise<string>);
getFileUploadUrl: ( originalFilename, contentType, size, originalExtension, recordPk ) => Promise<{ uploadUrl: string, tagline?: string, filePath?: string, uploadExtraParams?: Record<string, string>, previewUrl?: string, error?: string } | {error: string}>;
constructor(options: PluginOptions) {
super(options, import.meta.url);
this.options = options;
// for calcualting average time
this.totalCalls = 0;
this.totalDuration = 0;
this.getFileDownloadUrl = async (path: string, expiresInSeconds: number = 1800) : Promise<string> => {
if (!path) {
return '';
}
return this.options.storageAdapter.getDownloadUrl(path, expiresInSeconds);
}
this.getFileUploadUrl = async ( originalFilename, contentType, size, originalExtension, recordPk ) : Promise<{ uploadUrl: string, tagline?: string, filePath?: string, uploadExtraParams?: Record<string, string>, previewUrl?: string, error?: string } | {error: string}> => {
if (this.options.allowedFileExtensions && !this.options.allowedFileExtensions.includes(originalExtension.toLowerCase())) {
return {
error: `File extension "${originalExtension}" is not allowed, allowed extensions are: ${this.options.allowedFileExtensions.join(', ')}`
};
}
let record = undefined;
if (recordPk) {
// get record by recordPk
const pkName = this.resourceConfig.columns.find((column: any) => column.primaryKey)?.name;
record = await this.adminforth.resource(this.resourceConfig.resourceId).get(
[Filters.EQ(pkName, recordPk)]
)
}
const filePath: string = this.options.filePath({ originalFilename, originalExtension, contentType, record });
if (filePath.startsWith('/')) {
throw new Error('s3Path should not start with /, please adjust s3path function to not return / at the start of the path');
}
const { uploadUrl, uploadExtraParams } = await this.options.storageAdapter.getUploadSignedUrl(filePath, contentType, 1800);
let previewUrl;
if (this.options.preview?.previewUrl) {
previewUrl = this.options.preview.previewUrl({ filePath });
} else {
previewUrl = await this.options.storageAdapter.getDownloadUrl(filePath, 1800);
}
const tagline = `${ADMINFORTH_NOT_YET_USED_TAG}=true`;
return {
uploadUrl,
tagline,
filePath,
uploadExtraParams,
previewUrl,
};
};
if (this.options.generation?.rateLimit?.limit) {
this.rateLimiter = new RateLimiter(this.options.generation.rateLimit?.limit)
}
}
private normalizePaths(value: any): string[] {
if (!value) return [];
if (Array.isArray(value)) return value.filter(Boolean).map(String);
return [String(value)];
}
private async callStorageAdapter(primaryMethod: string, fallbackMethod: string, filePath: string) {
const adapter: any = this.options.storageAdapter as any;
const fn = adapter?.[primaryMethod] ?? adapter?.[fallbackMethod];
if (typeof fn !== 'function') {
throw new Error(`Storage adapter is missing method "${primaryMethod}" (fallback "${fallbackMethod}")`);
}
await fn.call(adapter, filePath);
}
public markKeyForNotDeletion(filePath: string) {
return this.callStorageAdapter('markKeyForNotDeletion', 'markKeyForNotDeletation', filePath);
}
public markKeyForDeletion(filePath: string) {
return this.callStorageAdapter('markKeyForDeletion', 'markKeyForDeletation', filePath);
}
private async generateImages(jobId: string, prompt: string,requestAttachmentFiles: string[], recordId: any, adminUser: any, headers: any) {
if (this.options.generation.rateLimit?.limit) {
if (!await this.rateLimiter.consume(`${this.pluginInstanceId}-${this.adminforth.auth.getClientIp(headers)}`)) {
jobs.set(jobId, { status: "failed", error: this.options.generation.rateLimit.errorMessage });
return { error: this.options.generation.rateLimit.errorMessage };
}
}
let attachmentFiles = requestAttachmentFiles;
let error: string | undefined = undefined;
const STUB_MODE = false;
const images = await Promise.all(
(new Array(this.options.generation.countToGenerate)).fill(0).map(async () => {
if (STUB_MODE) {
await new Promise((resolve) => setTimeout(resolve, 2000));
return `https://picsum.photos/200/300?random=${Math.floor(Math.random() * 1000)}`;
}
const start = +new Date();
let resp;
try {
resp = await this.options.generation.adapter.generate(
{
prompt,
inputFiles: attachmentFiles,
n: 1,
size: this.options.generation.outputSize,
}
)
} catch (e: any) {
error = `No response from image generation provider: ${e.message}. Please check your prompt or try again later.`;
return;
}
if (resp.error) {
console.error('Error generating image', resp.error);
error = resp.error;
return;
}
this.totalCalls++;
this.totalDuration += (+new Date() - start) / 1000;
return resp.imageURLs[0]
})
);
jobs.set(jobId, { status: "completed", images, error });
return { ok: true };
};
instanceUniqueRepresentation(pluginOptions: any) : string {
return `${pluginOptions.pathColumnName}`;
}
async setupLifecycleRule() {
const adapterUserUniqueRepresentation = `${this.resourceConfig.resourceId}-${this.pluginInstanceId}`;
this.options.storageAdapter.setupLifecycle(adapterUserUniqueRepresentation);
}
async genPreviewUrl(record: any) {
const value = record?.[this.options.pathColumnName];
const paths = this.normalizePaths(value);
if (!paths.length) return;
const makeUrl = async (filePath: string) => {
if (this.options.preview?.previewUrl) {
return this.options.preview.previewUrl({ filePath });
}
return await this.options.storageAdapter.getDownloadUrl(filePath, 1800);
};
const urls = await Promise.all(paths.map(makeUrl));
record[`previewUrl_${this.pluginInstanceId}`] = Array.isArray(value) ? urls : urls[0];
}
async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
super.modifyResourceConfig(adminforth, resourceConfig);
this.resourceConfig = resourceConfig;
// after column to store the path of the uploaded file, add new VirtualColumn,
// show only in edit and create views
// use component uploader.vue
const { pathColumnName } = this.options;
const pathColumnIndex = resourceConfig.columns.findIndex((column: any) => column.name === pathColumnName);
if (pathColumnIndex === -1) {
throw new Error(`Column with name "${pathColumnName}" not found in resource "${resourceConfig.label}"`);
}
const pluginFrontendOptions = {
allowedExtensions: this.options.allowedFileExtensions,
maxFileSize: this.options.maxFileSize,
pluginInstanceId: this.pluginInstanceId,
resourceLabel: resourceConfig.label,
generateImages: this.options.generation ? true : false,
pathColumnLabel: resourceConfig.columns[pathColumnIndex].label,
maxWidth: this.options.preview?.maxWidth,
maxListWidth: this.options.preview?.maxListWidth,
maxShowWidth: this.options.preview?.maxShowWidth,
minWidth: this.options.preview?.minWidth,
minListWidth: this.options.preview?.minListWidth,
minShowWidth: this.options.preview?.minShowWidth,
generationPrompt: this.options.generation?.generationPrompt,
recorPkFieldName: this.resourceConfig.columns.find((column: any) => column.primaryKey)?.name,
pathColumnName: this.options.pathColumnName,
};
// define components which will be imported from other components
this.componentPath('imageGenerator.vue');
if (!resourceConfig.columns[pathColumnIndex].components) {
resourceConfig.columns[pathColumnIndex].components = {};
}
const pathColumn = resourceConfig.columns[pathColumnIndex];
// add preview column to list
if (this.options.preview?.usePreviewComponents !== false) {
resourceConfig.columns[pathColumnIndex].components.list = {
file: this.componentPath('preview.vue'),
meta: pluginFrontendOptions,
};
resourceConfig.columns[pathColumnIndex].components.show = {
file: this.componentPath('preview.vue'),
meta: pluginFrontendOptions,
};
}
resourceConfig.columns[pathColumnIndex].components.create = {
file: this.componentPath('uploader.vue'),
meta: pluginFrontendOptions,
}
resourceConfig.columns[pathColumnIndex].components.edit = {
file: this.componentPath('uploader.vue'),
meta: pluginFrontendOptions,
}
// ** HOOKS FOR CREATE **//
// in afterSave hook, aremove tag adminforth-not-yet-used from the file
resourceConfig.hooks.create.afterSave.push(async ({ record }: { record: any }) => {
process.env.HEAVY_DEBUG && console.log('💾💾 after save ', record?.id);
const paths = this.normalizePaths(record?.[pathColumnName]);
await Promise.all(paths.map(async (p) => {
process.env.HEAVY_DEBUG && console.log('🪥🪥 remove ObjectTagging', p);
// let it crash if it fails: this is a new file which just was uploaded.
await this.markKeyForNotDeletion(p);
}));
return { ok: true };
});
// ** HOOKS FOR SHOW **//
// add show hook to get presigned URL
if (pathColumn.showIn.show) {
resourceConfig.hooks.show.afterDatasourceResponse.push(async ({ response }: { response: any }) => {
const record = response[0];
if (!record) {
return { ok: true };
}
if (record[pathColumnName]) {
await this.genPreviewUrl(record)
}
return { ok: true };
});
}
// ** HOOKS FOR LIST **//
if (pathColumn.showIn.list) {
resourceConfig.hooks.list.afterDatasourceResponse.push(async ({ response }: { response: any }) => {
await Promise.all(response.map(async (record: any) => {
if (record[this.options.pathColumnName]) {
await this.genPreviewUrl(record)
}
}));
return { ok: true };
})
}
// ** HOOKS FOR DELETE **//
// add delete hook which sets tag adminforth-candidate-for-cleanup to true
resourceConfig.hooks.delete.afterSave.push(async ({ record }: { record: any }) => {
const paths = this.normalizePaths(record?.[pathColumnName]);
await Promise.all(paths.map(async (p) => {
try {
await this.markKeyForDeletion(p);
} catch (e) {
// file might be e.g. already deleted, so we catch error
console.error(`Error setting tag ${ADMINFORTH_NOT_YET_USED_TAG} to true for object ${p}. File will not be auto-cleaned up`, e);
}
}));
return { ok: true };
});
// ** HOOKS FOR EDIT **//
// add edit postSave hook to delete old file and remove tag from new file
resourceConfig.hooks.edit.afterSave.push(async ({ updates, oldRecord }: { updates: any, oldRecord: any }) => {
if (updates[pathColumnName] || updates[pathColumnName] === null) {
const oldValue = oldRecord?.[pathColumnName];
const newValue = updates?.[pathColumnName];
const oldPaths = this.normalizePaths(oldValue);
const newPaths = newValue === null ? [] : this.normalizePaths(newValue);
const oldSet = new Set(oldPaths);
const newSet = new Set(newPaths);
const toDelete = oldPaths.filter((p) => !newSet.has(p));
const toKeep = newPaths.filter((p) => !oldSet.has(p));
await Promise.all(toDelete.map(async (p) => {
// put tag to delete old file
try {
await this.markKeyForDeletion(p);
} catch (e) {
// file might be e.g. already deleted, so we catch error
console.error(`Error setting tag ${ADMINFORTH_NOT_YET_USED_TAG} to true for object ${p}. File will not be auto-cleaned up`, e);
}
}));
await Promise.all(toKeep.map(async (p) => {
// remove tag from new file
// in this case we let it crash if it fails: this is a new file which just was uploaded.
await this.markKeyForNotDeletion(p);
}));
}
return { ok: true };
});
}
validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: any) {
this.adminforth = adminforth;
if (this.options.generation) {
const template = this.options.generation?.generationPrompt;
const regex = /{{(.*?)}}/g;
const matches = template.match(regex);
if (matches) {
matches.forEach((match) => {
const field = match.replace(/{{|}}/g, '').trim();
if (!resourceConfig.columns.find((column: any) => column.name === field)) {
const similar = suggestIfTypo(resourceConfig.columns.map((column: any) => column.name), field);
throw new Error(`Field "${field}" specified in generationPrompt not found in resource "${resourceConfig.label}". ${similar ? `Did you mean "${similar}"?` : ''}`);
} else {
let column = resourceConfig.columns.find((column: any) => column.name === field);
if (column.backendOnly === true) {
throw new Error(`Field "${field}" specified in generationPrompt is marked as backendOnly in resource "${resourceConfig.label}". Please remove backendOnly or choose another field.`);
}
}
});
}
}
// called here because modifyResourceConfig can be called in build time where there is no environment and AWS secrets
this.setupLifecycleRule();
}
setupEndpoints(server: IHttpServer) {
server.endpoint({
method: 'GET',
path: `/plugin/${this.pluginInstanceId}/averageDuration`,
handler: async () => {
return {
totalCalls: this.totalCalls,
totalDuration: this.totalDuration,
averageDuration: this.totalCalls ? this.totalDuration / this.totalCalls : null,
};
}
});
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/get_file_upload_url`,
handler: async ({ body }) => {
const { originalFilename, contentType, size, originalExtension, recordPk } = body;
return this.getFileUploadUrl( originalFilename, contentType, size, originalExtension, recordPk );
}
});
// generation: {
// provider: 'openai-dall-e',
// countToGenerate: 3,
// openAiOptions: {
// model: 'dall-e-3',
// size: '1792x1024',
// apiKey: process.env.OPENAI_API_KEY as string,
// },
// },
// curl https://api.openai.com/v1/images/generations \
// -H "Content-Type: application/json" \
// -H "Authorization: Bearer $OPENAI_API_KEY" \
// -d '{
// "model": "dall-e-3",
// "prompt": "A cute baby sea otter",
// "n": 1,
// "size": "1024x1024"
// }'
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/create-image-generation-job`,
handler: async ({ body, adminUser, headers }) => {
const { prompt, recordId, requestAttachmentFiles } = body;
const jobId = randomUUID();
jobs.set(jobId, { status: "in_progress" });
this.generateImages(jobId, prompt, requestAttachmentFiles, recordId, adminUser, headers);
setTimeout(() => jobs.delete(jobId), 1_800_000);
setTimeout(() => {jobs.set(jobId, { status: "timeout" });}, 300_000);
return { ok: true, jobId };
}
});
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/get-image-generation-job-status`,
handler: async ({ body, adminUser, headers }) => {
const jobId = body.jobId;
if (!jobId) {
return { error: "Can't find job id" };
}
const job = jobs.get(jobId);
if (!job) {
return { error: "Job not found" };
}
return { ok: true, job };
}
});
server.endpoint({
method: 'GET',
path: `/plugin/${this.pluginInstanceId}/cors-proxy`,
handler: async ({ query, response }) => {
const { url } = query;
const resp = await fetch(url);
response.setHeader('Content-Type', resp.headers.get('Content-Type'));
//@ts-ignore
Readable.fromWeb( resp.body ).pipe( response.blobStream() );
return null
}
});
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/get_attachment_files`,
handler: async ({ body, adminUser }) => {
const { recordId } = body;
if (!recordId) return { error: 'Missing recordId' };
const record = await this.adminforth.resource(this.resourceConfig.resourceId).get([
Filters.EQ(this.resourceConfig.columns.find((col: any) => col.primaryKey)?.name, recordId),
]);
if (!record) return { error: 'Record not found' };
if (!this.options.generation.attachFiles) return { files: [] };
const files = await this.options.generation.attachFiles({ record, adminUser });
return {
files: Array.isArray(files) ? files : [files],
};
},
});
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/get-file-download-url`,
handler: async ({ body, adminUser }) => {
const { filePath } = body;
if (!filePath) {
return { error: 'Missing filePath' };
}
const allowedActions = await interpretResource( adminUser, this.resourceConfig, '', ActionCheckSource.CustomActionRequest, this.adminforth )
if (allowedActions.allowedActions.create === true || allowedActions.allowedActions.edit === true) {
const url = await this.options.storageAdapter.getDownloadUrl(filePath, 1800);
return {
url,
};
}
return { error: 'You do not have permission to download this file' };
},
});
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/get-file-preview-url`,
handler: async ({ body, adminUser }) => {
const { filePath } = body;
if (!filePath) {
return { error: 'Missing filePath' };
}
if (this.options.preview?.previewUrl) {
const url = this.options.preview.previewUrl({ filePath });
return { url };
}
return { error: 'failed to generate preview URL' };
},
});
}
/*
* Uploads a file from a buffer, creates a record in the resource, and returns the file path and preview URL.
*/
async uploadFromBufferToNewRecord({
filename,
contentType,
buffer,
adminUser,
extra,
recordAttributes,
}: UploadFromBufferParams): Promise<{ path: string; previewUrl: string; newRecordPk: any }> {
if (!filename || !contentType || !buffer) {
throw new Error('filename, contentType and buffer are required');
}
const lastDotIndex = filename.lastIndexOf('.');
if (lastDotIndex === -1) {
throw new Error('filename must contain an extension');
}
const originalExtension = filename.substring(lastDotIndex + 1).toLowerCase();
const originalFilename = filename.substring(0, lastDotIndex);
if (this.options.allowedFileExtensions && !this.options.allowedFileExtensions.includes(originalExtension)) {
throw new Error(
`File extension "${originalExtension}" is not allowed, allowed extensions are: ${this.options.allowedFileExtensions.join(', ')}`
);
}
let nodeBuffer: Buffer;
if (Buffer.isBuffer(buffer)) {
nodeBuffer = buffer;
} else if (buffer instanceof ArrayBuffer) {
nodeBuffer = Buffer.from(buffer);
} else if (ArrayBuffer.isView(buffer)) {
nodeBuffer = Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength);
} else {
throw new Error('Unsupported buffer type');
}
const size = nodeBuffer.byteLength;
if (this.options.maxFileSize && size > this.options.maxFileSize) {
throw new Error(
`File size ${size} is too large. Maximum allowed size is ${this.options.maxFileSize}`
);
}
const filePath: string = this.options.filePath({
originalFilename,
originalExtension,
contentType,
record: undefined,
});
if (filePath.startsWith('/')) {
throw new Error('s3Path should not start with /, please adjust s3path function to not return / at the start of the path');
}
const { uploadUrl, uploadExtraParams } = await this.options.storageAdapter.getUploadSignedUrl(
filePath,
contentType,
1800,
);
const headers: Record<string, string> = {
'Content-Type': contentType,
};
if (uploadExtraParams) {
Object.entries(uploadExtraParams).forEach(([key, value]) => {
headers[key] = value as string;
});
}
const resp = await fetch(uploadUrl as any, {
method: 'PUT',
headers,
body: nodeBuffer as any,
});
if (!resp.ok) {
let bodyText = '';
try {
bodyText = await resp.text();
} catch (e) {
// ignore
}
throw new Error(`Upload failed with status ${resp.status}: ${bodyText}`);
}
await this.markKeyForNotDeletion(filePath);
if (!this.resourceConfig) {
throw new Error('resourceConfig is not initialized yet');
}
const { error: createError, createdRecord, newRecordId }: any = await this.adminforth.createResourceRecord({
resource: this.resourceConfig,
record: { ...(recordAttributes ?? {}), [this.options.pathColumnName]: filePath },
adminUser,
extra,
});
if (createError) {
try {
await this.markKeyForDeletion(filePath);
} catch (e) {
// best-effort cleanup, ignore error
}
throw new Error(`Error creating record after upload: ${createError}`);
}
const pkColumn = this.resourceConfig.columns.find((column: any) => column.primaryKey);
const pkName = pkColumn?.name;
const newRecordPk = newRecordId ?? (pkName && createdRecord ? createdRecord[pkName] : undefined);
let previewUrl: string;
if (this.options.preview?.previewUrl) {
previewUrl = this.options.preview.previewUrl({ filePath });
} else {
previewUrl = await this.options.storageAdapter.getDownloadUrl(filePath, 1800);
}
return {
path: filePath,
previewUrl,
newRecordPk,
};
}
/*
* Uploads a file from a buffer and updates an existing record's path column.
* If the newly generated storage path would be the same as the current path,
* throws an error to avoid potential caching issues.
*/
async uploadFromBufferToExistingRecord({
recordId,
filename,
contentType,
buffer,
adminUser,
extra,
}: UploadFromBufferToExistingRecordParams): Promise<{ path: string; previewUrl: string }> {
if (recordId === undefined || recordId === null) {
throw new Error('recordId is required');
}
if (!filename || !contentType || !buffer) {
throw new Error('filename, contentType and buffer are required');
}
if (!this.resourceConfig) {
throw new Error('resourceConfig is not initialized yet');
}
const pkColumn = this.resourceConfig.columns.find((column: any) => column.primaryKey);
const pkName = pkColumn?.name;
if (!pkName) {
throw new Error('Primary key column not found in resource configuration');
}
const existingRecord = await this.adminforth
.resource(this.resourceConfig.resourceId)
.get([Filters.EQ(pkName, recordId)]);
if (!existingRecord) {
throw new Error(`Record with id ${recordId} not found`);
}
const lastDotIndex = filename.lastIndexOf('.');
if (lastDotIndex === -1) {
throw new Error('filename must contain an extension');
}
const originalExtension = filename.substring(lastDotIndex + 1).toLowerCase();
const originalFilename = filename.substring(0, lastDotIndex);
if (this.options.allowedFileExtensions && !this.options.allowedFileExtensions.includes(originalExtension)) {
throw new Error(
`File extension "${originalExtension}" is not allowed, allowed extensions are: ${this.options.allowedFileExtensions.join(', ')}`
);
}
let nodeBuffer: Buffer;
if (Buffer.isBuffer(buffer)) {
nodeBuffer = buffer;
} else if (buffer instanceof ArrayBuffer) {
nodeBuffer = Buffer.from(buffer);
} else if (ArrayBuffer.isView(buffer)) {
nodeBuffer = Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength);
} else {
throw new Error('Unsupported buffer type');
}
const size = nodeBuffer.byteLength;
if (this.options.maxFileSize && size > this.options.maxFileSize) {
throw new Error(
`File size ${size} is too large. Maximum allowed size is ${this.options.maxFileSize}`
);
}
const existingValue = existingRecord[this.options.pathColumnName];
const existingPaths = this.normalizePaths(existingValue);
const filePath: string = this.options.filePath({
originalFilename,
originalExtension,
contentType,
record: existingRecord,
});
if (filePath.startsWith('/')) {
throw new Error('s3Path should not start with /, please adjust s3path function to not return / at the start of the path');
}
if (existingPaths.includes(filePath)) {
throw new Error('New file path cannot be the same as existing path to avoid caching issues');
}
const { uploadUrl, uploadExtraParams } = await this.options.storageAdapter.getUploadSignedUrl(
filePath,
contentType,
1800,
);
const headers: Record<string, string> = {
'Content-Type': contentType,
};
if (uploadExtraParams) {
Object.entries(uploadExtraParams).forEach(([key, value]) => {
headers[key] = value as string;
});
}
const resp = await fetch(uploadUrl as any, {
method: 'PUT',
headers,
body: nodeBuffer as any,
});
if (!resp.ok) {
let bodyText = '';
try {
bodyText = await resp.text();
} catch (e) {
// ignore
}
throw new Error(`Upload failed with status ${resp.status}: ${bodyText}`);
}
const { error: updateError } = await this.adminforth.updateResourceRecord({
resource: this.resourceConfig,
recordId,
oldRecord: existingRecord,
adminUser,
extra,
updates: {
[this.options.pathColumnName]: filePath,
},
} as any);
if (updateError) {
try {
await this.markKeyForDeletion(filePath);
} catch (e) {
// best-effort cleanup, ignore error
}
throw new Error(`Error updating record after upload: ${updateError}`);
}
let previewUrl: string;
if (this.options.preview?.previewUrl) {
previewUrl = this.options.preview.previewUrl({ filePath });
} else {
previewUrl = await this.options.storageAdapter.getDownloadUrl(filePath, 1800);
}
return {
path: filePath,
previewUrl,
};
}
/**
* Generates a new signed upload URL for future uploading from the frontend via a direct upload (e.g. using fetch + FormData).
*
* After the upload, file still will be marked for auto-deletion after short time, so to keep it permanently,
* you need to either:
* * Use commitUrlToExistingRecord to commit the URL to an existing record. This will replace the path in the existing record and will do a cleanup of the old
* file pointed in this path column.
* * If you want to create a new record with this URL, you can call commitUrlToNewRecord, which will create a new record and set the path column to the uploaded file path.
* * Write URL to special field called pathColumnName so afterSave hook installed by the plugin will automatically mark as not candidate for auto-deletion
*
* ```ts
* const file = input.files[0];
*
* // 1) Ask your backend to call getUploadUrlForExistingRecord
* const { uploadUrl, filePath, uploadExtraParams } = await fetch('/api/uploads/get-url-existing', {
* method: 'POST',
* headers: { 'Content-Type': 'application/json' },
* body: JSON.stringify({
* recordId,
* filename: file.name,
* contentType: file.type,
* size: file.size,
* }),
* }).then(r => r.json());
*
* const formData = new FormData();
* if (uploadExtraParams) {
* Object.entries(uploadExtraParams).forEach(([key, value]) => {
* formData.append(key, value as string);
* });
* }
* formData.append('file', file);
*
* // 2) Direct upload from the browser to storage (multipart/form-data)
* const uploadResp = await fetch(uploadUrl, {
* method: 'POST',
* body: formData,
* });
* if (!uploadResp.ok) {
* throw new Error('Upload failed');
* }
*
* // 3) Tell your backend to commit the URL to the record e.g. from rest API call
* await fetch('/api/uploads/commit-existing', {
* method: 'POST',
* headers: { 'Content-Type': 'application/json' },
* body: JSON.stringify({ recordId, filePath }),
* });
* ```
*/
async getUploadUrl({
recordId,
filename,
contentType,
size,
}: GetUploadUrlParams): Promise<{
uploadUrl: string;
filePath: string;
uploadExtraParams?: Record<string, string>;
pathColumnName: string;
}> {
if (!filename || !contentType) {
throw new Error('filename and contentType are required');
}
if (!this.resourceConfig) {
throw new Error('resourceConfig is not initialized yet');
}
const pkColumn = this.resourceConfig.columns.find((column: any) => column.primaryKey);
const pkName = pkColumn?.name;
if (!pkName) {
throw new Error('Primary key column not found in resource configuration');
}
let existingRecord: any = undefined;
if (recordId !== undefined && recordId !== null) {
existingRecord = await this.adminforth
.resource(this.resourceConfig.resourceId)
.get([Filters.EQ(pkName, recordId)]);
if (!existingRecord) {
throw new Error(`Record with id ${recordId} not found`);
}
}
const lastDotIndex = filename.lastIndexOf('.');
if (lastDotIndex === -1) {
throw new Error('filename must contain an extension');
}
const originalExtension = filename.substring(lastDotIndex + 1).toLowerCase();
const originalFilename = filename.substring(0, lastDotIndex);
if (
this.options.allowedFileExtensions &&
!this.options.allowedFileExtensions.includes(originalExtension)
) {
throw new Error(
`File extension "${originalExtension}" is not allowed, allowed extensions are: ${this.options.allowedFileExtensions.join(
', ',
)}`,
);
}
if (size != null && this.options.maxFileSize && size > this.options.maxFileSize) {
throw new Error(
`File size ${size} is too large. Maximum allowed size is ${this.options.maxFileSize}`,
);
}
const existingValue = existingRecord?.[this.options.pathColumnName];
const existingPaths = existingValue ? this.normalizePaths(existingValue) : undefined;
const filePath: string = this.options.filePath({
originalFilename,
originalExtension,
contentType,
record: existingRecord,
});
if (filePath.startsWith('/')) {
throw new Error(
's3Path should not start with /, please adjust s3path function to not return / at the start of the path',
);
}
if (existingPaths && existingPaths.includes(filePath)) {
throw new Error(
'New file path cannot be the same as existing path to avoid caching issues',
);
}
const { uploadUrl, uploadExtraParams } = await this.options.storageAdapter.getUploadSignedUrl(
filePath,
contentType,
1800,
);
return {
uploadUrl,
filePath,
uploadExtraParams,
pathColumnName: this.options.pathColumnName,
};
}
/**
* Commits a previously generated upload URL to an existing record.
*
* Never call this method from edit afterSave and beforeSave hooks of the same resource,
* as it would create infinite loop of record updates.
* You should call this method from your own custom API endpoint after the upload is done.
*/
async commitUrlToUpdateExistingRecord({
recordId,
filePath,
adminUser,
extra,
}: CommitUrlToUpdateExistingRecordParams): Promise<{ path: string; previewUrl: string }> {
if (recordId === undefined || recordId === null) {
throw new Error('recordId is required');
}
if (!filePath) {
throw new Error('filePath is required');
}
if (!this.resourceConfig) {
throw new Error('resourceConfig is not initialized yet');
}
const pkColumn = this.resourceConfig.columns.find((column: any) => column.primaryKey);
const pkName = pkColumn?.name;
if (!pkName) {
throw new Error('Primary key column not found in resource configuration');
}
const existingRecord = await this.adminforth
.resource(this.resourceConfig.resourceId)
.get([Filters.EQ(pkName, recordId)]);
if (!existingRecord) {
throw new Error(`Record with id ${recordId} not found`);
}
const existingValue = existingRecord[this.options.pathColumnName];
const existingPaths = this.normalizePaths(existingValue);