mirror of
				https://github.com/Kareadita/Kavita.git
				synced 2025-11-03 19:17:05 -05:00 
			
		
		
		
	* When flattening directories, ensure the order or directories being enumerated follows a natural sort. Some users are discovering directories in a different order than other machines. * Added a case for volume parsing and fixed a poorly designed negative lookahead. Added a sentence case pipe for formatting things. Added time for all dates. * Some more sentence case * Register user now has a white input * Fixed an issue with Manga up/down reading mode where top of the page was going forwards, when it should have gone backwards * Reworked some code to ensure that scanseries doesn't show errors where in fact there was just nothing to update. * Last updated should be working as intended for new library flow. * Code smell
		
			
				
	
	
		
			87 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			87 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System.IO;
 | 
						|
using System.Linq;
 | 
						|
using API.Comparators;
 | 
						|
 | 
						|
namespace API.Extensions
 | 
						|
{
 | 
						|
    public static class DirectoryInfoExtensions
 | 
						|
    {
 | 
						|
        private static readonly NaturalSortComparer Comparer = new NaturalSortComparer();
 | 
						|
        public static void Empty(this DirectoryInfo directory)
 | 
						|
        {
 | 
						|
          // NOTE: We have this in DirectoryService.Empty(), do we need this here?
 | 
						|
          foreach(FileInfo file in directory.EnumerateFiles()) file.Delete();
 | 
						|
          foreach(DirectoryInfo subDirectory in directory.EnumerateDirectories()) subDirectory.Delete(true);
 | 
						|
        }
 | 
						|
 | 
						|
        public static void RemoveNonImages(this DirectoryInfo directory)
 | 
						|
        {
 | 
						|
          foreach (var file in directory.EnumerateFiles())
 | 
						|
          {
 | 
						|
            if (!Parser.Parser.IsImage(file.FullName))
 | 
						|
            {
 | 
						|
              file.Delete();
 | 
						|
            }
 | 
						|
          }
 | 
						|
        }
 | 
						|
 | 
						|
        /// <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)
 | 
						|
        {
 | 
						|
            var index = 0;
 | 
						|
            FlattenDirectory(directory, directory, ref index);
 | 
						|
        }
 | 
						|
 | 
						|
        private static void FlattenDirectory(DirectoryInfo root, DirectoryInfo directory, ref int directoryIndex)
 | 
						|
        {
 | 
						|
            if (!root.FullName.Equals(directory.FullName))
 | 
						|
            {
 | 
						|
                var fileIndex = 1;
 | 
						|
 | 
						|
                foreach (var file in directory.EnumerateFiles().OrderBy(file => file.FullName, Comparer))
 | 
						|
                {
 | 
						|
                    if (file.Directory == null) continue;
 | 
						|
                    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}_{Parser.Parser.PadZeros(fileIndex + "")}{file.Extension}";
 | 
						|
                    var newPath = Path.Join(root.FullName, newName);
 | 
						|
                    if (!File.Exists(newPath)) file.MoveTo(newPath);
 | 
						|
                    fileIndex++;
 | 
						|
                }
 | 
						|
 | 
						|
                directoryIndex++;
 | 
						|
            }
 | 
						|
 | 
						|
            var sort = new NaturalSortComparer();
 | 
						|
            foreach (var subDirectory in directory.EnumerateDirectories().OrderBy(d => d.FullName, sort))
 | 
						|
            {
 | 
						|
                FlattenDirectory(root, subDirectory, ref directoryIndex);
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |