| | 1 | | using System.Text; |
| | 2 | | using ValidateLib.ErrorsAndWarnings.Errors; |
| | 3 | |
|
| | 4 | | namespace ValidateLib.UtilityClasses |
| | 5 | | { |
| | 6 | | public class FileWrapper |
| | 7 | | { |
| 1 | 8 | | string FileIRI { get; set; } |
| 0 | 9 | | FileStream? _FileStream { get; set; } |
| 1 | 10 | | Uri _Uri { get; set; } |
| | 11 | | private bool _remote = false; |
| 1 | 12 | | private string _tempFIle = Path.GetTempFileName(); |
| | 13 | | private bool _disposed = false; |
| | 14 | | private string? _unifiedPath; |
| 1 | 15 | | public FileWrapper(string fileIRI) |
| | 16 | | { |
| 1 | 17 | | FileIRI = fileIRI; |
| 1 | 18 | | _Uri = new Uri(fileIRI); |
| 1 | 19 | | } |
| | 20 | |
|
| | 21 | | public FileStream? OpenNewFileStream() |
| | 22 | | { |
| 1 | 23 | | if (_disposed) |
| | 24 | | { |
| 0 | 25 | | throw new ObjectDisposedException("FileWrapper"); |
| | 26 | | } |
| 1 | 27 | | if (_unifiedPath is null) |
| | 28 | | { |
| 1 | 29 | | if (_Uri.Scheme == Uri.UriSchemeFile) |
| | 30 | | { |
| 1 | 31 | | _unifiedPath = _Uri.LocalPath; |
| | 32 | | } |
| 1 | 33 | | else if (_Uri.Scheme == Uri.UriSchemeHttp || _Uri.Scheme == Uri.UriSchemeHttps) |
| | 34 | | { |
| 1 | 35 | | _tempFIle = _tempFIle + Path.GetRandomFileName(); |
| 1 | 36 | | _remote = true; |
| 1 | 37 | | DownloadAndSave(FileIRI, _tempFIle); |
| 1 | 38 | | _unifiedPath = _tempFIle; |
| | 39 | | } |
| | 40 | | else |
| | 41 | | { |
| 0 | 42 | | ErrorFactory.ThrowUnsupportedSchemeError(FileIRI); |
| | 43 | | } |
| | 44 | | } |
| | 45 | |
|
| 1 | 46 | | return new FileStream(_unifiedPath!, FileMode.Open, FileAccess.Read); |
| | 47 | | } |
| | 48 | |
|
| | 49 | | public static void DownloadAndSave(string sourceFile, string tempFilePath) |
| | 50 | | { |
| | 51 | | try |
| | 52 | | { |
| 1 | 53 | | Stream fileStream = GetFileStream(sourceFile).Result; |
| 1 | 54 | | SaveStream(fileStream, tempFilePath).Wait(); |
| 1 | 55 | | } |
| | 56 | | catch (AggregateException aggregateException) |
| | 57 | | { |
| 0 | 58 | | StringBuilder sb = new StringBuilder(); |
| 0 | 59 | | foreach (var innerException in aggregateException.InnerExceptions) |
| | 60 | | { |
| 0 | 61 | | sb.AppendLine(innerException.Message); |
| | 62 | | } |
| 0 | 63 | | string allMessages = sb.ToString(); |
| | 64 | |
|
| 0 | 65 | | ErrorFactory.ThrowRemoteFileResoltuionError(sourceFile, sb.ToString()); |
| 0 | 66 | | } |
| 0 | 67 | | catch (Exception ex) |
| | 68 | | { |
| 0 | 69 | | ErrorFactory.ThrowRemoteFileResoltuionError(sourceFile, ex.Message); |
| 0 | 70 | | } |
| | 71 | |
|
| 1 | 72 | | } |
| | 73 | |
|
| | 74 | | async static Task<Stream> GetFileStream(string fileUrl) |
| | 75 | | { |
| 1 | 76 | | HttpClient httpClient = new HttpClient(); |
| 1 | 77 | | Stream fileStream = await httpClient.GetStreamAsync(fileUrl); |
| 1 | 78 | | return fileStream; |
| | 79 | |
|
| 1 | 80 | | } |
| | 81 | |
|
| | 82 | | async static Task SaveStream(Stream fileStream, string filePath) |
| | 83 | | { |
| 1 | 84 | | using (FileStream outputFileStream = new FileStream(filePath, FileMode.CreateNew)) |
| | 85 | | { |
| 1 | 86 | | await fileStream.CopyToAsync(outputFileStream); |
| 1 | 87 | | } |
| 1 | 88 | | } |
| | 89 | |
|
| | 90 | | public void Dispose() |
| | 91 | | { |
| | 92 | |
|
| 0 | 93 | | if (_remote) |
| | 94 | | { |
| 0 | 95 | | if (File.Exists(_tempFIle)) |
| | 96 | | { |
| 0 | 97 | | File.Delete(_tempFIle); |
| | 98 | | } |
| | 99 | | } |
| | 100 | |
|
| 0 | 101 | | _disposed = true; |
| 0 | 102 | | } |
| | 103 | | } |
| | 104 | | } |