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; namespace DysonNetwork.Drive.Clients { public class FileServiceClient : IFileServiceClient, IDisposable { private readonly HttpClient _httpClient; private readonly ILogger _logger; private readonly JsonSerializerOptions _jsonOptions; public FileServiceClient(HttpClient httpClient, ILogger logger) { _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; } public async Task GetFileAsync(string fileId) { var response = await _httpClient.GetAsync($"api/files/{fileId}"); response.EnsureSuccessStatusCode(); await using var stream = await response.Content.ReadAsStreamAsync(); return await JsonSerializer.DeserializeAsync(stream, _jsonOptions) ?? throw new InvalidOperationException("Failed to deserialize file response"); } public async Task GetFileStreamAsync(string fileId) { var response = await _httpClient.GetAsync($"api/files/{fileId}/download"); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStreamAsync(); } public async Task UploadFileAsync(Stream fileStream, string fileName, string? contentType = null) { using var content = new MultipartFormDataContent { { new StreamContent(fileStream), "file", fileName } }; var response = await _httpClient.PostAsync("api/files/upload", content); response.EnsureSuccessStatusCode(); await using var responseStream = await response.Content.ReadAsStreamAsync(); return await JsonSerializer.DeserializeAsync(responseStream, _jsonOptions) ?? throw new InvalidOperationException("Failed to deserialize upload response"); } public async Task DeleteFileAsync(string fileId) { var response = await _httpClient.DeleteAsync($"api/files/{fileId}"); response.EnsureSuccessStatusCode(); } public async Task ProcessImageAsync(Stream imageStream, string fileName, string? contentType = null) { using var content = new MultipartFormDataContent { { new StreamContent(imageStream), "image", fileName } }; var response = await _httpClient.PostAsync("api/files/process-image", content); response.EnsureSuccessStatusCode(); await using var responseStream = await response.Content.ReadAsStreamAsync(); return await JsonSerializer.DeserializeAsync(responseStream, _jsonOptions) ?? throw new InvalidOperationException("Failed to deserialize image processing response"); } public async Task GetFileUrl(string fileId, bool useCdn = false) { var url = $"api/files/{fileId}/url"; if (useCdn) { url += "?useCdn=true"; } var response = await _httpClient.GetAsync(url); response.EnsureSuccessStatusCode(); var result = await response.Content.ReadAsStringAsync(); return JsonSerializer.Deserialize(result, _jsonOptions) ?? string.Empty; } public void Dispose() { _httpClient?.Dispose(); GC.SuppressFinalize(this); } } }