private StreamContent CreateFileContent(string fileFieldName, UploadItem item) { var fileStream = item.FileInfo.CreateReadStream(); var fileContent = new StreamContent(fileStream); fileContent.Headers.ContentType = item.ContentType ?? MediaTypeHeaderValue.Parse(MimeTypes.GetMimeType(item.FileInfo.Name)); fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = fileFieldName, FileName = item.SkynetPath ?? item.FileInfo.Name }; return(fileContent); }
/// <inheritdoc /> public async Task <Skylink> UploadFile(UploadItem item, UploadOptions options = default) { if (item is null) { throw new ArgumentNullException(nameof(item)); } options ??= UploadOptions._default; using var multiPartContent = new MultipartFormDataContent { CreateFileContent("file", item) }; var response = await _httpClient.PostAsync($"/skynet/skyfile{options.ToQueryString()}", multiPartContent).ConfigureAwait(false); response.EnsureSuccessStatusCode(); var contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); var uploadResponse = await JsonSerializer.DeserializeAsync <UploadResponse>(contentStream, _jsonSerializerOptions).ConfigureAwait(false); return(uploadResponse.ParseAndValidate()); }