mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-08-11 09:13:42 -04:00
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:
parent
54878b14f4
commit
fdc925812d
@ -1,4 +1,7 @@
|
|||||||
using System.IO;
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using API.Services;
|
||||||
|
|
||||||
namespace API.Extensions
|
namespace API.Extensions
|
||||||
{
|
{
|
||||||
@ -37,26 +40,32 @@ namespace API.Extensions
|
|||||||
/// <param name="directory"></param>
|
/// <param name="directory"></param>
|
||||||
public static void Flatten(this DirectoryInfo directory)
|
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())
|
foreach (var file in directory.EnumerateFiles())
|
||||||
{
|
{
|
||||||
if (file.Directory == null) continue;
|
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);
|
var newPath = Path.Join(root.FullName, newName);
|
||||||
if (!File.Exists(newPath)) file.MoveTo(newPath);
|
if (!File.Exists(newPath)) file.MoveTo(newPath);
|
||||||
|
fileIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
directoryIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var subDirectory in directory.EnumerateDirectories())
|
foreach (var subDirectory in directory.EnumerateDirectories())
|
||||||
{
|
{
|
||||||
FlattenDirectory(root, subDirectory);
|
FlattenDirectory(root, subDirectory, ref directoryIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -340,7 +340,7 @@ namespace API.Services
|
|||||||
{
|
{
|
||||||
entry.WriteToDirectory(extractPath, new ExtractionOptions()
|
entry.WriteToDirectory(extractPath, new ExtractionOptions()
|
||||||
{
|
{
|
||||||
ExtractFullPath = false,
|
ExtractFullPath = true, // Don't flatten, let the flatterner ensure correct order of nested folders
|
||||||
Overwrite = false
|
Overwrite = false
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -62,10 +62,11 @@ namespace API.Services
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fileCount > 1)
|
new DirectoryInfo(extractPath).Flatten();
|
||||||
{
|
// if (fileCount > 1)
|
||||||
new DirectoryInfo(extractPath).Flatten();
|
// {
|
||||||
}
|
// new DirectoryInfo(extractPath).Flatten();
|
||||||
|
// }
|
||||||
|
|
||||||
return chapter;
|
return chapter;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user