public FirebaseStorageTask(FirebaseStorageOptions options, string url, string downloadUrl, Stream stream, CancellationToken cancellationToken, string mimeType = null) { this.TargetUrl = url; this.uploadTask = this.UploadFile(options, url, downloadUrl, stream, cancellationToken, mimeType); this.stream = stream; this.Progress = new Progress <FirebaseStorageProgress>(); Task.Factory.StartNew(this.ReportProgressLoop); }
/// <summary> /// Creates a new <see cref="HttpClient"/> with authentication header when <see cref="FirebaseStorageOptions.AuthTokenAsyncFactory"/> is specified. /// </summary> /// <param name="options">Firebase storage options.</param> public static async Task <HttpClient> CreateHttpClientAsync(this FirebaseStorageOptions options) { var client = new HttpClient(); if (options.HttpClientTimeout != default(TimeSpan)) { client.Timeout = options.HttpClientTimeout; } if (options.AuthTokenAsyncFactory != null) { var auth = await options.AuthTokenAsyncFactory().ConfigureAwait(false); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Firebase", auth); } return(client); }
private async Task <string> UploadFile(FirebaseStorageOptions options, string url, string downloadUrl, Stream stream, CancellationToken cancellationToken, string mimeType = null) { var responseData = "N/A"; try { using (var client = await options.CreateHttpClientAsync()) { var request = new HttpRequestMessage(HttpMethod.Post, url) { Content = new StreamContent(stream) }; if (!string.IsNullOrEmpty(mimeType)) { request.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType); } var response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false); responseData = await response.Content.ReadAsStringAsync().ConfigureAwait(false); response.EnsureSuccessStatusCode(); var data = JsonConvert.DeserializeObject <Dictionary <string, object> >(responseData); return(downloadUrl + data["downloadTokens"]); } } catch (TaskCanceledException) { if (options.ThrowOnCancel) { throw; } return(string.Empty); } catch (Exception ex) { throw new FirebaseStorageException(url, responseData, ex); } }