Добавьте файлы проекта.
This commit is contained in:
44
YandexMusic.API/Common/DataDownloader.cs
Normal file
44
YandexMusic.API/Common/DataDownloader.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user