/// <summary> /// Set up of db file and checking workspace files. /// </summary> public async Task SetUp() { string dbFile; do { // ask for database filename and see if it's valid Console.Write("Enter the name of the exported database file " + "(it must be placed on the same place as this .exe file, " + "leave as empty if the exported db filename is Backup_pages.db): "); dbFile = Console.ReadLine(); DbFile = string.IsNullOrWhiteSpace(dbFile) ? DbFile : dbFile; // check if database schema contains the images table and expected columns // see DbLayout.cs for how it layouts a database schema // see Database.cs line 39 for expected column names and data-types Console.Write($"Checking if {DbFile} is a valid database... "); if (!await new Database.Database(DbFile).IsDatabaseValid()) { continue; } } while (!File.Exists(DbFile) || !DbFile.EndsWith(".db")); Console.WriteLine("Ok."); var database = new Database.Database(DbFile); // setup workspace files. Adds an 'images' folder, and a 'log.txt' text file // Grabs all images from the db for preparation with other methods Console.Write("Checking workspace files... "); Directory.CreateDirectory("images"); using (var sw = File.AppendText("log.txt")) { await sw.WriteLineAsync($"Log started at: {DateTime.Now.ToString()}" + sw.NewLine + "----------------------------------------------------------------------" + sw.NewLine); } DbImages = await database.GetDbImagesFromDb(); Console.WriteLine($"Workspace files has been created. You have {DbImages.Count} images.\n"); }
/// <summary> /// Relinks image file paths in the database /// </summary> /// <returns></returns> public async Task RelinkImagePaths() { // ask for relinking image paths string answer; do { Console.Write("Relink the image paths in the database to be under the same folder location (Yes/No)? "); answer = Console.ReadLine(); } while (!(answer.Equals("Yes", StringComparison.OrdinalIgnoreCase) || answer.Equals("No", StringComparison.OrdinalIgnoreCase))); if (answer.Equals("No", StringComparison.OrdinalIgnoreCase)) { return; } // Prepare DbImages field var database = new Database.Database(DbFile); if (DbImages.Count == 0 || DbImages is null) { DbImages = await database.GetDbImagesFromDb(); } Console.ForegroundColor = ConsoleColor.Red; Console.Write("WARNING: "); Console.ForegroundColor = ConsoleColor.Gray; Console.Write("This procedure would change every image's filepath where the android app reads the image from.\n\n" + "For example, if the image's previous filepath was located at "); Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("/storage/emulated/0/Android/data/com.erakk.lnreader/files/images/..\n"); Console.ForegroundColor = ConsoleColor.Gray; Console.Write("And you specify it to be at "); Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("/storage/sdcard/LNReader/images/..\n"); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("Then you must put the downloaded content from the images folder at that new specified location. \n" + "Because this procedure would tell the app to locate the images at the specified new filepath onwards. \n" + "It will relocate all the image's filepath into that new location via the db, to be precise.\n"); Console.WriteLine("This app will take care of appending the image's relative path to your new specified path. \n\nE.g."); Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("/storage/emulated/0/Android/data/com.erakk.lnreader/files/images"); Console.ForegroundColor = ConsoleColor.Gray; Console.Write(" + "); Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("/project/images/b/bc/This_title_is_too_long%21_v1_Cover.png"); Console.ForegroundColor = ConsoleColor.Gray; Console.Write(" = "); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("/storage/emulated/0/Android/data/com.erakk.lnreader/files/images/project/images/b/bc/This_title_is_too_long%21_v1_Cover.png\n"); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("When inputting the new location, you only need to type in the path to place the " + "/project/.. folder at in your phone. The path must start from the root of your phone"); Console.WriteLine("Examples of expected filepath: "); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("/storage/emulated/0/Android/data/com.erakk.lnreader/files/images"); Console.WriteLine("/storage/emulated/0/LNReaderFiles/images\n"); Console.ForegroundColor = ConsoleColor.Gray; // TODO: Better validation for filepath // Goal for file path validation: // It has to be a filepath that works for the android file-system // starts with a '/' to indicate root of android device // ends with a non-'/' char, because the image addresses that will be attached after // the save file path already has a '/' at their beginning string newLocation; do { Console.Write("What would be the new location for all the images? "); newLocation = Console.ReadLine(); } while (!(newLocation.StartsWith("/") && !newLocation.EndsWith("/"))); // Start diagnostics and update the db with the new location Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); int x = await database.UpdateAllImageFilePath(newLocation, DbImages); stopwatch.Stop(); Console.WriteLine($"Changed {x} file paths"); Console.WriteLine($"{stopwatch.ElapsedMilliseconds} ms has elapsed for re-linking image file paths."); }