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.
This commit is contained in:
Joseph Milazzo 2021-05-20 18:01:14 -05:00 committed by GitHub
parent 54878b14f4
commit fdc925812d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 12 deletions

View File

@ -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
/// <param name="directory"></param>
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);
}
}
}

View File

@ -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
});
}

View File

@ -62,10 +62,11 @@ namespace API.Services
}
if (fileCount > 1)
{
new DirectoryInfo(extractPath).Flatten();
}
// if (fileCount > 1)
// {
// new DirectoryInfo(extractPath).Flatten();
// }
return chapter;
}