@@ -2,34 +2,14 @@ import React, { useCallback, useEffect, useMemo, useState } from 'react';
2
2
// @ts -ignore
3
3
import Uppy , { UploadResult } from '@uppy/core' ;
4
4
// @ts -ignore
5
- import AwsS3Multipart from '@uppy/aws-s3-multipart' ;
6
- // @ts -ignore
7
5
import { useFetch } from '@gitroom/helpers/utils/custom.fetch' ;
8
-
9
- import sha256 from 'sha256' ;
6
+ import { getUppyUploadPlugin } from '@gitroom/react/helpers/uppy.upload' ;
10
7
import { FileInput , ProgressBar } from '@uppy/react' ;
11
8
12
9
// Uppy styles
13
10
import '@uppy/core/dist/style.min.css' ;
14
11
import '@uppy/dashboard/dist/style.min.css' ;
15
12
16
- const fetchUploadApiEndpoint = async (
17
- fetch : any ,
18
- endpoint : string ,
19
- data : any
20
- ) => {
21
- const res = await fetch ( `/media/${ endpoint } ` , {
22
- method : 'POST' ,
23
- body : JSON . stringify ( data ) ,
24
- headers : {
25
- accept : 'application/json' ,
26
- 'Content-Type' : 'application/json' ,
27
- } ,
28
- } ) ;
29
-
30
- return res . json ( ) ;
31
- } ;
32
-
33
13
export function MultipartFileUploader ( {
34
14
onUploadSuccess,
35
15
allowedFileTypes,
@@ -41,7 +21,7 @@ export function MultipartFileUploader({
41
21
const [ loaded , setLoaded ] = useState ( false ) ;
42
22
const [ reload , setReload ] = useState ( false ) ;
43
23
44
- const onUploadSuccessExtended = useCallback ( ( result : UploadResult ) => {
24
+ const onUploadSuccessExtended = useCallback ( ( result : UploadResult < any , any > ) => {
45
25
setReload ( true ) ;
46
26
onUploadSuccess ( result ) ;
47
27
} , [ onUploadSuccess ] ) ;
@@ -78,7 +58,9 @@ export function MultipartFileUploaderAfter({
78
58
onUploadSuccess : ( result : UploadResult ) => void ;
79
59
allowedFileTypes : string ;
80
60
} ) {
61
+ const storageProvider = process . env . NEXT_PUBLIC_STORAGE_PROVIDER || "local" ;
81
62
const fetch = useFetch ( ) ;
63
+
82
64
const uppy = useMemo ( ( ) => {
83
65
const uppy2 = new Uppy ( {
84
66
autoProceed : true ,
@@ -87,38 +69,17 @@ export function MultipartFileUploaderAfter({
87
69
allowedFileTypes : allowedFileTypes . split ( ',' ) ,
88
70
maxFileSize : 1000000000 ,
89
71
} ,
90
- } ) . use ( AwsS3Multipart , {
91
- // @ts -ignore
92
- createMultipartUpload : async ( file ) => {
93
- const arrayBuffer = await new Response ( file . data ) . arrayBuffer ( ) ;
94
- // @ts -ignore
95
- const fileHash = await sha256 ( arrayBuffer ) ;
96
- const contentType = file . type ;
97
- return fetchUploadApiEndpoint ( fetch , 'create-multipart-upload' , {
98
- file,
99
- fileHash,
100
- contentType,
101
- } ) ;
102
- } ,
103
- // @ts -ignore
104
- listParts : ( file , props ) =>
105
- fetchUploadApiEndpoint ( fetch , 'list-parts' , { file, ...props } ) ,
106
- // @ts -ignore
107
- signPart : ( file , props ) =>
108
- fetchUploadApiEndpoint ( fetch , 'sign-part' , { file, ...props } ) ,
109
- // @ts -ignore
110
- abortMultipartUpload : ( file , props ) =>
111
- fetchUploadApiEndpoint ( fetch , 'abort-multipart-upload' , {
112
- file,
113
- ...props ,
114
- } ) ,
115
- // @ts -ignore
116
- completeMultipartUpload : ( file , props ) =>
117
- fetchUploadApiEndpoint ( fetch , 'complete-multipart-upload' , {
118
- file,
119
- ...props ,
120
- } ) ,
121
72
} ) ;
73
+
74
+ const { plugin, options } = getUppyUploadPlugin ( storageProvider , fetch )
75
+ uppy2 . use ( plugin , options )
76
+ // Set additional metadata when a file is added
77
+ uppy2 . on ( 'file-added' , ( file ) => {
78
+ uppy2 . setFileMeta ( file . id , {
79
+ useCloudflare : storageProvider === 'cloudflare' ? 'true' : 'false' , // Example of adding a custom field
80
+ // Add more fields as needed
81
+ } ) ;
82
+ } ) ;
122
83
123
84
uppy2 . on ( 'complete' , ( result ) => {
124
85
onUploadSuccess ( result ) ;
@@ -141,15 +102,17 @@ export function MultipartFileUploaderAfter({
141
102
142
103
return (
143
104
< >
105
+ { /* <Dashboard uppy={uppy} /> */ }
144
106
< ProgressBar uppy = { uppy } />
145
107
< FileInput
146
108
uppy = { uppy }
147
109
locale = { {
148
110
strings : {
149
111
chooseFiles : 'Upload' ,
150
112
} ,
113
+ pluralize : ( n ) => n
151
114
} }
152
- />
115
+ />
153
116
</ >
154
117
) ;
155
118
}
0 commit comments