11import { FILEREAD_CHUNK_SIZE } from "./Constants" ;
22import Lang from "./Lang" ;
3- import { ReadableStream , WritableStream } from ' web-streams-polyfill' ;
3+ import { ReadableStream , WritableStream } from " web-streams-polyfill" ;
44
55export const foo = 1 ;
66
77interface FileState {
8- token : string ,
9- uuid : string ,
8+ token : string ;
9+ uuid : string ;
1010}
1111
12- const baseurl = "http://localhost" ;
12+ const baseurl = "" ; // " http://localhost:3000 ";
1313
1414export function createFileReadable ( file : File ) : ReadableStream {
1515 let offset = 0 ;
1616 const queuingStrategy = new CountQueuingStrategy ( { highWaterMark : 1 } ) ;
1717
18- return new ReadableStream ( {
19- async pull ( cntrl ) {
20- if ( cntrl . desiredSize !== null && cntrl . desiredSize <= 0 ) {
21- return ;
22- }
23- const read = await file
24- . slice ( offset , offset + FILEREAD_CHUNK_SIZE )
25- . arrayBuffer ( ) ;
26-
27- if ( read . byteLength === 0 ) {
28- return cntrl . close ( ) ;
29- }
30- offset += FILEREAD_CHUNK_SIZE ;
31- cntrl . enqueue ( new Uint8Array ( read ) ) ;
18+ return new ReadableStream (
19+ {
20+ async pull ( cntrl ) {
21+ if ( cntrl . desiredSize !== null && cntrl . desiredSize <= 0 ) {
22+ return ;
23+ }
24+ const read = await file
25+ . slice ( offset , offset + FILEREAD_CHUNK_SIZE )
26+ . arrayBuffer ( ) ;
27+
28+ if ( read . byteLength === 0 ) {
29+ return cntrl . close ( ) ;
30+ }
31+ offset += FILEREAD_CHUNK_SIZE ;
32+ cntrl . enqueue ( new Uint8Array ( read ) ) ;
33+ } ,
3234 } ,
33- } ,
34- queuingStrategy ) ;
35+ queuingStrategy
36+ ) ;
3537}
3638
3739async function initFile (
@@ -45,85 +47,99 @@ async function initFile(
4547 signal : abortSignal ,
4648 method : "POST" ,
4749 headers : {
48- "Content-Type" : "application/json"
50+ "Content-Type" : "application/json" ,
4951 } ,
5052 body : JSON . stringify ( {
51- sender : sender ,
53+ sender : sender ,
5254 recipient : recipient ,
5355 mailContent : mailContent ,
54- mailLang : lang
55- } )
56+ mailLang : lang ,
57+ } ) ,
5658 } ) ;
5759
58-
5960 if ( response . status !== 200 ) {
6061 const errorText = await response . text ( ) ;
61- throw new Error ( `Error occured while initializing file. status: ${ response . status } , msg: ${ errorText } ` ) ;
62+ throw new Error (
63+ `Error occured while initializing file. status: ${ response . status } , msg: ${ errorText } `
64+ ) ;
6265 }
6366
6467 const resJson = await response . json ( ) ;
6568 const token = response . headers . get ( "cryptifytoken" ) as string ;
6669 return {
6770 token : token ,
68- uuid : resJson [ "uuid" ]
71+ uuid : resJson [ "uuid" ] ,
6972 } ;
7073}
7174
7275async function storeChunk (
7376 abortSignal : AbortSignal ,
7477 state : FileState ,
7578 chunk : Uint8Array ,
76- offset : number ,
79+ offset : number
7780) : Promise < FileState > {
7881 const response = await fetch ( `${ baseurl } /fileupload/${ state . uuid } ` , {
7982 signal : abortSignal ,
8083 method : "PUT" ,
8184 headers : {
82- " cryptifytoken" : state . token ,
85+ cryptifytoken : state . token ,
8386 "Content-Type" : "application/octet-stream" ,
84- "content-range" : `bytes ${ offset } -${ offset + chunk . length } /*`
87+ "content-range" : `bytes ${ offset } -${ offset + chunk . length } /*` ,
8588 } ,
86- body : new Blob ( [ chunk ] )
89+ body : new Blob ( [ chunk ] ) ,
8790 } ) ;
8891
8992 if ( response . status !== 200 ) {
9093 const errorText = await response . text ( ) ;
91- throw new Error ( `Error occured while uploading chunk. status: ${ response . status } , msg: ${ errorText } ` ) ;
94+ throw new Error (
95+ `Error occured while uploading chunk. status: ${ response . status } , msg: ${ errorText } `
96+ ) ;
9297 }
9398
9499 const token = response . headers . get ( "cryptifytoken" ) as string ;
95100
96101 return {
97102 token : token ,
98- uuid : state . uuid
103+ uuid : state . uuid ,
99104 } ;
100105}
101106
102- async function finalize ( abortSignal : AbortSignal , state : FileState , size : number ) : Promise < void > {
107+ async function finalize (
108+ abortSignal : AbortSignal ,
109+ state : FileState ,
110+ size : number
111+ ) : Promise < void > {
103112 const response = await fetch ( `${ baseurl } /fileupload/finalize/${ state . uuid } ` , {
104113 signal : abortSignal ,
105114 method : "POST" ,
106115 headers : {
107- " cryptifytoken" : state . token ,
108- "content-range" : `bytes */${ size } `
109- }
116+ cryptifytoken : state . token ,
117+ "content-range" : `bytes */${ size } ` ,
118+ } ,
110119 } ) ;
111120
112121 if ( response . status !== 200 ) {
113- const errorText = await response . text ( )
114- throw new Error ( `Error occured while finalizing file upload. status: ${ response . status } , body: ${ errorText } ` ) ;
122+ const errorText = await response . text ( ) ;
123+ throw new Error (
124+ `Error occured while finalizing file upload. status: ${ response . status } , body: ${ errorText } `
125+ ) ;
115126 }
116127}
117128
118- export async function getFileLoadStream ( abortSignal : AbortSignal , uuid : string ) : Promise < [ number , ReadableStream < Uint8Array > ] > {
129+ export async function getFileLoadStream (
130+ abortSignal : AbortSignal ,
131+ uuid : string
132+ ) : Promise < [ number , ReadableStream < Uint8Array > ] > {
119133 const response = await fetch ( `${ baseurl } /filedownload/${ uuid } ` , {
120134 signal : abortSignal ,
121135 method : "GET" ,
122136 } ) ;
123137
124138 if ( response . status !== 200 ) {
125- const errorText = await response . text ( )
126- throw new Error ( `Error occured while fetching file. status: ${ response . status } , body: ${ errorText } ` ) ;
139+ const errorText = await response . text ( ) ;
140+ throw new Error (
141+ `Error occured while fetching file. status: ${ response . status } , body: ${ errorText } `
142+ ) ;
127143 }
128144
129145 const filesize = parseInt ( response . headers . get ( "content-length" ) as string ) ;
@@ -144,38 +160,45 @@ export function getFileStoreStream(
144160) : WritableStream < Uint8Array > {
145161 let state : FileState = {
146162 token : "" ,
147- uuid : ""
148- }
163+ uuid : "" ,
164+ } ;
149165
150166 let processed = 0 ;
151167 const queuingStrategy = new CountQueuingStrategy ( { highWaterMark : 1 } ) ;
152168
153169 const start = async ( c : WritableStreamDefaultController ) => {
154170 try {
155- state = await initFile ( abortController . signal , sender , recipient , mailContent , lang ) ;
171+ state = await initFile (
172+ abortController . signal ,
173+ sender ,
174+ recipient ,
175+ mailContent ,
176+ lang
177+ ) ;
156178 progressReported ( processed , false ) ;
157179 if ( abortController . signal . aborted ) {
158180 throw new Error ( "Abort signaled during initFile." ) ;
159181 }
160- }
161- catch ( e ) {
182+ } catch ( e ) {
162183 c . error ( e ) ;
163184 }
164185 } ;
165-
166- const write = async ( chunk : Uint8Array , c : WritableStreamDefaultController ) => {
186+
187+ const write = async (
188+ chunk : Uint8Array ,
189+ c : WritableStreamDefaultController
190+ ) => {
167191 try {
168192 state = await storeChunk ( abortController . signal , state , chunk , processed ) ;
169193 processed += chunk . length ;
170194 progressReported ( processed , false ) ;
171195 if ( abortController . signal . aborted ) {
172196 throw new Error ( "Abort signaled during storeChunk." ) ;
173197 }
174- }
175- catch ( e ) {
198+ } catch ( e ) {
176199 c . error ( e ) ;
177200 }
178- }
201+ } ;
179202
180203 const close = async ( ) => {
181204 const timeoutId = setTimeout ( ( ) => abortController . abort ( ) , 60000 ) ;
@@ -185,16 +208,19 @@ export function getFileStoreStream(
185208 if ( abortController . signal . aborted ) {
186209 throw new Error ( "Abort signaled during finalize." ) ;
187210 }
188- }
211+ } ;
189212
190213 const abort = async ( ) => {
191214 abortController . abort ( ) ;
192- }
215+ } ;
193216
194- return new WritableStream ( {
195- start,
196- write,
197- close,
198- abort
199- } , queuingStrategy ) ;
217+ return new WritableStream (
218+ {
219+ start,
220+ write,
221+ close,
222+ abort,
223+ } ,
224+ queuingStrategy
225+ ) ;
200226}
0 commit comments