Skip to content

Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.

Bugfix: Flatten wasn't consistent (#227)

Joseph Milazzofdc925812db8
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
/// <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);
}
}
}
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;
}