66using System . Threading . Tasks ;
77using BirdMessenger ;
88using BirdMessenger . Collections ;
9+ using BirdMessenger . Delegates ;
10+ using BirdMessenger . Infrastructure ;
911using Newtonsoft . Json ;
1012using Supabase . Storage . Exceptions ;
1113
@@ -175,7 +177,7 @@ public static async Task<HttpResponseMessage> UploadAsync(
175177 }
176178 }
177179
178- var response = await client . PostAsync ( uri , content , cancellationToken ) ;
180+ var response = await client . PostAsync ( uri , content , cancellationToken ) ;
179181
180182 if ( ! response . IsSuccessStatusCode )
181183 {
@@ -199,8 +201,8 @@ public static Task<HttpResponseMessage> UploadOrContinueFileAsync(
199201 this HttpClient client ,
200202 Uri uri ,
201203 string filePath ,
204+ MetadataCollection metadata ,
202205 Dictionary < string , string > ? headers = null ,
203- MetadataCollection ? metadata = null ,
204206 Progress < float > ? progress = null ,
205207 CancellationToken cancellationToken = default
206208 )
@@ -210,8 +212,8 @@ public static Task<HttpResponseMessage> UploadOrContinueFileAsync(
210212 client ,
211213 uri ,
212214 fileStream ,
213- headers ,
214215 metadata ,
216+ headers ,
215217 progress ,
216218 cancellationToken
217219 ) ;
@@ -221,8 +223,8 @@ public static Task<HttpResponseMessage> UploadOrContinueByteAsync(
221223 this HttpClient client ,
222224 Uri uri ,
223225 byte [ ] data ,
226+ MetadataCollection metadata ,
224227 Dictionary < string , string > ? headers = null ,
225- MetadataCollection ? metadata = null ,
226228 Progress < float > ? progress = null ,
227229 CancellationToken cancellationToken = default
228230 )
@@ -232,8 +234,8 @@ public static Task<HttpResponseMessage> UploadOrContinueByteAsync(
232234 client ,
233235 uri ,
234236 stream ,
235- headers ,
236237 metadata ,
238+ headers ,
237239 progress ,
238240 cancellationToken
239241 ) ;
@@ -243,8 +245,8 @@ private static async Task<HttpResponseMessage> ResumableUploadAsync(
243245 this HttpClient client ,
244246 Uri uri ,
245247 Stream fileStream ,
248+ MetadataCollection metadata ,
246249 Dictionary < string , string > ? headers = null ,
247- MetadataCollection ? metadata = null ,
248250 IProgress < float > ? progress = null ,
249251 CancellationToken cancellationToken = default
250252 )
@@ -266,32 +268,51 @@ private static async Task<HttpResponseMessage> ResumableUploadAsync(
266268 }
267269 }
268270
269- var createOption = new TusCreateRequestOption ( )
271+ var cacheKey =
272+ $ "{ metadata [ "bucketName" ] } /{ metadata [ "objectName" ] } /{ metadata [ "contentType" ] } ";
273+
274+ UploadMemoryCache . TryGet ( cacheKey , out var upload ) ;
275+ Uri ? fileLocation = null ;
276+ if ( upload == null )
270277 {
271- Endpoint = uri ,
272- Metadata = metadata ,
273- UploadLength = fileStream . Length ,
274- } ;
278+ var createOption = new TusCreateRequestOption ( )
279+ {
280+ Endpoint = uri ,
281+ Metadata = metadata ,
282+ UploadLength = fileStream . Length ,
283+ } ;
275284
276- var responseCreate = await client . TusCreateAsync ( createOption , cancellationToken ) ;
285+ try
286+ {
287+ var responseCreate = await client . TusCreateAsync (
288+ createOption ,
289+ cancellationToken
290+ ) ;
291+
292+ fileLocation = responseCreate . FileLocation ;
293+ UploadMemoryCache . Set ( cacheKey , fileLocation . ToString ( ) ) ;
294+ }
295+ catch ( TusException error )
296+ {
297+ throw await HandleResponseError ( error ) ;
298+ }
299+ }
300+
301+ if ( upload != null )
302+ fileLocation = new Uri ( upload ) ;
277303
278304 var patchOption = new TusPatchRequestOption
279305 {
280- FileLocation = responseCreate . FileLocation ,
306+ FileLocation = fileLocation ,
281307 Stream = fileStream ,
282308 UploadBufferSize = 6 * 1024 * 1024 ,
283309 UploadType = UploadType . Chunk ,
284- OnProgressAsync = x =>
310+ OnProgressAsync = x => ReportProgressAsync ( progress , x ) ,
311+ OnCompletedAsync = _ =>
285312 {
286- if ( progress == null )
287- return Task . CompletedTask ;
288-
289- var uploadedProgress = ( float ) x . UploadedSize / x . TotalSize * 100f ;
290- progress . Report ( uploadedProgress ) ;
291-
313+ UploadMemoryCache . Remove ( cacheKey ) ;
292314 return Task . CompletedTask ;
293315 } ,
294- OnCompletedAsync = _ => Task . CompletedTask ,
295316 OnFailedAsync = _ => Task . CompletedTask ,
296317 } ;
297318
@@ -300,19 +321,54 @@ private static async Task<HttpResponseMessage> ResumableUploadAsync(
300321 if ( responsePatch . OriginResponseMessage . IsSuccessStatusCode )
301322 return responsePatch . OriginResponseMessage ;
302323
303- var httpContent = await responsePatch . OriginResponseMessage . Content . ReadAsStringAsync ( ) ;
324+ throw await HandleResponseError ( responsePatch . OriginResponseMessage ) ;
325+ }
326+
327+ private static Task ReportProgressAsync (
328+ IProgress < float > ? progress ,
329+ UploadProgressEvent progressInfo
330+ )
331+ {
332+ if ( progress == null )
333+ return Task . CompletedTask ;
334+
335+ var uploadedProgress = ( float ) progressInfo . UploadedSize / progressInfo . TotalSize * 100f ;
336+ progress . Report ( uploadedProgress ) ;
337+
338+ return Task . CompletedTask ;
339+ }
340+
341+ private static async Task < SupabaseStorageException > HandleResponseError (
342+ HttpResponseMessage response
343+ )
344+ {
345+ var httpContent = await response . Content . ReadAsStringAsync ( ) ;
304346 var errorResponse = JsonConvert . DeserializeObject < ErrorResponse > ( httpContent ) ;
305- var e = new SupabaseStorageException ( errorResponse ? . Message ?? httpContent )
347+ var error = new SupabaseStorageException ( errorResponse ? . Message ?? httpContent )
348+ {
349+ Content = httpContent ,
350+ Response = response ,
351+ StatusCode = errorResponse ? . StatusCode ?? ( int ) response . StatusCode ,
352+ } ;
353+ error . AddReason ( ) ;
354+
355+ return error ;
356+ }
357+
358+ private static async Task < SupabaseStorageException > HandleResponseError (
359+ TusException response
360+ )
361+ {
362+ var httpContent = await response . OriginHttpResponse . Content . ReadAsStringAsync ( ) ;
363+ var error = new SupabaseStorageException ( httpContent )
306364 {
307365 Content = httpContent ,
308- Response = responsePatch . OriginResponseMessage ,
309- StatusCode =
310- errorResponse ? . StatusCode
311- ?? ( int ) responsePatch . OriginResponseMessage . StatusCode ,
366+ Response = response . OriginHttpResponse ,
367+ StatusCode = ( int ) response . OriginHttpResponse . StatusCode ,
312368 } ;
369+ error . AddReason ( ) ;
313370
314- e . AddReason ( ) ;
315- throw e ;
371+ return error ;
316372 }
317373 }
318374}
0 commit comments