♻️ Extract the Storage service to DysonNetwork.Drive microservice
This commit is contained in:
99
DysonNetwork.Drive/Clients/FileServiceClient.cs
Normal file
99
DysonNetwork.Drive/Clients/FileServiceClient.cs
Normal file
@ -0,0 +1,99 @@
|
||||
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<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 { 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();
|
||||
return await JsonSerializer.DeserializeAsync<CloudFile>(stream, _jsonOptions)
|
||||
?? throw new InvalidOperationException("Failed to deserialize file response");
|
||||
}
|
||||
|
||||
public async Task<Stream> GetFileStreamAsync(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 = 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<CloudFile>(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<CloudFile> 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<CloudFile>(responseStream, _jsonOptions)
|
||||
?? throw new InvalidOperationException("Failed to deserialize image processing response");
|
||||
}
|
||||
|
||||
public async Task<string> 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<string>(result, _jsonOptions) ?? string.Empty;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_httpClient?.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user