44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
using System.Net;
|
|
|
|
namespace YandexMusic.API.Common
|
|
{
|
|
/// <summary>
|
|
/// Загрузчик файлов по ссылке
|
|
/// </summary>
|
|
public class DataDownloader
|
|
{
|
|
private AuthStorage authStorage;
|
|
|
|
private async Task<HttpContent> GetResponseContent(string url, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead)
|
|
{
|
|
HttpRequestMessage message = new(new HttpMethod(WebRequestMethods.Http.Get), url);
|
|
|
|
HttpResponseMessage response = await authStorage.Provider.GetWebResponseAsync(message, httpCompletionOption);
|
|
return response.Content;
|
|
}
|
|
|
|
public async Task<Stream> AsStream(string url, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead)
|
|
{
|
|
HttpContent content = await GetResponseContent(url, httpCompletionOption);
|
|
return await content.ReadAsStreamAsync();
|
|
}
|
|
|
|
public async Task<byte[]> AsBytes(string url)
|
|
{
|
|
HttpContent content = await GetResponseContent(url);
|
|
return await content.ReadAsByteArrayAsync();
|
|
}
|
|
|
|
public async Task ToFile(string url, string fileName)
|
|
{
|
|
using Stream stream = await AsStream(url);
|
|
using FileStream fs = File.Create(fileName);
|
|
await stream.CopyToAsync(fs);
|
|
}
|
|
|
|
public DataDownloader(AuthStorage storage)
|
|
{
|
|
authStorage = storage;
|
|
}
|
|
}
|
|
} |