Separated Wafer-Counter
JsonElement instead of Request body Attachment Class Bump Ready to test GetLastGroupIdWithValue Changed to v4
This commit is contained in:
99
Wafer-Counter/Repositories/FileShareRepository.cs
Normal file
99
Wafer-Counter/Repositories/FileShareRepository.cs
Normal file
@ -0,0 +1,99 @@
|
||||
using OI.Metrology.Shared.Models;
|
||||
using OI.Metrology.Shared.Models.Stateless;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace OI.Metrology.Wafer.Counter.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);
|
||||
if (File.Exists(to))
|
||||
File.Move(to, $"{to}.{DateTime.Now.Ticks}.old");
|
||||
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> 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> 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> httpResponseMessage = httpClient.GetAsync(uri);
|
||||
httpResponseMessage.Wait();
|
||||
result = httpResponseMessage.Result;
|
||||
return result;
|
||||
}
|
||||
|
||||
List<NginxFileSystemSortable> IFileShareRepository.GetNginxFileSystemSortableCollection(HttpClient httpClient, Uri uri, string? endsWith)
|
||||
{
|
||||
List<NginxFileSystemSortable> results = new();
|
||||
Task<HttpResponseMessage> httpResponseMessage = httpClient.GetAsync(uri);
|
||||
httpResponseMessage.Wait();
|
||||
if (httpResponseMessage.Result.StatusCode == System.Net.HttpStatusCode.OK)
|
||||
{
|
||||
FileShareRepository fileShareRepository = this;
|
||||
Task<string> json = httpResponseMessage.Result.Content.ReadAsStringAsync();
|
||||
json.Wait();
|
||||
NginxFileSystem[]? nginxFileSystemCollection = JsonSerializer.Deserialize(json.Result, NginxFileSystemCollectionSourceGenerationContext.Default.NginxFileSystemArray);
|
||||
List<NginxFileSystemSortable> 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;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user