:drunk: No idea what did AI did
This commit is contained in:
		
							
								
								
									
										77
									
								
								DysonNetwork.Common/Clients/FileServiceClient.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								DysonNetwork.Common/Clients/FileServiceClient.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,77 @@ | ||||
| using System; | ||||
| using System.IO; | ||||
| using System.Net.Http; | ||||
| using System.Text.Json; | ||||
| using System.Threading.Tasks; | ||||
| using DysonNetwork.Common.Interfaces; | ||||
| using DysonNetwork.Common.Models; | ||||
| using Microsoft.Extensions.Logging; | ||||
| using NodaTime; | ||||
| using NodaTime.Serialization.SystemTextJson; | ||||
|  | ||||
| namespace DysonNetwork.Common.Clients | ||||
| { | ||||
|     public class FileServiceClient : IFileServiceClient, IDisposable | ||||
|     { | ||||
|         private readonly HttpClient _httpClient; | ||||
|         private readonly ILogger<FileServiceClient> _logger; | ||||
|         private readonly JsonSerializerOptions _jsonOptions; | ||||
|  | ||||
|         public FileServiceClient(HttpClient httpClient, ILogger<FileServiceClient> logger) | ||||
|         { | ||||
|             _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); | ||||
|             _logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||||
|             _jsonOptions = new JsonSerializerOptions().ConfigureForNodaTime(DateTimeZoneProviders.Tzdb); | ||||
|             _jsonOptions.PropertyNameCaseInsensitive = true; | ||||
|         } | ||||
|  | ||||
|         public async Task<CloudFile> GetFileAsync(string fileId) | ||||
|         { | ||||
|             var response = await _httpClient.GetAsync($"api/files/{fileId}"); | ||||
|             response.EnsureSuccessStatusCode(); | ||||
|              | ||||
|             await using var stream = await response.Content.ReadAsStreamAsync(); | ||||
|             var file = await JsonSerializer.DeserializeAsync<CloudFile>(stream, _jsonOptions); | ||||
|             return file; | ||||
|         } | ||||
|  | ||||
|         public async Task<Stream> DownloadFileAsync(string fileId) | ||||
|         { | ||||
|             var response = await _httpClient.GetAsync($"api/files/{fileId}/download"); | ||||
|             response.EnsureSuccessStatusCode(); | ||||
|              | ||||
|             return await response.Content.ReadAsStreamAsync(); | ||||
|         } | ||||
|  | ||||
|         public async Task<CloudFile> UploadFileAsync(Stream fileStream, string fileName, string contentType, string? folderId = null) | ||||
|         { | ||||
|             using var content = new MultipartFormDataContent(); | ||||
|             var fileContent = new StreamContent(fileStream); | ||||
|             content.Add(fileContent, "file", fileName); | ||||
|              | ||||
|             if (!string.IsNullOrEmpty(folderId)) | ||||
|             { | ||||
|                 content.Add(new StringContent(folderId), "folderId"); | ||||
|             } | ||||
|              | ||||
|             var response = await _httpClient.PostAsync("api/files/upload", content); | ||||
|             response.EnsureSuccessStatusCode(); | ||||
|              | ||||
|             await using var responseStream = await response.Content.ReadAsStreamAsync(); | ||||
|             var file = await JsonSerializer.DeserializeAsync<CloudFile>(responseStream, _jsonOptions); | ||||
|             return file; | ||||
|         } | ||||
|  | ||||
|         public async Task DeleteFileAsync(string fileId) | ||||
|         { | ||||
|             var response = await _httpClient.DeleteAsync($"api/files/{fileId}"); | ||||
|             response.EnsureSuccessStatusCode(); | ||||
|         } | ||||
|  | ||||
|         public void Dispose() | ||||
|         { | ||||
|             _httpClient?.Dispose(); | ||||
|             GC.SuppressFinalize(this); | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user