diff --git a/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs b/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs index eb5190d..70ff2fd 100644 --- a/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs +++ b/CRD/Downloader/Crunchyroll/CrunchyrollManager.cs @@ -223,7 +223,7 @@ public class CrunchyrollManager{ QueueManager.Instance.Queue.Refresh(); - var fileNameAndPath = CrunOptions.DownloadToTempFolder ? Path.Combine(res.TempFolderPath, res.FileName) : Path.Combine(res.FolderPath, res.FileName); + var fileNameAndPath = CrunOptions.DownloadToTempFolder ? Path.Combine(res.TempFolderPath ?? string.Empty, res.FileName ?? string.Empty) : Path.Combine(res.FolderPath ?? string.Empty, res.FileName ?? string.Empty); if (CrunOptions is{ DlVideoOnce: false, KeepDubsSeperate: true }){ var groupByDub = Helpers.GroupByLanguageWithSubtitles(res.Data); var mergers = new List(); @@ -255,7 +255,9 @@ public class CrunchyrollManager{ foreach (var merger in mergers){ merger.CleanUp(); - await MoveFromTempFolder(merger, data, res.TempFolderPath, res.Data.Where(e => e.Type == DownloadMediaType.Subtitle)); + if (CrunOptions.DownloadToTempFolder){ + await MoveFromTempFolder(merger, data, res.TempFolderPath, res.Data.Where(e => e.Type == DownloadMediaType.Subtitle)); + } } } else{ var result = await MuxStreams(res.Data, @@ -280,7 +282,9 @@ public class CrunchyrollManager{ if (result is{ merger: not null, isMuxed: true }){ result.merger.CleanUp(); + } + if (CrunOptions.DownloadToTempFolder){ await MoveFromTempFolder(result.merger, data, res.TempFolderPath, res.Data.Where(e => e.Type == DownloadMediaType.Subtitle)); } } @@ -315,7 +319,7 @@ public class CrunchyrollManager{ #region Temp Files Move - private async Task MoveFromTempFolder(Merger merger, CrunchyEpMeta data, string tempFolderPath, IEnumerable subtitles){ + private async Task MoveFromTempFolder(Merger? merger, CrunchyEpMeta data, string tempFolderPath, IEnumerable subtitles){ if (!CrunOptions.DownloadToTempFolder) return; data.DownloadProgress = new DownloadProgress{ @@ -335,19 +339,17 @@ public class CrunchyrollManager{ } // Move the main output file - await MoveFile(merger.options.Output, tempFolderPath, data.DownloadPath); + await MoveFile(merger?.options.Output ?? string.Empty, tempFolderPath, data.DownloadPath ?? CfgManager.PathVIDEOS_DIR); // Move the subtitle files - if (CrunOptions.SkipSubsMux){ - foreach (var downloadedMedia in subtitles){ - await MoveFile(downloadedMedia.Path ?? string.Empty, tempFolderPath, data.DownloadPath); - } + foreach (var downloadedMedia in subtitles){ + await MoveFile(downloadedMedia.Path ?? string.Empty, tempFolderPath, data.DownloadPath ?? CfgManager.PathVIDEOS_DIR); } } private async Task MoveFile(string sourcePath, string tempFolderPath, string downloadPath){ if (string.IsNullOrEmpty(sourcePath) || !File.Exists(sourcePath)){ - Console.Error.WriteLine("Source file does not exist or path is invalid."); + // Console.Error.WriteLine("Source file does not exist or path is invalid."); return; } @@ -364,7 +366,7 @@ public class CrunchyrollManager{ ? CrunOptions.DownloadDirPath : CfgManager.PathVIDEOS_DIR; - var destinationPath = Path.Combine(destinationFolder, fileName); + var destinationPath = Path.Combine(destinationFolder ?? string.Empty, fileName ?? string.Empty); string destinationDirectory = Path.GetDirectoryName(destinationPath); if (string.IsNullOrEmpty(destinationDirectory)){ @@ -1137,6 +1139,7 @@ public class CrunchyrollManager{ Console.WriteLine($"An error occurred: {ex.Message}"); } + dlFailed = true; return new DownloadResponse{ Data = files, Error = dlFailed, @@ -1202,6 +1205,7 @@ public class CrunchyrollManager{ Console.WriteLine($"An error occurred: {ex.Message}"); } + dlFailed = true; return new DownloadResponse{ Data = files, Error = dlFailed, @@ -1393,6 +1397,9 @@ public class CrunchyrollManager{ !string.IsNullOrEmpty(options.DownloadDirPath) ? options.DownloadDirPath : CfgManager.PathVIDEOS_DIR; } + if (string.IsNullOrEmpty(data.DownloadPath)){ + data.DownloadPath = fileDir; + } return new DownloadResponse{ Data = files, diff --git a/CRD/Utils/Files/CfgManager.cs b/CRD/Utils/Files/CfgManager.cs index 4aa38a3..820d45c 100644 --- a/CRD/Utils/Files/CfgManager.cs +++ b/CRD/Utils/Files/CfgManager.cs @@ -17,7 +17,7 @@ using YamlDotNet.Serialization.NamingConventions; namespace CRD.Utils; public class CfgManager{ - private static string WorkingDirectory = Directory.GetCurrentDirectory(); + private static string WorkingDirectory = AppContext.BaseDirectory; public static readonly string PathCrToken = Path.Combine(WorkingDirectory, "config", "cr_token.yml"); public static readonly string PathCrDownloadOptions = Path.Combine(WorkingDirectory, "config", "settings.yml"); diff --git a/CRD/Utils/Structs/Structs.cs b/CRD/Utils/Structs/Structs.cs index 6a5c0b1..2af6269 100644 --- a/CRD/Utils/Structs/Structs.cs +++ b/CRD/Utils/Structs/Structs.cs @@ -66,10 +66,10 @@ public struct Episode{ public struct DownloadResponse{ public List Data{ get; set; } - public string FileName{ get; set; } + public string? FileName{ get; set; } - public string FolderPath{ get; set; } - public string TempFolderPath{ get; set; } + public string? FolderPath{ get; set; } + public string? TempFolderPath{ get; set; } public string VideoTitle{ get; set; } public bool Error{ get; set; } diff --git a/CRD/Views/MainWindow.axaml.cs b/CRD/Views/MainWindow.axaml.cs index 263ceab..5dd13e7 100644 --- a/CRD/Views/MainWindow.axaml.cs +++ b/CRD/Views/MainWindow.axaml.cs @@ -7,6 +7,7 @@ using Avalonia.Interactivity; using Avalonia.Markup.Xaml; using Avalonia.Media; using Avalonia.Platform; +using CRD.Downloader.Crunchyroll; using CRD.Utils; using CRD.Utils.Files; using CRD.Utils.Structs; @@ -64,7 +65,7 @@ public partial class MainWindow : AppWindow{ TitleBar.Height = TitleBarHeightAdjustment; TitleBar.ExtendsContentIntoTitleBar = true; TitleBar.TitleBarHitTestType = TitleBarHitTestType.Complex; - + Opened += OnOpened; Closing += OnClosing; @@ -73,7 +74,7 @@ public partial class MainWindow : AppWindow{ PositionChanged += OnPositionChanged; SizeChanged += OnSizeChanged; - + //select first element as default var nv = this.FindControl("NavView"); nv.SelectedItem = nv.MenuItems.ElementAt(0); @@ -195,7 +196,6 @@ public partial class MainWindow : AppWindow{ var screens = Screens.All; if (settings.ScreenIndex >= 0 && settings.ScreenIndex < screens.Count){ var screen = screens[settings.ScreenIndex]; - var screenBounds = screen.Bounds; // Restore the position first Position = new PixelPoint(settings.PosX, settings.PosY + TitleBarHeightAdjustment); @@ -209,7 +209,7 @@ public partial class MainWindow : AppWindow{ _restorePosition = new PixelPoint(settings.PosX, settings.PosY + TitleBarHeightAdjustment); // Ensure the window is on the correct screen before maximizing - Position = new PixelPoint(settings.PosX, settings.PosY+ TitleBarHeightAdjustment); + Position = new PixelPoint(settings.PosX, settings.PosY + TitleBarHeightAdjustment); } if (settings.IsMaximized){ @@ -232,15 +232,17 @@ public partial class MainWindow : AppWindow{ } var settings = new WindowSettings{ - Width = this.WindowState == WindowState.Maximized ? _restoreSize.Width : Width, - Height = this.WindowState == WindowState.Maximized ? _restoreSize.Height : Height, + Width = WindowState == WindowState.Maximized ? _restoreSize.Width : Width, + Height = WindowState == WindowState.Maximized ? _restoreSize.Height : Height, ScreenIndex = screenIndex, - PosX = this.WindowState == WindowState.Maximized ? _restorePosition.X : Position.X, - PosY = this.WindowState == WindowState.Maximized ? _restorePosition.Y : Position.Y, - IsMaximized = this.WindowState == WindowState.Maximized + PosX = WindowState == WindowState.Maximized ? _restorePosition.X : Position.X, + PosY = WindowState == WindowState.Maximized ? _restorePosition.Y : Position.Y, + IsMaximized = WindowState == WindowState.Maximized }; File.WriteAllText(CfgManager.PathWindowSettings, JsonConvert.SerializeObject(settings, Formatting.Indented)); + + FileNameManager.DeleteEmptyFolders(!string.IsNullOrEmpty(CrunchyrollManager.Instance.CrunOptions.DownloadTempDirPath) ? CrunchyrollManager.Instance.CrunOptions.DownloadTempDirPath : CfgManager.PathTEMP_DIR); } private void OnWindowStateChanged(object sender, AvaloniaPropertyChangedEventArgs e){