Archived
1
0
This repository has been archived on 2026-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
FoxTube/FoxTube.Core/Models/DownloadItem.cs
T
2020-05-09 23:16:19 +03:00

42 lines
1.1 KiB
C#

using System;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage;
using YoutubeExplode;
using YoutubeExplode.Videos.Streams;
namespace FoxTube.Models
{
public enum DownloadState { Initializing, Downloading, Cancelling }
public class DownloadItem : SavedVideo
{
public DownloadState State { get; private set; } = DownloadState.Initializing;
public IProgress<double> DownloadPercentage { get; set; }
private CancellationTokenSource CTS { get; set; } = new CancellationTokenSource();
public async Task CommenceDownload(IStreamInfo stream, IStorageFile destination)
{
Path = destination.Path;
YoutubeClient client = new YoutubeClient(UserManagement.Service.HttpClient);
State = DownloadState.Downloading;
Task task = client.Videos.Streams.DownloadAsync(stream, Path, DownloadPercentage, CTS.Token);
await task.ConfigureAwait(false);
if (!task.IsCanceled)
return;
await destination.DeleteAsync(StorageDeleteOption.PermanentDelete);
throw new OperationCanceledException();
}
public void Cancel()
{
State = DownloadState.Cancelling;
CTS.Cancel();
}
}
}