2022-05-11 20:50:50 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using VDownload.Core.Enums;
|
|
|
|
|
|
using VDownload.Core.EventArgs;
|
|
|
|
|
|
using VDownload.Core.Interfaces;
|
|
|
|
|
|
using VDownload.Core.Services;
|
|
|
|
|
|
using Windows.ApplicationModel.ExtendedExecution;
|
|
|
|
|
|
using Windows.Storage;
|
|
|
|
|
|
using Windows.Storage.AccessCache;
|
|
|
|
|
|
|
|
|
|
|
|
namespace VDownload.Core.Structs
|
|
|
|
|
|
{
|
|
|
|
|
|
public class DownloadTask
|
|
|
|
|
|
{
|
|
|
|
|
|
#region CONSTRUCTORS
|
|
|
|
|
|
|
|
|
|
|
|
public DownloadTask(string id, IVideo video, MediaType mediaType, BaseStream selectedStream, TrimData trim, OutputFile file, double schedule)
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = id;
|
|
|
|
|
|
Video = video;
|
|
|
|
|
|
MediaType = mediaType;
|
|
|
|
|
|
SelectedStream = selectedStream;
|
|
|
|
|
|
Trim = trim;
|
|
|
|
|
|
File = file;
|
|
|
|
|
|
Schedule = schedule;
|
|
|
|
|
|
|
|
|
|
|
|
Status = DownloadTaskStatus.Idle;
|
2022-05-15 22:13:47 +02:00
|
|
|
|
LastStatusChangedEventArgs = new DownloadTaskStatusChangedEventArgs(Status);
|
2022-05-11 20:50:50 +02:00
|
|
|
|
CancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region PROPERTIES
|
|
|
|
|
|
|
|
|
|
|
|
public string Id { get; set; }
|
|
|
|
|
|
public IVideo Video { get; set; }
|
|
|
|
|
|
public MediaType MediaType { get; set; }
|
|
|
|
|
|
public BaseStream SelectedStream { get; set; }
|
|
|
|
|
|
public TrimData Trim { get; set; }
|
|
|
|
|
|
public OutputFile File { get; set; }
|
|
|
|
|
|
public double Schedule { get; set; }
|
|
|
|
|
|
public DownloadTaskStatus Status { get; private set; }
|
2022-05-15 22:13:47 +02:00
|
|
|
|
public DownloadTaskStatusChangedEventArgs LastStatusChangedEventArgs { get; private set; }
|
2022-05-11 20:50:50 +02:00
|
|
|
|
public CancellationTokenSource CancellationTokenSource { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region METHODS
|
|
|
|
|
|
|
|
|
|
|
|
public async Task Run(bool delayWhenOnMeteredConnection)
|
|
|
|
|
|
{
|
2022-05-15 22:13:47 +02:00
|
|
|
|
StatusChanged.Invoke(this, new DownloadTaskStatusChangedEventArgs(Status));
|
2022-05-11 20:50:50 +02:00
|
|
|
|
|
|
|
|
|
|
CancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
|
|
|
|
|
|
|
if (Schedule > 0)
|
|
|
|
|
|
{
|
2022-05-15 22:13:47 +02:00
|
|
|
|
DateTime scheduleFor = DateTime.Now.AddMinutes(Schedule);
|
2022-05-11 20:50:50 +02:00
|
|
|
|
Status = DownloadTaskStatus.Scheduled;
|
2022-05-15 22:13:47 +02:00
|
|
|
|
LastStatusChangedEventArgs = new DownloadTaskStatusChangedEventArgs(Status, scheduleFor);
|
|
|
|
|
|
StatusChanged.Invoke(this, LastStatusChangedEventArgs);
|
|
|
|
|
|
while (DateTime.Now < scheduleFor && !CancellationTokenSource.Token.IsCancellationRequested)
|
|
|
|
|
|
{
|
|
|
|
|
|
await Task.Delay(100);
|
|
|
|
|
|
}
|
2022-05-11 20:50:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Status = DownloadTaskStatus.Queued;
|
2022-05-15 22:13:47 +02:00
|
|
|
|
LastStatusChangedEventArgs = new DownloadTaskStatusChangedEventArgs(Status);
|
|
|
|
|
|
StatusChanged.Invoke(this, LastStatusChangedEventArgs);
|
2022-05-11 20:50:50 +02:00
|
|
|
|
await DownloadTasksCollectionManagement.WaitInQueue(delayWhenOnMeteredConnection, CancellationTokenSource.Token);
|
|
|
|
|
|
|
|
|
|
|
|
if (!CancellationTokenSource.Token.IsCancellationRequested)
|
|
|
|
|
|
{
|
|
|
|
|
|
Status = DownloadTaskStatus.Downloading;
|
2022-05-15 22:13:47 +02:00
|
|
|
|
LastStatusChangedEventArgs = new DownloadTaskStatusChangedEventArgs(Status, 0);
|
|
|
|
|
|
StatusChanged.Invoke(this, LastStatusChangedEventArgs);
|
2022-05-11 20:50:50 +02:00
|
|
|
|
|
|
|
|
|
|
StorageFolder tempFolder;
|
2022-05-15 22:13:47 +02:00
|
|
|
|
if (StorageApplicationPermissions.FutureAccessList.ContainsItem("custom_temp_location"))
|
2022-05-11 20:50:50 +02:00
|
|
|
|
{
|
|
|
|
|
|
tempFolder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync("custom_temp_location");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
tempFolder = ApplicationData.Current.TemporaryFolder;
|
|
|
|
|
|
}
|
|
|
|
|
|
tempFolder = await tempFolder.CreateFolderAsync(Id);
|
|
|
|
|
|
|
|
|
|
|
|
bool endedWithError = false;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
CancellationTokenSource.Token.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
|
|
Stopwatch taskStopwatch = Stopwatch.StartNew();
|
|
|
|
|
|
|
|
|
|
|
|
ExtendedExecutionSession session = new ExtendedExecutionSession { Reason = ExtendedExecutionReason.Unspecified };
|
|
|
|
|
|
await session.RequestExtensionAsync();
|
|
|
|
|
|
CancellationTokenSource.Token.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
|
|
Video.DownloadingProgressChanged += DownloadingProgressChanged;
|
|
|
|
|
|
Video.ProcessingProgressChanged += ProcessingProgressChanged;
|
|
|
|
|
|
StorageFile tempOutputFile = await Video.DownloadAndTranscodeAsync(tempFolder, SelectedStream, File.Extension, MediaType, Trim, CancellationTokenSource.Token);
|
|
|
|
|
|
|
|
|
|
|
|
session.Dispose();
|
|
|
|
|
|
|
|
|
|
|
|
Status = DownloadTaskStatus.Finalizing;
|
2022-05-15 22:13:47 +02:00
|
|
|
|
LastStatusChangedEventArgs = new DownloadTaskStatusChangedEventArgs(Status);
|
|
|
|
|
|
StatusChanged.Invoke(this, LastStatusChangedEventArgs);
|
2022-05-11 20:50:50 +02:00
|
|
|
|
|
|
|
|
|
|
StorageFile outputFile = await File.Create();
|
|
|
|
|
|
|
|
|
|
|
|
CancellationTokenSource.Token.ThrowIfCancellationRequested();
|
|
|
|
|
|
await tempOutputFile.MoveAndReplaceAsync(outputFile);
|
|
|
|
|
|
|
|
|
|
|
|
taskStopwatch.Stop();
|
|
|
|
|
|
|
|
|
|
|
|
Status = DownloadTaskStatus.EndedSuccessfully;
|
2022-05-15 22:13:47 +02:00
|
|
|
|
LastStatusChangedEventArgs = new DownloadTaskStatusChangedEventArgs(Status, taskStopwatch.Elapsed);
|
|
|
|
|
|
StatusChanged.Invoke(this, LastStatusChangedEventArgs);
|
2022-05-11 20:50:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
endedWithError = true;
|
|
|
|
|
|
Status = DownloadTaskStatus.EndedUnsuccessfully;
|
2022-05-15 22:13:47 +02:00
|
|
|
|
LastStatusChangedEventArgs = new DownloadTaskStatusChangedEventArgs(Status, ex);
|
|
|
|
|
|
StatusChanged.Invoke(this, LastStatusChangedEventArgs);
|
2022-05-11 20:50:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!endedWithError || (bool)Config.GetValue("delete_task_temp_when_ended_with_error"))
|
|
|
|
|
|
{
|
|
|
|
|
|
// Delete temporary files
|
|
|
|
|
|
await tempFolder.DeleteAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Status = DownloadTaskStatus.EndedUnsuccessfully;
|
2022-05-15 22:13:47 +02:00
|
|
|
|
LastStatusChangedEventArgs = new DownloadTaskStatusChangedEventArgs(Status, new OperationCanceledException(CancellationTokenSource.Token));
|
|
|
|
|
|
StatusChanged.Invoke(this, LastStatusChangedEventArgs);
|
2022-05-11 20:50:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void DownloadingProgressChanged(object sender, ProgressChangedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Status = DownloadTaskStatus.Downloading;
|
2022-05-15 22:13:47 +02:00
|
|
|
|
LastStatusChangedEventArgs = new DownloadTaskStatusChangedEventArgs(Status, e.Progress);
|
|
|
|
|
|
StatusChanged.Invoke(this, LastStatusChangedEventArgs);
|
2022-05-11 20:50:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ProcessingProgressChanged(object sender, ProgressChangedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Status = DownloadTaskStatus.Processing;
|
2022-05-15 22:13:47 +02:00
|
|
|
|
LastStatusChangedEventArgs = new DownloadTaskStatusChangedEventArgs(Status, e.Progress);
|
|
|
|
|
|
StatusChanged.Invoke(this, LastStatusChangedEventArgs);
|
2022-05-11 20:50:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region EVENT
|
|
|
|
|
|
|
2022-05-15 22:13:47 +02:00
|
|
|
|
public event EventHandler<DownloadTaskStatusChangedEventArgs> StatusChanged;
|
2022-05-11 20:50:50 +02:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|