blob: 26433bae7b713931651ce2ed20b81813b7186b3c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
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();
}
}
}
}
|