From 0fa768556aca00f4346ccd71917fad048def6323 Mon Sep 17 00:00:00 2001 From: Rory& Date: Thu, 30 May 2024 08:22:50 +0000 Subject: Move around some projects, further cleanup pending --- .../Services/MediaStore.cs | 64 ++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Utilities/LibMatrix.HomeserverEmulator/Services/MediaStore.cs (limited to 'Utilities/LibMatrix.HomeserverEmulator/Services/MediaStore.cs') diff --git a/Utilities/LibMatrix.HomeserverEmulator/Services/MediaStore.cs b/Utilities/LibMatrix.HomeserverEmulator/Services/MediaStore.cs new file mode 100644 index 0000000..00f2a42 --- /dev/null +++ b/Utilities/LibMatrix.HomeserverEmulator/Services/MediaStore.cs @@ -0,0 +1,64 @@ +using System.Text.Json; +using LibMatrix.Services; + +namespace LibMatrix.HomeserverEmulator.Services; + +public class MediaStore { + private readonly HSEConfiguration _config; + private readonly HomeserverResolverService _hsResolver; + private List index = new(); + + public MediaStore(HSEConfiguration config, HomeserverResolverService hsResolver) { + _config = config; + _hsResolver = hsResolver; + if (config.StoreData) { + var path = Path.Combine(config.DataStoragePath, "media"); + if (!Directory.Exists(path)) Directory.CreateDirectory(path); + if (File.Exists(Path.Combine(path, "index.json"))) + index = JsonSerializer.Deserialize>(File.ReadAllText(Path.Combine(path, "index.json"))); + } + else + Console.WriteLine("Data storage is disabled, not loading rooms from disk"); + } + + // public async Task UploadMedia(string userId, string mimeType, Stream stream, string? filename = null) { + // var mediaId = $"mxc://{Guid.NewGuid().ToString()}"; + // var path = Path.Combine(_config.DataStoragePath, "media", mediaId); + // if (!Directory.Exists(path)) Directory.CreateDirectory(path); + // var file = Path.Combine(path, filename ?? "file"); + // await using var fs = File.Create(file); + // await stream.CopyToAsync(fs); + // index.Add(new() { }); + // return media; + // } + + public async Task GetRemoteMedia(string serverName, string mediaId) { + if (_config.StoreData) { + var path = Path.Combine(_config.DataStoragePath, "media", serverName, mediaId); + if (!File.Exists(path)) { + var mediaUrl = await _hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + if (mediaUrl is null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Media not found" + }; + using var client = new HttpClient(); + var stream = await client.GetStreamAsync(mediaUrl); + await using var fs = File.Create(path); + await stream.CopyToAsync(fs); + } + return new FileStream(path, FileMode.Open); + } + else { + var mediaUrl = await _hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + if (mediaUrl is null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Media not found" + }; + using var client = new HttpClient(); + return await client.GetStreamAsync(mediaUrl); + } + } + public class MediaInfo { } +} \ No newline at end of file -- cgit 1.4.1