mirror of
https://git.recroomarchive.org/RecRoomArchive/RRAC.git
synced 2026-09-08 14:41:25 -07:00
387ec7ba89
Added basic info to get in game for like...August 2016 ;-; It's not much but it's a start
44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
namespace RecRoomArchive.Services
|
|
{
|
|
public class ImageService(FileService fileService)
|
|
{
|
|
private static readonly HttpClient httpClient = new();
|
|
|
|
private readonly FileService _fileService = fileService;
|
|
|
|
public async Task<Stream?> 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<string> 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('=');
|
|
}
|
|
}
|
|
} |