@@ -185,6 +185,8 @@ interface DeployAPIResponse {
185185 id : string ;
186186}
187187
188+ const CHUNK_SIZE = 5 * 1024 * 1024 ; // 5MB
189+
188190export const deploy = ( {
189191 app,
190192 files,
@@ -199,54 +201,88 @@ export const deploy = ({
199201
200202 setLoading ( true ) ;
201203
202- var res : Promise < DeployAPIResponse > ;
204+ const handleUploadError = async ( res : Response ) => {
205+ if ( res . status === 429 ) {
206+ return setError ( ( await api . errors ( res ) ) [ 0 ] ) ;
207+ }
203208
204- if ( files && files . length > 0 ) {
205- // Create a FormData object to hold the files
206- const formData = new FormData ( ) ;
209+ if ( res . status === 401 || res . status === 403 || res . status === 404 ) {
210+ return setError ( "repo-not-found" ) ;
211+ }
207212
208- // Append the files to the formData object
209- files . forEach ( file => {
210- formData . append ( "files" , file ) ;
211- } ) ;
213+ let message = "" ;
212214
213- formData . append ( "appId" , app . id ) ;
214- formData . append ( "envId" , environment . id ! ) ;
215- formData . append ( "publish" , config ?. publish ? "true" : "false" ) ;
215+ try {
216+ const data = await res . json ( ) ;
217+ message = data . error ;
218+ } catch { }
216219
217- res = api . upload < DeployAPIResponse > ( "/app/deploy" , {
218- body : formData ,
219- } ) ;
220- } else {
221- res = api . post < DeployAPIResponse > ( `/app/deploy` , {
222- envId : environment . id ,
223- appId : app . id ,
224- ...config ,
225- } ) ;
226- }
227-
228- return res
229- . catch ( async res => {
230- if ( res . status === 429 ) {
231- return setError ( ( await api . errors ( res ) ) [ 0 ] ) ;
232- }
220+ setError (
221+ message ||
222+ "Something wrong happened here. Please contact us at hello@stormkit.io"
223+ ) ;
224+ } ;
233225
234- if ( res . status === 401 || res . status === 403 || res . status === 404 ) {
235- return setError ( "repo-not-found" ) ;
236- }
226+ if ( files && files . length > 0 ) {
227+ if ( files . length !== 1 ) {
228+ setError ( "You can upload only one file at a time." ) ;
229+ return Promise . resolve ( ) ;
230+ }
237231
238- let message = "" ;
232+ const file = files [ 0 ] ; // Assume only one file is uploaded
233+ const totalChunks = Math . ceil ( file . size / CHUNK_SIZE ) ;
234+ const fileId = `${ app . id } -${ Date . now ( ) } ` ; // Unique file ID
235+
236+ const promises : Promise < DeployAPIResponse > [ ] = [ ] ;
237+
238+ for ( let i = 0 ; i < totalChunks ; i ++ ) {
239+ const start = i * CHUNK_SIZE ;
240+ const end = Math . min ( start + CHUNK_SIZE , file . size ) ;
241+ const chunk = file . slice ( start , end ) ;
242+
243+ // Create a FormData object to hold the files
244+ const formData = new FormData ( ) ;
245+
246+ formData . append ( "appId" , app . id ) ;
247+ formData . append ( "envId" , environment . id ! ) ;
248+ formData . append ( "publish" , config ?. publish ? "true" : "false" ) ;
249+ formData . append ( "files" , chunk , file . name ) ;
250+
251+ promises . push (
252+ api . upload < DeployAPIResponse > ( "/app/deploy" , {
253+ body : formData ,
254+ headers : {
255+ "X-File-ID" : fileId ,
256+ "X-Chunked-Upload" : totalChunks > 0 ? "true" : "false" ,
257+ "X-Total-Chunks" : Math . max ( totalChunks , 1 ) . toString ( ) ,
258+ "X-Chunk-Index" : i . toString ( ) ,
259+ } ,
260+ } )
261+ ) ;
262+ }
239263
240- try {
241- const data = await res . json ( ) ;
242- message = data . error ;
243- } catch { }
264+ return Promise . all ( promises )
265+ . then ( res => {
266+ // Return the response with the deployment id
267+ for ( let i = 0 ; i < res . length ; i ++ ) {
268+ if ( res [ i ] . id ) {
269+ return res [ i ] ;
270+ }
271+ }
272+ } )
273+ . catch ( handleUploadError )
274+ . finally ( ( ) => {
275+ setLoading ( false ) ;
276+ } ) ;
277+ }
244278
245- setError (
246- message ||
247- "Something wrong happened here. Please contact us at hello@stormkit.io"
248- ) ;
279+ return api
280+ . post < DeployAPIResponse > ( `/app/deploy` , {
281+ envId : environment . id ,
282+ appId : app . id ,
283+ ...config ,
249284 } )
285+ . catch ( handleUploadError )
250286 . finally ( ( ) => {
251287 setLoading ( false ) ;
252288 } ) ;
0 commit comments