Skip to content

Commit

Permalink
Fix Download
Browse files Browse the repository at this point in the history
  • Loading branch information
MaKrotos committed Apr 1, 2024
1 parent 04b4d66 commit dda4d52
Show file tree
Hide file tree
Showing 14 changed files with 430 additions and 128 deletions.
1 change: 1 addition & 0 deletions VK UI3 (Package)/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@

<Capabilities>
<rescap:Capability Name="runFullTrust" />
<Capability Name="internetClient"/>
</Capabilities>
</Package>
1 change: 0 additions & 1 deletion VK UI3 (Package)/VK UI3 (Package).wapproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
</AppxManifest>
</ItemGroup>
<ItemGroup>
<None Include="Certificate.pfx" />
<Content Include="Images\BadgeLogo.scale-100.png" />
<Content Include="Images\BadgeLogo.scale-125.png" />
<Content Include="Images\BadgeLogo.scale-150.png" />
Expand Down
2 changes: 1 addition & 1 deletion VK UI3/Controls/TrackControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void TrackControl_DataContextChanged(FrameworkElement sender, DataContex


AddRemove.Visibility = Visibility.Visible;
AddRemove.Text = isOwner ? "Удалить" : "Добавть";
AddRemove.Text = isOwner ? "Удалить" : "Добавить";
AddRemove.Icon = new SymbolIcon(isOwner ? Symbol.Delete : Symbol.Add);

if (dataTrack.iVKGetAudio is PlayListVK aplaylist && aplaylist.playlist.Permissions.Edit)
Expand Down
114 changes: 100 additions & 14 deletions VK UI3/DownloadTrack/CheckFFmpeg.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,73 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using vkPosterBot.DB;

namespace VK_UI3.DownloadTrack
{
public class CheckFFmpeg
{
public async Task<double?> getFileSizeInMBAsync()
{
try
{
using (var client = new HttpClient())
{
var response = await client.GetAsync(getLinkFFMPEG(), HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
var contentLength = response.Content.Headers.ContentLength;
return contentLength.HasValue ? (double)contentLength.Value / (1024 * 1024) : (double?)null;
}
}
}
catch
{

}
return null;
}

public string getPathFfmpeg()
{
var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
path = Path.Combine(path, "VKMMKZ");
path = Path.Combine(path, "FFMPeg.exe");
return path;
}
public bool isExist()
{
if (File.Exists(getPathFfmpeg()))
{

return true;
}
return false;
}
public string? getLinkFFMPEG()
{
switch (RuntimeInformation.OSArchitecture)
{
case Architecture.X86:
return "https://github.com/MaKrotos/Music-M/releases/download/0.1.0.0/Microsoft.WindowsAppRuntimeX64.1.4.msix";
return "https://github.com/MaKrotos/Music-M/releases/download/0.1.0.0/ffmpegX32.exe";
case Architecture.X64:
return "https://github.com/MaKrotos/Music-M/releases/download/0.1.0.0/Microsoft.WindowsAppRuntimeX86.1.4.msix";
return "https://github.com/MaKrotos/Music-M/releases/download/0.1.0.0/ffmpegX64.exe";
default:
return null;
}
}
}

public class DownloadFileWithProgress
{
public bool isNowDownload = false;
public delegate void DownloadCompletedHandler(object sender, EventArgs e);
public event DownloadCompletedHandler DownloadCompleted;

Expand All @@ -36,31 +77,76 @@ public class DownloadFileWithProgress
private WebClient webClient;
private CheckFFmpeg checkFFmpeg = new CheckFFmpeg();

public DownloadFileWithProgress(string destination)

public DownloadFileWithProgress()
{
webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChangedEvent);
this.DownloadFile(destination);
if (MainWindow.downloadFileWithProgress == null)
{
MainWindow.downloadFileWithProgress = this;


webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChangedEvent);
if (!checkFFmpeg.isExist())
{
MainWindow.mainWindow.requstDownloadFFMpegAsync();
}
}
else {

MainWindow.mainWindow.MainWindow_showDownload();
}
}

public void DownloadFile(string destination)
public double? mb;

public async void DownloadFile()
{
string? url = checkFFmpeg.getLinkFFMPEG();
if (url != null)
{
webClient.DownloadFileAsync(new Uri(url), destination);
}
_= Task.Run(async () =>
{
string path = checkFFmpeg.getPathFfmpeg() + ".temp";
if (File.Exists(path)) { File.Delete(path); }

mb = await checkFFmpeg.getFileSizeInMBAsync();
string? url = checkFFmpeg.getLinkFFMPEG();
if (url != null)
{
webClient.OpenRead(url);


webClient.DownloadFileAsync(new Uri(url), path);
}
});
}

public void CancelDownload()
{
webClient.CancelAsync();
foreach (var item in PlayListDownload.PlayListDownloads)
{
item.Cancel();
}
MainWindow.downloadFileWithProgress = null;
}

private void Completed(object sender, AsyncCompletedEventArgs e)
{

string path = checkFFmpeg.getPathFfmpeg();
string temp = path + ".temp";

File.Move(temp, path);

DownloadCompleted?.Invoke(this, e);
MainWindow.downloadFileWithProgress = null;
if (SettingsTable.GetSetting("downloadALL") == null)
PlayListDownload.ResumeOnlyFirst();
else
PlayListDownload.ResumeAll();


MainWindow.mainWindow.MainWindow_showDownload();
}

private void ProgressChangedEvent(object sender, DownloadProgressChangedEventArgs e)
Expand Down
88 changes: 66 additions & 22 deletions VK UI3/DownloadTrack/PlayListDownload.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using Microsoft.UI.Dispatching;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using VK_UI3.Views.Download;
using VK_UI3.VKs.IVK;
using VkNet.Model.Attachments;
using vkPosterBot.DB;
Expand All @@ -17,6 +19,7 @@ namespace VK_UI3.DownloadTrack
public class PlayListDownload
{
public static ObservableCollection<PlayListDownload> PlayListDownloads = new ObservableCollection<PlayListDownload>();
public static List<IVKGetAudio> PlayListDownloadsList = new List<IVKGetAudio>();


CancellationTokenSource cts = new CancellationTokenSource();
Expand All @@ -27,21 +30,19 @@ public class PlayListDownload
private bool pause;
public IVKGetAudio iVKGetAudio;

public WeakEventManager OnTrackDownloaded = new WeakEventManager();
public void TrackDownloaded() { OnTrackDownloaded?.RaiseEvent(this, EventArgs.Empty); }
public EventHandler OnTrackDownloaded;
public void TrackDownloaded() { OnTrackDownloaded?.Invoke(this, EventArgs.Empty); }



public WeakEventManager onStatusUpdate = new WeakEventManager();
public void StatusUpdate() { onStatusUpdate?.RaiseEvent(this, EventArgs.Empty); }
public EventHandler onStatusUpdate;
public void StatusUpdate() { onStatusUpdate?.Invoke(this, EventArgs.Empty); }


public static WeakEventManager OnStartDownload = new WeakEventManager();
public void StartDownload() { OnStartDownload?.RaiseEvent(this, EventArgs.Empty); }


public static WeakEventManager OnEndAllDownload = new WeakEventManager();
public void EndAllDownload() { OnEndAllDownload?.RaiseEvent(this, EventArgs.Empty); }
public static EventHandler OnEndAllDownload;
public void EndAllDownload() { OnEndAllDownload?.Invoke(this, EventArgs.Empty); }

public string location;

Expand All @@ -63,7 +64,9 @@ public PlayListDownload(IVKGetAudio iVKGetAudio, string path, DispatcherQueue di
this.iVKGetAudio = iVKGetAudio;
this.dispatcherQueue = dispatcherQueue;
if (iVKGetAudio is SectionAudio) return;
PlayListDownloads.Add(this);
if (PlayListDownloadsList.Contains(iVKGetAudio)) return;



foreach (char c in Path.GetInvalidPathChars())
{
Expand All @@ -82,22 +85,40 @@ public PlayListDownload(IVKGetAudio iVKGetAudio, string path, DispatcherQueue di

this.location = path + "\\" + name;

if (SettingsTable.GetSetting("downloadALL") == null && PlayListDownloads.IndexOf(this) != 0) Pause();
PlayListDownloadAsync();


if (!new CheckFFmpeg().isExist())
{
new DownloadFileWithProgress();
this.Pause();
}else
{
MainWindow.mainWindow.MainWindow_showDownload();
}

dispatcherQueue.TryEnqueue(() =>
{
PlayListDownloads.Add(this);

PlayListDownloadsList.Add(iVKGetAudio);
if (SettingsTable.GetSetting("downloadALL") == null && PlayListDownloads.IndexOf(this) != 0)
Pause();
PlayListDownloadAsync();
});
}

public async Task PlayListDownloadAsync()
{
if (PlayListDownloads.Count() == 1)
StartDownload();


_ = Task.Run(() =>
{
try
{
string appPath = AppDomain.CurrentDomain.BaseDirectory;


string ffmpegPath = Path.Combine(appPath, "DownloadTrack", "ffmpeg.exe");
string ffmpegPath = new CheckFFmpeg().getPathFfmpeg();

var i = 0;
while (iVKGetAudio.countTracks > downloaded)
Expand Down Expand Up @@ -164,7 +185,9 @@ public async Task PlayListDownloadAsync()

dispatcherQueue.TryEnqueue(async () =>
{
OnTrackDownloaded.RaiseEvent(this, new TrackDownloadedEventArgs { Downloaded = downloaded, Total = (int)iVKGetAudio.countTracks });

OnTrackDownloaded?.Invoke(this, new TrackDownloadedEventArgs { Downloaded = downloaded, Total = (int)iVKGetAudio.countTracks });

});

i++;
Expand All @@ -182,6 +205,7 @@ public async Task PlayListDownloadAsync()
dispatcherQueue.TryEnqueue(async () =>
{
PlayListDownloads.Remove(this);
PlayListDownloadsList.Remove(iVKGetAudio);
if (PlayListDownloads.Count() == 0) EndAllDownload();
});

Expand All @@ -190,7 +214,10 @@ public async Task PlayListDownloadAsync()
{

this.error = true;
StatusUpdate();
dispatcherQueue.TryEnqueue(async () =>
{
StatusUpdate();
});
}
}, cts.Token);
}
Expand All @@ -201,8 +228,8 @@ public static void ResumeOnlyFirst()
{
item.Pause();
}

PlayListDownloads.FirstOrDefault()?.Resume();
if (PlayListDownloads.Count != 0)
PlayListDownloads[0].Resume();
}

public static void ResumeAll()
Expand Down Expand Up @@ -253,25 +280,42 @@ public void Pause()
{
pauseEvent.Reset();
pause = true;
StatusUpdate();

dispatcherQueue.TryEnqueue(async () =>
{
StatusUpdate();
});
}

public void Resume()
{
if (MainWindow.downloadFileWithProgress != null) return;
pauseEvent.Set();
pause = false;
StatusUpdate();

dispatcherQueue.TryEnqueue(async () =>
{
StatusUpdate();
});

}

public void Cancel() {
if (error)
{
PlayListDownloads.Remove(this);
PlayListDownloadsList.Remove(iVKGetAudio);
return;
}
Resume();

cts.Cancel();

pause = true;
pauseEvent.Set();

dispatcherQueue.TryEnqueue(async () =>
{
StatusUpdate();
});
}


Expand Down
Loading

0 comments on commit dda4d52

Please sign in to comment.