@@ -2,34 +2,14 @@ import React, { useCallback, useEffect, useMemo, useState } from 'react';
22// @ts -ignore
33import Uppy , { UploadResult } from '@uppy/core' ;
44// @ts -ignore
5- import AwsS3Multipart from '@uppy/aws-s3-multipart' ;
6- // @ts -ignore
75import { useFetch } from '@gitroom/helpers/utils/custom.fetch' ;
8-
9- import sha256 from 'sha256' ;
6+ import { getUppyUploadPlugin } from '@gitroom/react/helpers/uppy.upload' ;
107import { FileInput , ProgressBar } from '@uppy/react' ;
118
129// Uppy styles
1310import '@uppy/core/dist/style.min.css' ;
1411import '@uppy/dashboard/dist/style.min.css' ;
1512
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-
3313export function MultipartFileUploader ( {
3414 onUploadSuccess,
3515 allowedFileTypes,
@@ -41,7 +21,7 @@ export function MultipartFileUploader({
4121 const [ loaded , setLoaded ] = useState ( false ) ;
4222 const [ reload , setReload ] = useState ( false ) ;
4323
44- const onUploadSuccessExtended = useCallback ( ( result : UploadResult ) => {
24+ const onUploadSuccessExtended = useCallback ( ( result : UploadResult < any , any > ) => {
4525 setReload ( true ) ;
4626 onUploadSuccess ( result ) ;
4727 } , [ onUploadSuccess ] ) ;
@@ -78,7 +58,9 @@ export function MultipartFileUploaderAfter({
7858 onUploadSuccess : ( result : UploadResult ) => void ;
7959 allowedFileTypes : string ;
8060} ) {
61+ const storageProvider = process . env . NEXT_PUBLIC_STORAGE_PROVIDER || "local" ;
8162 const fetch = useFetch ( ) ;
63+
8264 const uppy = useMemo ( ( ) => {
8365 const uppy2 = new Uppy ( {
8466 autoProceed : true ,
@@ -87,38 +69,17 @@ export function MultipartFileUploaderAfter({
8769 allowedFileTypes : allowedFileTypes . split ( ',' ) ,
8870 maxFileSize : 1000000000 ,
8971 } ,
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- } ) ,
12172 } ) ;
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+ } ) ;
12283
12384 uppy2 . on ( 'complete' , ( result ) => {
12485 onUploadSuccess ( result ) ;
@@ -141,15 +102,17 @@ export function MultipartFileUploaderAfter({
141102
142103 return (
143104 < >
105+ { /* <Dashboard uppy={uppy} /> */ }
144106 < ProgressBar uppy = { uppy } />
145107 < FileInput
146108 uppy = { uppy }
147109 locale = { {
148110 strings : {
149111 chooseFiles : 'Upload' ,
150112 } ,
113+ pluralize : ( n ) => n
151114 } }
152- />
115+ />
153116 </ >
154117 ) ;
155118}
0 commit comments