Implemented the ability to flatten directories, esp useful with nested folders in archives.
This commit is contained in:
parent
56e8a0059e
commit
7f404a0ce9
7 changed files with 91 additions and 22 deletions
|
|
@ -30,7 +30,7 @@ namespace API.Comparators
|
|||
{
|
||||
bool c1 = Char.IsDigit(s1, i1);
|
||||
bool c2 = Char.IsDigit(s2, i2);
|
||||
var r = 0; // temp result
|
||||
int r; // temp result
|
||||
if(!c1 && !c2)
|
||||
{
|
||||
bool letter1 = Char.IsLetter(s1, i1);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
using API.DTOs;
|
||||
using API.Entities;
|
||||
using API.Interfaces;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,65 @@
|
|||
namespace API.Extensions
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace API.Extensions
|
||||
{
|
||||
public static class DirectoryInfoExtensions
|
||||
{
|
||||
public static void Empty(this System.IO.DirectoryInfo directory)
|
||||
public static void Empty(this DirectoryInfo directory)
|
||||
{
|
||||
foreach(System.IO.FileInfo file in directory.EnumerateFiles()) file.Delete();
|
||||
foreach(System.IO.DirectoryInfo subDirectory in directory.EnumerateDirectories()) subDirectory.Delete(true);
|
||||
foreach(FileInfo file in directory.EnumerateFiles()) file.Delete();
|
||||
foreach(DirectoryInfo subDirectory in directory.EnumerateDirectories()) subDirectory.Delete(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flattens all files in subfolders to the passed directory recursively.
|
||||
///
|
||||
///
|
||||
/// foo<para />
|
||||
/// ├── 1.txt<para />
|
||||
/// ├── 2.txt<para />
|
||||
/// ├── 3.txt<para />
|
||||
/// ├── 4.txt<para />
|
||||
/// └── bar<para />
|
||||
/// ├── 1.txt<para />
|
||||
/// ├── 2.txt<para />
|
||||
/// └── 5.txt<para />
|
||||
///
|
||||
/// becomes:<para />
|
||||
/// foo<para />
|
||||
/// ├── 1.txt<para />
|
||||
/// ├── 2.txt<para />
|
||||
/// ├── 3.txt<para />
|
||||
/// ├── 4.txt<para />
|
||||
/// ├── bar_1.txt<para />
|
||||
/// ├── bar_2.txt<para />
|
||||
/// └── bar_5.txt<para />
|
||||
/// </summary>
|
||||
/// <param name="directory"></param>
|
||||
public static void Flatten(this DirectoryInfo directory)
|
||||
{
|
||||
FlattenDirectory(directory, directory);
|
||||
}
|
||||
|
||||
private static void FlattenDirectory(DirectoryInfo root, DirectoryInfo directory)
|
||||
{
|
||||
if (!root.FullName.Equals(directory.FullName)) // I might be able to replace this with root === directory
|
||||
{
|
||||
foreach (var file in directory.EnumerateFiles())
|
||||
{
|
||||
if (file.Directory == null) continue;
|
||||
var newName = $"{file.Directory.Name}_{file.Name}";
|
||||
var newPath = Path.Join(root.FullName, newName);
|
||||
Console.WriteLine($"Renaming/Moving file to: {newPath}");
|
||||
file.MoveTo(newPath);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var subDirectory in directory.EnumerateDirectories())
|
||||
{
|
||||
FlattenDirectory(root, subDirectory);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,6 @@ using API.DTOs;
|
|||
|
||||
namespace API.Interfaces
|
||||
{
|
||||
// TODO: Refactor this into IDiskService to encapsulate all disk based IO
|
||||
public interface IDirectoryService
|
||||
{
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ namespace API.Services
|
|||
foreach (var file in volume.Files)
|
||||
{
|
||||
var extractPath = GetVolumeCachePath(volumeId, file);
|
||||
|
||||
|
||||
_directoryService.ExtractArchive(file.FilePath, extractPath);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -137,6 +137,7 @@ namespace API.Services
|
|||
{
|
||||
FilePath = info.FullFilePath,
|
||||
Chapter = chapter,
|
||||
Format = info.Format,
|
||||
NumberOfPages = GetNumberOfPagesFromArchive(info.FullFilePath)
|
||||
};
|
||||
}
|
||||
|
|
@ -284,7 +285,13 @@ namespace API.Services
|
|||
return Path.Join(Directory.GetCurrentDirectory(), $"../cache/{volumeId}/");
|
||||
}
|
||||
|
||||
public string ExtractArchive(string archivePath, int volumeId)
|
||||
/// <summary>
|
||||
/// TODO: Delete this method
|
||||
/// </summary>
|
||||
/// <param name="archivePath"></param>
|
||||
/// <param name="volumeId"></param>
|
||||
/// <returns></returns>
|
||||
private string ExtractArchive(string archivePath, int volumeId)
|
||||
{
|
||||
if (!File.Exists(archivePath) || !Parser.Parser.IsArchive(archivePath))
|
||||
{
|
||||
|
|
@ -302,10 +309,18 @@ namespace API.Services
|
|||
|
||||
using ZipArchive archive = ZipFile.OpenRead(archivePath);
|
||||
|
||||
if (!archive.HasFiles()) return "";
|
||||
|
||||
// TODO: Throw error if we couldn't extract
|
||||
var needsFlattening = archive.Entries.Count > 0 && !Path.HasExtension(archive.Entries.ElementAt(0).FullName);
|
||||
if (!archive.HasFiles() && !needsFlattening) return "";
|
||||
|
||||
archive.ExtractToDirectory(extractPath);
|
||||
_logger.LogInformation($"Extracting archive to {extractPath}");
|
||||
|
||||
if (needsFlattening)
|
||||
{
|
||||
_logger.LogInformation("Extracted archive is nested in root folder, flattening...");
|
||||
new DirectoryInfo(extractPath).Flatten();
|
||||
}
|
||||
|
||||
return extractPath;
|
||||
}
|
||||
|
|
@ -325,12 +340,18 @@ namespace API.Services
|
|||
}
|
||||
|
||||
using ZipArchive archive = ZipFile.OpenRead(archivePath);
|
||||
|
||||
if (!archive.HasFiles()) return "";
|
||||
// TODO: Throw error if we couldn't extract
|
||||
var needsFlattening = archive.Entries.Count > 0 && !Path.HasExtension(archive.Entries.ElementAt(0).FullName);
|
||||
if (!archive.HasFiles() && !needsFlattening) return "";
|
||||
|
||||
archive.ExtractToDirectory(extractPath);
|
||||
_logger.LogDebug($"Extracting archive to {extractPath}");
|
||||
|
||||
if (!needsFlattening) return extractPath;
|
||||
|
||||
_logger.LogInformation("Extracted archive is nested in root folder, flattening...");
|
||||
new DirectoryInfo(extractPath).Flatten();
|
||||
|
||||
return extractPath;
|
||||
}
|
||||
|
||||
|
|
@ -343,7 +364,6 @@ namespace API.Services
|
|||
}
|
||||
|
||||
using ZipArchive archive = ZipFile.OpenRead(archivePath);
|
||||
Console.WriteLine();
|
||||
return archive.Entries.Count(e => Parser.Parser.IsImage(e.FullName));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue