From fdc925812db8c7950927c8099368111019e84175 Mon Sep 17 00:00:00 2001 From: Joseph Milazzo Date: Thu, 20 May 2021 18:01:14 -0500 Subject: [PATCH] Bugfix: Flatten wasn't consistent (#227) * Ensure that when caching, the order of the cached files remains the same way as if we manually navigated through nested folders. --- API/Extensions/DirectoryInfoExtensions.cs | 23 ++++++++++++++++------- API/Services/ArchiveService.cs | 2 +- API/Services/CacheService.cs | 9 +++++---- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/API/Extensions/DirectoryInfoExtensions.cs b/API/Extensions/DirectoryInfoExtensions.cs index e7f65dc51..c41ca9f8b 100644 --- a/API/Extensions/DirectoryInfoExtensions.cs +++ b/API/Extensions/DirectoryInfoExtensions.cs @@ -1,4 +1,7 @@ -using System.IO; +using System; +using System.IO; +using System.Linq; +using API.Services; namespace API.Extensions { @@ -37,26 +40,32 @@ namespace API.Extensions /// public static void Flatten(this DirectoryInfo directory) { - FlattenDirectory(directory, directory); + var index = 0; + FlattenDirectory(directory, directory, ref index); } - private static void FlattenDirectory(DirectoryInfo root, DirectoryInfo directory) + private static void FlattenDirectory(DirectoryInfo root, DirectoryInfo directory, ref int directoryIndex) { - if (!root.FullName.Equals(directory.FullName)) // I might be able to replace this with root === directory + if (!root.FullName.Equals(directory.FullName)) { + var fileIndex = 1; foreach (var file in directory.EnumerateFiles()) { if (file.Directory == null) continue; - var newName = $"{file.Directory.Name}_{file.Name}"; + var paddedIndex = Parser.Parser.PadZeros(directoryIndex + ""); + // We need to rename the files so that after flattening, they are in the order we found them + var newName = $"{paddedIndex}_{fileIndex}.{file.Extension}"; var newPath = Path.Join(root.FullName, newName); if (!File.Exists(newPath)) file.MoveTo(newPath); - + fileIndex++; } + + directoryIndex++; } foreach (var subDirectory in directory.EnumerateDirectories()) { - FlattenDirectory(root, subDirectory); + FlattenDirectory(root, subDirectory, ref directoryIndex); } } } diff --git a/API/Services/ArchiveService.cs b/API/Services/ArchiveService.cs index b3ef35ee9..9adb19c0c 100644 --- a/API/Services/ArchiveService.cs +++ b/API/Services/ArchiveService.cs @@ -340,7 +340,7 @@ namespace API.Services { entry.WriteToDirectory(extractPath, new ExtractionOptions() { - ExtractFullPath = false, + ExtractFullPath = true, // Don't flatten, let the flatterner ensure correct order of nested folders Overwrite = false }); } diff --git a/API/Services/CacheService.cs b/API/Services/CacheService.cs index 4dcad4dc5..2ce9b375b 100644 --- a/API/Services/CacheService.cs +++ b/API/Services/CacheService.cs @@ -62,10 +62,11 @@ namespace API.Services } - if (fileCount > 1) - { - new DirectoryInfo(extractPath).Flatten(); - } + new DirectoryInfo(extractPath).Flatten(); + // if (fileCount > 1) + // { + // new DirectoryInfo(extractPath).Flatten(); + // } return chapter; }