< Summary

Information
Class: ValidateLib.UtilityClasses.FileWrapper
Assembly: validatelib.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\UtilityClasses\FileWrapper.cs
Line coverage
63%
Covered lines: 29
Uncovered lines: 17
Coverable lines: 46
Total lines: 104
Line coverage: 63%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBlocks covered Blocks not covered
FileWrapper(...)60
OpenNewFileStream()244
DownloadAndSave(...)622
GetFileStream()142
SaveStream()222
Dispose()05

File(s)

C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\UtilityClasses\FileWrapper.cs

#LineLine coverage
 1using System.Text;
 2using ValidateLib.ErrorsAndWarnings.Errors;
 3
 4namespace ValidateLib.UtilityClasses
 5{
 6    public class FileWrapper
 7    {
 18        string FileIRI { get; set; }
 09        FileStream? _FileStream { get; set; }
 110        Uri _Uri { get; set; }
 11        private bool _remote = false;
 112        private string _tempFIle = Path.GetTempFileName();
 13        private bool _disposed = false;
 14        private string? _unifiedPath;
 115        public FileWrapper(string fileIRI)
 16        {
 117            FileIRI = fileIRI;
 118            _Uri = new Uri(fileIRI);
 119        }
 20
 21        public FileStream? OpenNewFileStream()
 22        {
 123            if (_disposed)
 24            {
 025                throw new ObjectDisposedException("FileWrapper");
 26            }
 127            if (_unifiedPath is null)
 28            {
 129                if (_Uri.Scheme == Uri.UriSchemeFile)
 30                {
 131                    _unifiedPath = _Uri.LocalPath;
 32                }
 133                else if (_Uri.Scheme == Uri.UriSchemeHttp || _Uri.Scheme == Uri.UriSchemeHttps)
 34                {
 135                    _tempFIle = _tempFIle + Path.GetRandomFileName();
 136                    _remote = true;
 137                    DownloadAndSave(FileIRI, _tempFIle);
 138                    _unifiedPath = _tempFIle;
 39                }
 40                else
 41                {
 042                    ErrorFactory.ThrowUnsupportedSchemeError(FileIRI);
 43                }
 44            }
 45
 146            return new FileStream(_unifiedPath!, FileMode.Open, FileAccess.Read);
 47        }
 48
 49        public static void DownloadAndSave(string sourceFile, string tempFilePath)
 50        {
 51            try
 52            {
 153                Stream fileStream = GetFileStream(sourceFile).Result;
 154                SaveStream(fileStream, tempFilePath).Wait();
 155            }
 56            catch (AggregateException aggregateException)
 57            {
 058                StringBuilder sb = new StringBuilder();
 059                foreach (var innerException in aggregateException.InnerExceptions)
 60                {
 061                    sb.AppendLine(innerException.Message);
 62                }
 063                string allMessages = sb.ToString();
 64
 065                ErrorFactory.ThrowRemoteFileResoltuionError(sourceFile, sb.ToString());
 066            }
 067            catch (Exception ex)
 68            {
 069                ErrorFactory.ThrowRemoteFileResoltuionError(sourceFile, ex.Message);
 070            }
 71
 172        }
 73
 74        async static Task<Stream> GetFileStream(string fileUrl)
 75        {
 176            HttpClient httpClient = new HttpClient();
 177            Stream fileStream = await httpClient.GetStreamAsync(fileUrl);
 178            return fileStream;
 79
 180        }
 81
 82        async static Task SaveStream(Stream fileStream, string filePath)
 83        {
 184            using (FileStream outputFileStream = new FileStream(filePath, FileMode.CreateNew))
 85            {
 186                await fileStream.CopyToAsync(outputFileStream);
 187            }
 188        }
 89
 90        public void Dispose()
 91        {
 92
 093            if (_remote)
 94            {
 095                if (File.Exists(_tempFIle))
 96                {
 097                    File.Delete(_tempFIle);
 98                }
 99            }
 100
 0101            _disposed = true;
 0102        }
 103    }
 104}