using OI.Metrology.Shared.Models; using OI.Metrology.Shared.Models.Stateless; using System.Text.Json; namespace OI.Metrology.Server.Repository; public class FileShareRepository : IFileShareRepository { Uri IFileShareRepository.Append(Uri uri, params string[] paths) => new(paths.Aggregate(uri.AbsoluteUri, (current, path) => string.Format("{0}/{1}", current.TrimEnd('/'), path.TrimStart('/')))); private Uri GetEndPoint(HttpClient httpClient, string method) { Uri result; if (httpClient.BaseAddress is null) throw new NullReferenceException(nameof(httpClient.BaseAddress)); IFileShareRepository fileShareRepository = this; result = fileShareRepository.Append(httpClient.BaseAddress, "api", "v1", "file-share", method); return result; } void IFileShareRepository.CopyFile(string from, string to) { string directory = Path.GetDirectoryName(to) ?? throw new NullReferenceException(); if (!Directory.Exists(directory)) _ = Directory.CreateDirectory(directory); File.Copy(from, to); } void IFileShareRepository.MoveFile(string from, string to) { string directory = Path.GetDirectoryName(to) ?? throw new NullReferenceException(); if (!Directory.Exists(directory)) _ = Directory.CreateDirectory(directory); File.Move(from, to); } void IFileShareRepository.FileWrite(string path, string contents) { string directory = Path.GetDirectoryName(path) ?? throw new NullReferenceException(); if (!Directory.Exists(directory)) _ = Directory.CreateDirectory(directory); File.WriteAllText(path, contents); } void IFileShareRepository.CopyFile(HttpClient httpClient, string from, string to) { Uri uri = GetEndPoint(httpClient, "copy-file"); Task httpResponseMessage = httpClient.GetAsync(uri); httpResponseMessage.Wait(); if (httpResponseMessage.Result.StatusCode != System.Net.HttpStatusCode.OK) throw new Exception(httpResponseMessage.Result.StatusCode.ToString()); } void IFileShareRepository.MoveFile(HttpClient httpClient, string from, string to) { Uri uri = GetEndPoint(httpClient, "move-file"); Task httpResponseMessage = httpClient.GetAsync(uri); httpResponseMessage.Wait(); if (httpResponseMessage.Result.StatusCode != System.Net.HttpStatusCode.OK) throw new Exception(httpResponseMessage.Result.StatusCode.ToString()); } void IFileShareRepository.FileWrite(HttpClient httpClient, string path, string contents) { Uri uri = GetEndPoint(httpClient, "file-write"); Task httpResponseMessage = httpClient.GetAsync(uri); httpResponseMessage.Wait(); if (httpResponseMessage.Result.StatusCode != System.Net.HttpStatusCode.OK) throw new Exception(httpResponseMessage.Result.StatusCode.ToString()); } HttpResponseMessage IFileShareRepository.ReadFile(HttpClient httpClient, Uri uri) { HttpResponseMessage result; Task httpResponseMessage = httpClient.GetAsync(uri); httpResponseMessage.Wait(); result = httpResponseMessage.Result; return result; } List IFileShareRepository.GetNginxFileSystemSortableCollection(HttpClient httpClient, Uri uri, string? endsWith) { List results = new(); Task httpResponseMessage = httpClient.GetAsync(uri); httpResponseMessage.Wait(); if (httpResponseMessage.Result.StatusCode == System.Net.HttpStatusCode.OK) { FileShareRepository fileShareRepository = this; Task json = httpResponseMessage.Result.Content.ReadAsStringAsync(); json.Wait(); NginxFileSystem[]? nginxFileSystemCollection = JsonSerializer.Deserialize(json.Result, NginxFileSystemCollectionSourceGenerationContext.Default.NginxFileSystemArray); List nginxFileSystemSortableCollection = NginxFileSystemSortable.Convert(fileShareRepository, uri, nginxFileSystemCollection); foreach (NginxFileSystemSortable nginxFileSystemSortable in nginxFileSystemSortableCollection.OrderByDescending(l => l.DateTime)) { if (!string.IsNullOrEmpty(endsWith) && !nginxFileSystemSortable.Name.EndsWith(endsWith)) continue; results.Add(nginxFileSystemSortable); } } return results; } }