Implement DownloadFile() for Yandex file system provider
This commit is contained in:
Родитель
613c77df17
Коммит
60fef52181
|
@ -18,51 +18,20 @@ namespace Camelotia.Services.Providers
|
|||
public sealed class YandexFileSystemProvider : IProvider
|
||||
{
|
||||
private const string YandexAuthTokenUrl = "https://oauth.yandex.ru/token";
|
||||
private const string CloudApiDownloadFileUrl = "https://cloud-api.yandex.net/v1/disk/resources/download?path=";
|
||||
private const string CloudApiGetPathBase = "https://cloud-api.yandex.net:443/v1/disk/resources?path=";
|
||||
private const string SuccessContent = "<html><body>Please return to the app.</body></html>";
|
||||
private const string ClientSecret = "f14bfc0275a34ceea83d7de7f4b50898";
|
||||
private const string ClientId = "122661520b174cb5b85b4a3c26aa66f6";
|
||||
|
||||
private readonly ReplaySubject<bool> _isAuthorized = new ReplaySubject<bool>(1);
|
||||
private string _accessToken;
|
||||
private readonly HttpClient _http = new HttpClient();
|
||||
|
||||
public string Size => "Unknown";
|
||||
|
||||
public string Name => "Yandex Disk";
|
||||
|
||||
public string Description => "Yandex Disk file provider";
|
||||
|
||||
public async Task<IEnumerable<FileModel>> Get(string path)
|
||||
{
|
||||
using (var http = new HttpClient())
|
||||
{
|
||||
http.DefaultRequestHeaders.Accept.Clear();
|
||||
http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", _accessToken);
|
||||
|
||||
var encodedPath = WebUtility.UrlEncode(path);
|
||||
var pathUrl = CloudApiGetPathBase + encodedPath;
|
||||
using (var response = await http.GetAsync(pathUrl).ConfigureAwait(false))
|
||||
{
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var content = JsonConvert.DeserializeObject<YandexContentResponse>(json);
|
||||
var models = content.Embedded.Items
|
||||
.Select(file => new FileModel(
|
||||
file.Name,
|
||||
file.Path.Replace("disk:", ""),
|
||||
file.Type == "dir",
|
||||
file.Size));
|
||||
|
||||
return models;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Task DownloadFile(string from, Stream to) => Task.CompletedTask;
|
||||
|
||||
public Task UploadFile(string to, Stream from, string name) => Task.CompletedTask;
|
||||
|
||||
public IObservable<bool> IsAuthorized => _isAuthorized;
|
||||
|
||||
|
@ -71,10 +40,49 @@ namespace Camelotia.Services.Providers
|
|||
public bool SupportsOAuth => true;
|
||||
|
||||
public Task DirectAuth(string login, string password) => Task.CompletedTask;
|
||||
|
||||
public async Task<IEnumerable<FileModel>> Get(string path)
|
||||
{
|
||||
var encodedPath = WebUtility.UrlEncode(path);
|
||||
var pathUrl = CloudApiGetPathBase + encodedPath;
|
||||
using (var response = await _http.GetAsync(pathUrl).ConfigureAwait(false))
|
||||
{
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var content = JsonConvert.DeserializeObject<YandexContentResponse>(json);
|
||||
var models = content.Embedded.Items
|
||||
.Select(file => new FileModel(
|
||||
file.Name,
|
||||
file.Path.Replace("disk:", ""),
|
||||
file.Type == "dir",
|
||||
file.Size));
|
||||
|
||||
return models;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DownloadFile(string from, Stream to)
|
||||
{
|
||||
var encodedPath = WebUtility.UrlEncode(from);
|
||||
var pathUrl = CloudApiDownloadFileUrl + encodedPath;
|
||||
using (var response = await _http.GetAsync(pathUrl).ConfigureAwait(false))
|
||||
{
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var content = JsonConvert.DeserializeObject<YandexFileDownloadResponse>(json);
|
||||
using (var file = await _http.GetAsync(content.Href).ConfigureAwait(false))
|
||||
using (var stream = await file.Content.ReadAsStreamAsync().ConfigureAwait(false))
|
||||
await stream.CopyToAsync(to).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public Task UploadFile(string to, Stream from, string name) => Task.CompletedTask;
|
||||
|
||||
public Task Logout()
|
||||
{
|
||||
_accessToken = string.Empty;
|
||||
_http.DefaultRequestHeaders.Clear();
|
||||
_isAuthorized.OnNext(false);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
@ -83,7 +91,9 @@ namespace Camelotia.Services.Providers
|
|||
{
|
||||
var code = await GetAuthenticationCode();
|
||||
var token = await GetAuthenticationToken(code);
|
||||
_accessToken = token;
|
||||
_http.DefaultRequestHeaders.Clear();
|
||||
_http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
_http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", token);
|
||||
_isAuthorized.OnNext(true);
|
||||
}
|
||||
|
||||
|
@ -176,5 +186,11 @@ namespace Camelotia.Services.Providers
|
|||
[JsonProperty("created")]
|
||||
public DateTime Created { get; set; }
|
||||
}
|
||||
|
||||
internal class YandexFileDownloadResponse
|
||||
{
|
||||
[JsonProperty("href")]
|
||||
public string Href { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче