namespace RecRoomArchive.Services { public class ImageService(FileService fileService) { private static readonly HttpClient httpClient = new(); private readonly FileService _fileService = fileService; public async Task GetImage(string imageName) { var path = $"images/{imageName}"; if (!_fileService.FileExists(path)) { var response = await httpClient.GetAsync($"https://cdn.rec.net/img/{imageName}"); if (!response.IsSuccessStatusCode) return null; return await response.Content.ReadAsStreamAsync(); } return new FileStream(Path.Combine("data", path), FileMode.Open, FileAccess.Read, FileShare.Read); } public async Task SaveImageAsync(Stream imageStream) { var imageName = $"{GetRandomFileName()}.jpg"; var path = ("data/images"); var fullPath = Path.Combine(path, imageName); await using var fileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 81920, useAsync: true); await imageStream.CopyToAsync(fileStream); return imageName; } private static string GetRandomFileName() { return Convert.ToBase64String(Guid.NewGuid().ToByteArray()).Replace('+', '-').Replace('/', '_').TrimEnd('='); } } }