mirror of
https://git.recroomarchive.org/RecRoomArchive/RRAC.git
synced 2026-09-08 14:41:25 -07:00
Initialize repository
Added basic info to get in game for like...August 2016 ;-; It's not much but it's a start
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
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('=');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user