diff options
| author | Mirta <mirta@twine-s.com> | 2020-12-30 16:39:52 +0200 |
|---|---|---|
| committer | Mirta <mirta@twine-s.com> | 2020-12-30 16:39:52 +0200 |
| commit | 00a491d93733d4625ad329b2ba8237f445364b3f (patch) | |
| tree | 4b24c6fa78d7648f4bb7cefafa464bb0b063fec4 /Software/Visual_Studio/Tango.Transport/Web | |
| parent | 124ad4150f80c6846fdee41dbbda9848c105f6e5 (diff) | |
| download | Tango-00a491d9.tar.gz Tango-00a491d9.zip | |
merge
Diffstat (limited to 'Software/Visual_Studio/Tango.Transport/Web')
| -rw-r--r-- | Software/Visual_Studio/Tango.Transport/Web/AutoFileDownloader.cs | 123 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Transport/Web/IWebFileDownloader.cs | 15 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Transport/Web/StandardFileDownloader.cs | 78 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Transport/Web/StorageBlobDownloader.cs | 27 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Transport/Web/StorageBlobProgressEventArgs.cs (renamed from Software/Visual_Studio/Tango.Transport/Web/WebFileDownloaderProgressEventArgs.cs) | 2 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Transport/Web/StorageBlobStream.cs | 45 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Transport/Web/StorageBlobUploader.cs | 4 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Transport/Web/WebTransportClient.cs | 57 |
8 files changed, 23 insertions, 328 deletions
diff --git a/Software/Visual_Studio/Tango.Transport/Web/AutoFileDownloader.cs b/Software/Visual_Studio/Tango.Transport/Web/AutoFileDownloader.cs deleted file mode 100644 index 26433bae7..000000000 --- a/Software/Visual_Studio/Tango.Transport/Web/AutoFileDownloader.cs +++ /dev/null @@ -1,123 +0,0 @@ -using Microsoft.WindowsAzure.Storage.Blob; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; -using Tango.Core.IO; - -namespace Tango.Transport.Web -{ - public class AutoFileDownloader : IWebFileDownloader - { - public enum DownloadMode - { - Standard, - Blob - } - - private bool _disposed; - private StorageBlobDownloader _blobDownloader; - private StandardFileDownloader _standardDownloader; - private bool _isCdnOK = false; - private long _fileSize = -1; - - public event EventHandler<WebFileDownloaderProgressEventArgs> Progress; - - public String Address { get; private set; } - - public String FileName { get; private set; } - - public DownloadMode Mode { get; private set; } - - public AutoFileDownloader(String blobAddress, String cdnAddress, String fileName) - { - FileName = fileName; - - _blobDownloader = new StorageBlobDownloader(blobAddress, fileName); - _standardDownloader = new StandardFileDownloader(cdnAddress, fileName); - - _blobDownloader.Progress += OnProgress; - _standardDownloader.Progress += OnProgress; - } - - private void OnProgress(object sender, WebFileDownloaderProgressEventArgs e) - { - Progress?.Invoke(this, e); - } - - public async Task Download() - { - if (_disposed) - { - throw new ObjectDisposedException("The file downloader can only be used once."); - } - - if (_fileSize == -1) - { - await GetFileSize(); - } - - if (_isCdnOK) - { - await _standardDownloader.Download(); - } - else - { - await _blobDownloader.Download(); - } - } - - public async Task ResolveMode() - { - await GetFileSize(); - } - - public Task<long> GetFileSize() - { - if (_fileSize == -1) - { - return Task.Factory.StartNew<long>(() => - { - try - { - _fileSize = _standardDownloader.GetFileSize().Result; - _isCdnOK = true; - Mode = DownloadMode.Standard; - Address = _standardDownloader.Address; - return _fileSize; - } - catch - { - try - { - _fileSize = _blobDownloader.GetFileSize().Result; - Mode = DownloadMode.Blob; - Address = _blobDownloader.Address; - return _fileSize; - } - catch - { - throw new Exception("Invalid address for standard download or blob."); - } - } - }); - } - else - { - return Task.FromResult(_fileSize); - } - } - - public void Dispose() - { - if (!_disposed) - { - _disposed = true; - _blobDownloader.Dispose(); - _standardDownloader.Dispose(); - } - } - } -} diff --git a/Software/Visual_Studio/Tango.Transport/Web/IWebFileDownloader.cs b/Software/Visual_Studio/Tango.Transport/Web/IWebFileDownloader.cs deleted file mode 100644 index 2f65553d5..000000000 --- a/Software/Visual_Studio/Tango.Transport/Web/IWebFileDownloader.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Tango.Transport.Web -{ - public interface IWebFileDownloader : IDisposable - { - event EventHandler<WebFileDownloaderProgressEventArgs> Progress; - Task Download(); - Task<long> GetFileSize(); - } -} diff --git a/Software/Visual_Studio/Tango.Transport/Web/StandardFileDownloader.cs b/Software/Visual_Studio/Tango.Transport/Web/StandardFileDownloader.cs deleted file mode 100644 index 1b62fc023..000000000 --- a/Software/Visual_Studio/Tango.Transport/Web/StandardFileDownloader.cs +++ /dev/null @@ -1,78 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; - -namespace Tango.Transport.Web -{ - public class StandardFileDownloader : IWebFileDownloader - { - private WebClient _client; - private TaskCompletionSource<object> _completionSource; - - public String Address { get; private set; } - - public String FileName { get; private set; } - - public event EventHandler<WebFileDownloaderProgressEventArgs> Progress; - - public StandardFileDownloader(String address, String fileName) - { - Address = address; - FileName = fileName; - _client = new WebClient(); - _client.Proxy = null; - _client.DownloadProgressChanged += _client_DownloadProgressChanged; - _client.DownloadFileCompleted += _client_DownloadFileCompleted; - - _completionSource = new TaskCompletionSource<object>(); - } - - private void _client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) - { - if (e.Error != null) - { - _completionSource.SetException(e.Error); - } - else - { - _completionSource.SetResult(true); - } - } - - public Task Download() - { - _client.DownloadFileAsync(new Uri(Address), FileName); - return _completionSource.Task; - } - - private void _client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) - { - Progress?.Invoke(this, new WebFileDownloaderProgressEventArgs() - { - Current = e.BytesReceived, - Total = e.TotalBytesToReceive, - }); - } - - public Task<long> GetFileSize() - { - return Task.Factory.StartNew<long>(() => - { - using (var client = new WebClient()) - { - client.OpenRead(Address); - Int64 bytes_total = Convert.ToInt64(client.ResponseHeaders["Content-Length"]); - return bytes_total; - } - }); - } - - public void Dispose() - { - _client.Dispose(); - } - } -} diff --git a/Software/Visual_Studio/Tango.Transport/Web/StorageBlobDownloader.cs b/Software/Visual_Studio/Tango.Transport/Web/StorageBlobDownloader.cs index dfbd9f93a..603463823 100644 --- a/Software/Visual_Studio/Tango.Transport/Web/StorageBlobDownloader.cs +++ b/Software/Visual_Studio/Tango.Transport/Web/StorageBlobDownloader.cs @@ -9,33 +9,30 @@ using Tango.Core.IO; namespace Tango.Transport.Web { - public class StorageBlobDownloader : IWebFileDownloader + public class StorageBlobDownloader : IDisposable { private bool _disposed; private FileStreamWrapper _stream; private long _fileSize; - private String _fileName; public CloudBlockBlob Blob { get; private set; } - public String Address { get; private set; } - - public event EventHandler<WebFileDownloaderProgressEventArgs> Progress; + public event EventHandler<StorageBlobProgressEventArgs> Progress; public StorageBlobDownloader(CloudBlockBlob blob, String fileName) { Blob = blob; - _fileName = fileName; + _stream = new FileStreamWrapper(fileName, FileMode.Create, OnProgress); } public StorageBlobDownloader(String blobAddress, String fileName) : this(new CloudBlockBlob(new Uri(blobAddress)), fileName) { - Address = blobAddress; + } private void OnProgress(long current) { - Progress?.Invoke(this, new WebFileDownloaderProgressEventArgs() + Progress?.Invoke(this, new StorageBlobProgressEventArgs() { Current = current, Total = _fileSize, @@ -52,8 +49,6 @@ namespace Tango.Transport.Web await Blob.FetchAttributesAsync(); _fileSize = Blob.Properties.Length; - _stream = new FileStreamWrapper(_fileName, FileMode.Create, OnProgress); - await Blob.DownloadToStreamAsync(_stream); Dispose(); } @@ -63,18 +58,8 @@ namespace Tango.Transport.Web if (!_disposed) { _disposed = true; - - if (_stream != null) - { - _stream.Dispose(); - } + _stream.Dispose(); } } - - public async Task<long> GetFileSize() - { - await Blob.FetchAttributesAsync(); - return Blob.Properties.Length; - } } } diff --git a/Software/Visual_Studio/Tango.Transport/Web/WebFileDownloaderProgressEventArgs.cs b/Software/Visual_Studio/Tango.Transport/Web/StorageBlobProgressEventArgs.cs index 570c4058a..ae48e34cf 100644 --- a/Software/Visual_Studio/Tango.Transport/Web/WebFileDownloaderProgressEventArgs.cs +++ b/Software/Visual_Studio/Tango.Transport/Web/StorageBlobProgressEventArgs.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace Tango.Transport.Web { - public class WebFileDownloaderProgressEventArgs : EventArgs + public class StorageBlobProgressEventArgs : EventArgs { public long Total { get; set; } public long Current { get; set; } diff --git a/Software/Visual_Studio/Tango.Transport/Web/StorageBlobStream.cs b/Software/Visual_Studio/Tango.Transport/Web/StorageBlobStream.cs deleted file mode 100644 index f4cbf7f62..000000000 --- a/Software/Visual_Studio/Tango.Transport/Web/StorageBlobStream.cs +++ /dev/null @@ -1,45 +0,0 @@ -using Microsoft.WindowsAzure.Storage.Blob; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Tango.Transport.Web -{ - public class StorageBlobStream : IDisposable - { - public CloudBlockBlob Blob { get; private set; } - private Stream _blobStream; - - public String Address { get; private set; } - - private StorageBlobStream(CloudBlockBlob blob) - { - Blob = blob; - } - - public StorageBlobStream(String blobAddress) : this(new CloudBlockBlob(new Uri(blobAddress))) - { - Address = blobAddress; - } - - public Stream OpenRead() - { - _blobStream = Blob.OpenRead(); - return _blobStream; - } - - public Stream OpenWrite() - { - _blobStream = Blob.OpenWrite(); - return _blobStream; - } - - public void Dispose() - { - _blobStream?.Dispose(); - } - } -} diff --git a/Software/Visual_Studio/Tango.Transport/Web/StorageBlobUploader.cs b/Software/Visual_Studio/Tango.Transport/Web/StorageBlobUploader.cs index 00600c679..8d645f33f 100644 --- a/Software/Visual_Studio/Tango.Transport/Web/StorageBlobUploader.cs +++ b/Software/Visual_Studio/Tango.Transport/Web/StorageBlobUploader.cs @@ -16,7 +16,7 @@ namespace Tango.Transport.Web public CloudBlockBlob Blob { get; private set; } - public event EventHandler<WebFileDownloaderProgressEventArgs> Progress; + public event EventHandler<StorageBlobProgressEventArgs> Progress; public StorageBlobUploader(CloudBlockBlob blob, String fileName) { @@ -31,7 +31,7 @@ namespace Tango.Transport.Web private void OnProgress(long current) { - Progress?.Invoke(this, new WebFileDownloaderProgressEventArgs() + Progress?.Invoke(this, new StorageBlobProgressEventArgs() { Current = current, Total = _stream.Length, diff --git a/Software/Visual_Studio/Tango.Transport/Web/WebTransportClient.cs b/Software/Visual_Studio/Tango.Transport/Web/WebTransportClient.cs index 4d68dbf59..ed2e69468 100644 --- a/Software/Visual_Studio/Tango.Transport/Web/WebTransportClient.cs +++ b/Software/Visual_Studio/Tango.Transport/Web/WebTransportClient.cs @@ -21,12 +21,6 @@ namespace Tango.Transport.Web public string AuthenticationToken { get; set; } - public TimeSpan RequestTimeout - { - get { return _httpClient.Timeout; } - set { _httpClient.Timeout = value; } - } - static WebTransportClient() { _settings = new JsonSerializerSettings() @@ -103,53 +97,30 @@ namespace Tango.Transport.Web } catch (HttpRequestException ex) { - bool handled = false; + String message = JObject.Parse(data).GetValue("Message").ToString(); + Exception exception = null; try { - String message = JObject.Parse(data).GetValue("Message").ToString(); - Exception exception = null; - - try + String exceptionMessage = JObject.Parse(data).GetValue("ExceptionMessage").ToString(); + String exceptionType = JObject.Parse(data).GetValue("ExceptionType").ToString(); + String stackTrace = JObject.Parse(data).GetValue("StackTrace").ToString(); + Type type = GetType(exceptionType); + if (type != null) { - String exceptionMessage = JObject.Parse(data).GetValue("ExceptionMessage").ToString(); - String exceptionType = JObject.Parse(data).GetValue("ExceptionType").ToString(); - String stackTrace = JObject.Parse(data).GetValue("StackTrace").ToString(); - Type type = GetType(exceptionType); - if (type != null) - { - exception = Activator.CreateInstance(type, new object[] { exceptionMessage + "\n" + stackTrace }) as Exception; - - } - else - { - exception = new HttpException(exceptionMessage + "\n" + stackTrace); - } + exception = Activator.CreateInstance(type, new object[] { exceptionMessage + "\n" + stackTrace }) as Exception; } - catch + else { - if (message == null) - { - Logging.LogManager.Default.Log($"Error parsing response message!\n{data}"); - } - - throw new HttpRequestException(ex.Message + " " + message); + exception = new HttpException(exceptionMessage + "\n" + stackTrace); } - - handled = true; - throw exception; } - catch (Exception handledException) + catch { - if (handled) - { - throw handledException; - } - else - { - throw ex; - } + throw new HttpRequestException(ex.Message + " " + message); } + + throw exception; } return JsonConvert.DeserializeObject<Response>(data); |
