diff --git a/Program.cs b/Program.cs index f7d9ec1..c449cd1 100644 --- a/Program.cs +++ b/Program.cs @@ -1,14 +1,16 @@ using ManyConsole; +using Microsoft.PowerShell.Commands; using MySql.Data.MySqlClient; +using Mysqlx.Crud; using Newtonsoft.Json; +using Spectre.Console; using System.Data.Odbc; using System.Diagnostics; -using System.Text; -using System.Management.Automation; using System.IO; -using Microsoft.PowerShell.Commands; -using Mysqlx.Crud; -using Spectre.Console; +using System; +using System.Management; +using System.Management.Automation; +using System.Text; namespace PhotosImporter { public class Program @@ -33,6 +35,7 @@ namespace PhotosImporter public string PhotosPath { get; set; } public string PhotosTable { get; set; } public bool LiveUpdate { get; set; } + public bool Recursive { get; set; } private static bool HasNonASCIIChars(string str) { @@ -63,13 +66,13 @@ namespace PhotosImporter return (prq1 || prq2); } - private static string FixBadFilenameAndUpdateRecord(FileInfo file, string[] videoExtensions, string table) + private static string FixBadFilenameAndUpdateRecord(FileInfo file, string[] videoExtensions, string table, string path) { string newFileName = string.Join("_", Encoding.ASCII.GetString(ASCIIEncoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(file.Name))).Split(Path.GetInvalidFileNameChars())); string newPath = Path.Combine(file.Directory.FullName,newFileName); file.MoveTo(newPath); - return UpdateRecord(new FileInfo(newPath), videoExtensions, table); + return UpdateRecord(new FileInfo(newPath), videoExtensions, table, path); } @@ -79,18 +82,14 @@ namespace PhotosImporter return bannedExtensions.Contains(extension); } - private static string UpdateRecord(FileInfo file, string[] videoExtensions, string table) + private static string UpdateRecord(FileInfo file, string[] videoExtensions, string table, string path) { - string name = Path.GetFileNameWithoutExtension(file.FullName); + //string name = Path.GetFileNameWithoutExtension(file.FullName); string hash = GetFileHash(file); string extension = file.Extension; string tags = GetFileTags(file); - int isVideo = 0; - if (videoExtensions.Contains(extension)) - { - isVideo = 1; - } - double fileSizeMb = GetFileSizeMB(file); + bool isVideo = videoExtensions.Contains(extension); + double fileSizeMb = GetFileSizeMB(file); //using (OdbcConnection connection = new OdbcConnection("DSN=photos")) //{ @@ -111,7 +110,7 @@ namespace PhotosImporter // Console.WriteLine(command.CommandText); // command.ExecuteNonQuery(); //} - string query = $"INSERT INTO {table} (name, hash, ext, tags, video, size) VALUES (\"{name}\", \"{Escape(hash)}\", \"{extension.Substring(1)}\", \'{tags}\', {isVideo.ToString()}, {fileSizeMb.ToString()});"; + string query = $"INSERT INTO {table} (name, hash, ext, tags, video, size) VALUES (\"{file.ToString().Replace(path+"\\", "").Replace(extension,"").Replace("\\","/")}\", \"{Escape(hash)}\", \"{extension.Substring(1)}\", \'{tags}\', {Convert.ToInt16(isVideo).ToString()}, {fileSizeMb.ToString()});"; return query; } @@ -166,7 +165,7 @@ namespace PhotosImporter return string.Join(":", tmp[0]["TagsList"]); } else { - return null; + return "{}"; } //Console.WriteLine("Line 100: Tags (none)"); @@ -233,7 +232,8 @@ namespace PhotosImporter // Required options/flags, append '=' to obtain the required value. HasRequiredOption("p|path=", "Path", p => PhotosPath = p); HasRequiredOption("t|table=", "Table", t => PhotosTable = t); - HasOption("l|live=", "Live Update", l => LiveUpdate = false); + HasRequiredOption("l|live=", "Live Update", l => LiveUpdate = l); + HasRequiredOption("r|recursive=", "Recursive Search", r => Recursive = r); // Optional options/flags, append ':' to obtain an optional value, or null if not specified. //HasOption("s|strip:", "Strips ',' from the file before writing to output.", @@ -277,23 +277,30 @@ namespace PhotosImporter var tsk = new ProgressTaskSettings { MaxValue = Directory.GetFiles(PhotosPath).Length, AutoStart = true }; var task1 = ctx.AddTask("Photos Processed", tsk); - foreach (string filename in Directory.GetFiles(PhotosPath)) + System.IO.EnumerationOptions Eopt = new() + { + RecurseSubdirectories = true + }; + foreach (string filename in Directory.GetFiles(PhotosPath,"*",Eopt)) { FileInfo file = new(filename); + //"".Replace(PhotosPath+"\\","") + string entryName = file.ToString().Replace(PhotosPath, ""); if (!file.Name.StartsWith('.') && !IsBannedExtension(file.Name, bannedExtensions)) { //Console.WriteLine($"{file.Name}"); //Path.GetFileName(filename); if (IsBadFilename(file.Name)) { - badFileNotifications.Add($"Bad Filename: {file}"); + badFileNotifications.Add($"Bad Filename: {file}, added anyways."); //Console.WriteLine($"Bad Filename: {file.Name}"); - sqlFile.WriteLine(FixBadFilenameAndUpdateRecord(file, videoExtensions, PhotosTable)); + UpdateRecord(file, videoExtensions, PhotosTable, PhotosPath); + //sqlFile.WriteLine(FixBadFilenameAndUpdateRecord(file, videoExtensions, PhotosTable)); } else { - ; - sqlFile.WriteLine(UpdateRecord(file, videoExtensions, PhotosTable)); + + sqlFile.WriteLine(UpdateRecord(file, videoExtensions, PhotosTable, PhotosPath)); } //Console.WriteLine("Processed: " + file.Name); }