some changes

This commit is contained in:
2026-05-18 09:50:10 -04:00
parent 5fb096b6e2
commit 89a957a85d

View File

@@ -1,14 +1,16 @@
using ManyConsole; using ManyConsole;
using Microsoft.PowerShell.Commands;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
using Mysqlx.Crud;
using Newtonsoft.Json; using Newtonsoft.Json;
using Spectre.Console;
using System.Data.Odbc; using System.Data.Odbc;
using System.Diagnostics; using System.Diagnostics;
using System.Text;
using System.Management.Automation;
using System.IO; using System.IO;
using Microsoft.PowerShell.Commands; using System;
using Mysqlx.Crud; using System.Management;
using Spectre.Console; using System.Management.Automation;
using System.Text;
namespace PhotosImporter namespace PhotosImporter
{ {
public class Program public class Program
@@ -33,6 +35,7 @@ namespace PhotosImporter
public string PhotosPath { get; set; } public string PhotosPath { get; set; }
public string PhotosTable { get; set; } public string PhotosTable { get; set; }
public bool LiveUpdate { get; set; } public bool LiveUpdate { get; set; }
public bool Recursive { get; set; }
private static bool HasNonASCIIChars(string str) private static bool HasNonASCIIChars(string str)
{ {
@@ -63,13 +66,13 @@ namespace PhotosImporter
return (prq1 || prq2); 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 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); string newPath = Path.Combine(file.Directory.FullName,newFileName);
file.MoveTo(newPath); 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); 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 hash = GetFileHash(file);
string extension = file.Extension; string extension = file.Extension;
string tags = GetFileTags(file); string tags = GetFileTags(file);
int isVideo = 0; bool isVideo = videoExtensions.Contains(extension);
if (videoExtensions.Contains(extension)) double fileSizeMb = GetFileSizeMB(file);
{
isVideo = 1;
}
double fileSizeMb = GetFileSizeMB(file);
//using (OdbcConnection connection = new OdbcConnection("DSN=photos")) //using (OdbcConnection connection = new OdbcConnection("DSN=photos"))
//{ //{
@@ -111,7 +110,7 @@ namespace PhotosImporter
// Console.WriteLine(command.CommandText); // Console.WriteLine(command.CommandText);
// command.ExecuteNonQuery(); // 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; return query;
} }
@@ -166,7 +165,7 @@ namespace PhotosImporter
return string.Join(":", tmp[0]["TagsList"]); return string.Join(":", tmp[0]["TagsList"]);
} else } else
{ {
return null; return "{}";
} }
//Console.WriteLine("Line 100: Tags (none)"); //Console.WriteLine("Line 100: Tags (none)");
@@ -233,7 +232,8 @@ namespace PhotosImporter
// Required options/flags, append '=' to obtain the required value. // Required options/flags, append '=' to obtain the required value.
HasRequiredOption("p|path=", "Path", p => PhotosPath = p); HasRequiredOption("p|path=", "Path", p => PhotosPath = p);
HasRequiredOption("t|table=", "Table", t => PhotosTable = t); HasRequiredOption("t|table=", "Table", t => PhotosTable = t);
HasOption("l|live=", "Live Update", l => LiveUpdate = false); HasRequiredOption<bool>("l|live=", "Live Update", l => LiveUpdate = l);
HasRequiredOption<bool>("r|recursive=", "Recursive Search", r => Recursive = r);
// Optional options/flags, append ':' to obtain an optional value, or null if not specified. // 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.", //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 tsk = new ProgressTaskSettings { MaxValue = Directory.GetFiles(PhotosPath).Length, AutoStart = true };
var task1 = ctx.AddTask("Photos Processed", tsk); 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); FileInfo file = new(filename);
//"".Replace(PhotosPath+"\\","")
string entryName = file.ToString().Replace(PhotosPath, "");
if (!file.Name.StartsWith('.') && !IsBannedExtension(file.Name, bannedExtensions)) if (!file.Name.StartsWith('.') && !IsBannedExtension(file.Name, bannedExtensions))
{ {
//Console.WriteLine($"{file.Name}"); //Console.WriteLine($"{file.Name}");
//Path.GetFileName(filename); //Path.GetFileName(filename);
if (IsBadFilename(file.Name)) if (IsBadFilename(file.Name))
{ {
badFileNotifications.Add($"Bad Filename: {file}"); badFileNotifications.Add($"Bad Filename: {file}, added anyways.");
//Console.WriteLine($"Bad Filename: {file.Name}"); //Console.WriteLine($"Bad Filename: {file.Name}");
sqlFile.WriteLine(FixBadFilenameAndUpdateRecord(file, videoExtensions, PhotosTable)); UpdateRecord(file, videoExtensions, PhotosTable, PhotosPath);
//sqlFile.WriteLine(FixBadFilenameAndUpdateRecord(file, videoExtensions, PhotosTable));
} }
else else
{ {
;
sqlFile.WriteLine(UpdateRecord(file, videoExtensions, PhotosTable)); sqlFile.WriteLine(UpdateRecord(file, videoExtensions, PhotosTable, PhotosPath));
} }
//Console.WriteLine("Processed: " + file.Name); //Console.WriteLine("Processed: " + file.Name);
} }