mirror of
				https://github.com/Kareadita/Kavita.git
				synced 2025-11-03 19:17:05 -05:00 
			
		
		
		
	* Updated some signatures to avoid a ToArray() within a loop. * Use UpdateSeries directly when adding new series, rather than a modified version for new series only. * Refactored some messages for scanner loop to reduce duplicate code and write messages more clear. Hooked in a RefreshMetadataProgress event (no UI changes). * Fixed a bug on docker where backup service was using different logic than non-docker, which isn't needed after config change last release. * Allow user to make more than 1 backup per day * Implemented a select all checkbox for library access modal
		
			
				
	
	
		
			165 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			165 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.IO;
 | 
						|
using System.Linq;
 | 
						|
using API.Services;
 | 
						|
using Kavita.Common;
 | 
						|
 | 
						|
namespace API.Data
 | 
						|
{
 | 
						|
    public static class MigrateConfigFiles
 | 
						|
    {
 | 
						|
        private static readonly List<string> LooseLeafFiles = new List<string>()
 | 
						|
        {
 | 
						|
            "appsettings.json",
 | 
						|
            "appsettings.Development.json",
 | 
						|
            "kavita.db",
 | 
						|
        };
 | 
						|
 | 
						|
        private static readonly List<string> AppFolders = new List<string>()
 | 
						|
        {
 | 
						|
            "covers",
 | 
						|
            "stats",
 | 
						|
            "logs",
 | 
						|
            "backups",
 | 
						|
            "cache",
 | 
						|
            "temp"
 | 
						|
        };
 | 
						|
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// In v0.4.8 we moved all config files to config/ to match with how docker was setup. This will move all config files from current directory
 | 
						|
        /// to config/
 | 
						|
        /// </summary>
 | 
						|
        public static void Migrate(bool isDocker)
 | 
						|
        {
 | 
						|
            Console.WriteLine("Checking if migration to config/ is needed");
 | 
						|
 | 
						|
            if (isDocker)
 | 
						|
            {
 | 
						|
                if (Configuration.LogPath.Contains("config"))
 | 
						|
                {
 | 
						|
                    Console.WriteLine("Migration to config/ not needed");
 | 
						|
                    return;
 | 
						|
                }
 | 
						|
 | 
						|
                Console.WriteLine(
 | 
						|
                    "Migrating files from pre-v0.4.8. All Kavita config files are now located in config/");
 | 
						|
 | 
						|
                CopyAppFolders();
 | 
						|
                DeleteAppFolders();
 | 
						|
 | 
						|
                UpdateConfiguration();
 | 
						|
 | 
						|
                Console.WriteLine("Migration complete. All config files are now in config/ directory");
 | 
						|
                return;
 | 
						|
            }
 | 
						|
 | 
						|
            if (new FileInfo(Configuration.AppSettingsFilename).Exists)
 | 
						|
            {
 | 
						|
                Console.WriteLine("Migration to config/ not needed");
 | 
						|
                return;
 | 
						|
            }
 | 
						|
 | 
						|
            Console.WriteLine(
 | 
						|
                "Migrating files from pre-v0.4.8. All Kavita config files are now located in config/");
 | 
						|
 | 
						|
            Console.WriteLine($"Creating {DirectoryService.ConfigDirectory}");
 | 
						|
            DirectoryService.ExistOrCreate(DirectoryService.ConfigDirectory);
 | 
						|
 | 
						|
            try
 | 
						|
            {
 | 
						|
                CopyLooseLeafFiles();
 | 
						|
 | 
						|
                CopyAppFolders();
 | 
						|
 | 
						|
                // Then we need to update the config file to point to the new DB file
 | 
						|
                UpdateConfiguration();
 | 
						|
            }
 | 
						|
            catch (Exception)
 | 
						|
            {
 | 
						|
                Console.WriteLine("There was an exception during migration. Please move everything manually.");
 | 
						|
                return;
 | 
						|
            }
 | 
						|
 | 
						|
            // Finally delete everything in the source directory
 | 
						|
            Console.WriteLine("Removing old files");
 | 
						|
            DeleteLooseFiles();
 | 
						|
            DeleteAppFolders();
 | 
						|
            Console.WriteLine("Removing old files...DONE");
 | 
						|
 | 
						|
            Console.WriteLine("Migration complete. All config files are now in config/ directory");
 | 
						|
        }
 | 
						|
 | 
						|
        private static void DeleteAppFolders()
 | 
						|
        {
 | 
						|
            foreach (var folderToDelete in AppFolders)
 | 
						|
            {
 | 
						|
                if (!new DirectoryInfo(Path.Join(Directory.GetCurrentDirectory(), folderToDelete)).Exists) continue;
 | 
						|
 | 
						|
                DirectoryService.ClearAndDeleteDirectory(Path.Join(Directory.GetCurrentDirectory(), folderToDelete));
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        private static void DeleteLooseFiles()
 | 
						|
        {
 | 
						|
            var configFiles = LooseLeafFiles.Select(file => new FileInfo(Path.Join(Directory.GetCurrentDirectory(), file)))
 | 
						|
                .Where(f => f.Exists);
 | 
						|
            DirectoryService.DeleteFiles(configFiles.Select(f => f.FullName));
 | 
						|
        }
 | 
						|
 | 
						|
        private static void CopyAppFolders()
 | 
						|
        {
 | 
						|
            Console.WriteLine("Moving folders to config");
 | 
						|
 | 
						|
                foreach (var folderToMove in AppFolders)
 | 
						|
                {
 | 
						|
                    if (new DirectoryInfo(Path.Join(DirectoryService.ConfigDirectory, folderToMove)).Exists) continue;
 | 
						|
 | 
						|
                    try
 | 
						|
                    {
 | 
						|
                        DirectoryService.CopyDirectoryToDirectory(
 | 
						|
                            Path.Join(Directory.GetCurrentDirectory(), folderToMove),
 | 
						|
                            Path.Join(DirectoryService.ConfigDirectory, folderToMove));
 | 
						|
                    }
 | 
						|
                    catch (Exception)
 | 
						|
                    {
 | 
						|
                        /* Swallow Exception */
 | 
						|
                    }
 | 
						|
                }
 | 
						|
 | 
						|
 | 
						|
            Console.WriteLine("Moving folders to config...DONE");
 | 
						|
        }
 | 
						|
 | 
						|
        private static void CopyLooseLeafFiles()
 | 
						|
        {
 | 
						|
            var configFiles = LooseLeafFiles.Select(file => new FileInfo(Path.Join(Directory.GetCurrentDirectory(), file)))
 | 
						|
                .Where(f => f.Exists);
 | 
						|
            // First step is to move all the files
 | 
						|
            Console.WriteLine("Moving files to config/");
 | 
						|
            foreach (var fileInfo in configFiles)
 | 
						|
            {
 | 
						|
                try
 | 
						|
                {
 | 
						|
                    fileInfo.CopyTo(Path.Join(DirectoryService.ConfigDirectory, fileInfo.Name));
 | 
						|
                }
 | 
						|
                catch (Exception)
 | 
						|
                {
 | 
						|
                    /* Swallow exception when already exists */
 | 
						|
                }
 | 
						|
            }
 | 
						|
 | 
						|
            Console.WriteLine("Moving files to config...DONE");
 | 
						|
        }
 | 
						|
 | 
						|
        private static void UpdateConfiguration()
 | 
						|
        {
 | 
						|
            Console.WriteLine("Updating appsettings.json to new paths");
 | 
						|
            Configuration.DatabasePath = "config//kavita.db";
 | 
						|
            Configuration.LogPath = "config//logs/kavita.log";
 | 
						|
            Console.WriteLine("Updating appsettings.json to new paths...DONE");
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |