spring cleaning
This commit is contained in:
@@ -19,8 +19,6 @@
|
|||||||
<PackageReference Include="Microsoft.WSMan.Runtime" Version="7.4.7" />
|
<PackageReference Include="Microsoft.WSMan.Runtime" Version="7.4.7" />
|
||||||
<PackageReference Include="MySql.Data" Version="9.0.0" />
|
<PackageReference Include="MySql.Data" Version="9.0.0" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
<PackageReference Include="NExifTool" Version="0.12.0" />
|
|
||||||
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
|
|
||||||
<PackageReference Include="System.Data.Odbc" Version="8.0.0" />
|
<PackageReference Include="System.Data.Odbc" Version="8.0.0" />
|
||||||
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.1" />
|
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.1" />
|
||||||
<PackageReference Include="System.Management" Version="10.0.0-preview.1.25080.5" />
|
<PackageReference Include="System.Management" Version="10.0.0-preview.1.25080.5" />
|
||||||
|
|||||||
107
Program.cs
107
Program.cs
@@ -2,15 +2,9 @@
|
|||||||
using MySql.Data.MySqlClient;
|
using MySql.Data.MySqlClient;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System.Data.Odbc;
|
using System.Data.Odbc;
|
||||||
using NExifTool;
|
|
||||||
using Nito.AsyncEx.Synchronous;
|
|
||||||
using Nito.AsyncEx;
|
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
using Newtonsoft.Json.Linq;
|
|
||||||
using System.Management.Automation;
|
using System.Management.Automation;
|
||||||
using Microsoft.PowerShell;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Microsoft.PowerShell.Commands;
|
using Microsoft.PowerShell.Commands;
|
||||||
namespace PhotosImporter
|
namespace PhotosImporter
|
||||||
@@ -66,16 +60,13 @@ namespace PhotosImporter
|
|||||||
return (prq1 || prq2);
|
return (prq1 || prq2);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void FixBadFilenameAndUpdateRecord(FileInfo file, string[] videoExtensions, string table)
|
private static string FixBadFilenameAndUpdateRecord(FileInfo file, string[] videoExtensions, string table)
|
||||||
{
|
{
|
||||||
char[] invalidChars = Path.GetInvalidFileNameChars();
|
string newFileName = string.Join("_", Encoding.ASCII.GetString(ASCIIEncoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(file.Name))).Split(Path.GetInvalidFileNameChars()));
|
||||||
//invalidChars.Append('\'');
|
|
||||||
string newFileName = string.Join("_", Encoding.ASCII.GetString(ASCIIEncoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(file.Name))).Split(invalidChars));
|
|
||||||
string newPath = Path.Combine(file.Directory.FullName,newFileName);
|
string newPath = Path.Combine(file.Directory.FullName,newFileName);
|
||||||
//string newFilename = filename.Replace("'", "");//.Replace('"', '').Replace(",", "").Replace("`", "").Replace("’", "").Replace("—", "-");
|
|
||||||
//File.Move(table + filename, table + newFilename);
|
|
||||||
file.MoveTo(newPath);
|
file.MoveTo(newPath);
|
||||||
UpdateRecord(new FileInfo(newPath), videoExtensions, table);
|
|
||||||
|
return UpdateRecord(new FileInfo(newPath), videoExtensions, table);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +76,7 @@ namespace PhotosImporter
|
|||||||
return bannedExtensions.Contains(extension);
|
return bannedExtensions.Contains(extension);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void UpdateRecord(FileInfo file, string[] videoExtensions, string table)
|
private static string UpdateRecord(FileInfo file, string[] videoExtensions, string table)
|
||||||
{
|
{
|
||||||
string name = Path.GetFileNameWithoutExtension(file.FullName);
|
string name = Path.GetFileNameWithoutExtension(file.FullName);
|
||||||
string hash = GetFileHash(file);
|
string hash = GetFileHash(file);
|
||||||
@@ -98,30 +89,33 @@ namespace PhotosImporter
|
|||||||
}
|
}
|
||||||
double fileSizeMb = GetFileSizeMB(file);
|
double fileSizeMb = GetFileSizeMB(file);
|
||||||
|
|
||||||
using (OdbcConnection connection = new OdbcConnection("DSN=photos"))
|
//using (OdbcConnection connection = new OdbcConnection("DSN=photos"))
|
||||||
{
|
//{
|
||||||
connection.Open();
|
// connection.Open();
|
||||||
//string query = $"INSERT INTO {table} (name, hash, ext, tags, video, size) VALUES (@name, @hash, @ext, @tags, @video, @size)";
|
// //string query = $"INSERT INTO {table} (name, hash, ext, tags, video, size) VALUES (@name, @hash, @ext, @tags, @video, @size)";
|
||||||
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 (\"{name}\", \"{Escape(hash)}\", \"{extension.Substring(1)}\", \'{tags}\', {isVideo.ToString()}, {fileSizeMb.ToString()})";
|
||||||
//string query = $"INSERT INTO {table} (name, hash, ext, tags, video, size) VALUES (?, ?, ?, ?, ?, ?)";
|
// //string query = $"INSERT INTO {table} (name, hash, ext, tags, video, size) VALUES (?, ?, ?, ?, ?, ?)";
|
||||||
//Console.WriteLine(query);
|
// //Console.WriteLine(query);
|
||||||
OdbcCommand command = new(query, connection);
|
// OdbcCommand command = new(query, connection);
|
||||||
|
|
||||||
//command.Parameters.AddWithValue("@name", name);
|
// //command.Parameters.AddWithValue("@name", name);
|
||||||
//command.Parameters.AddWithValue("@hash", hash);
|
// //command.Parameters.AddWithValue("@hash", hash);
|
||||||
//command.Parameters.AddWithValue("@ext", extension);
|
// //command.Parameters.AddWithValue("@ext", extension);
|
||||||
//command.Parameters.AddWithValue("@tags", tags);
|
// //command.Parameters.AddWithValue("@tags", tags);
|
||||||
//command.Parameters.AddWithValue("@video", isVideo);
|
// //command.Parameters.AddWithValue("@video", isVideo);
|
||||||
//command.Parameters.AddWithValue("@size", fileSizeMb);
|
// //command.Parameters.AddWithValue("@size", fileSizeMb);
|
||||||
|
|
||||||
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()});";
|
||||||
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetFileHash(FileInfo file)
|
private static string GetFileHash(FileInfo file)
|
||||||
{
|
{
|
||||||
string res = "";
|
string res = "";
|
||||||
|
|
||||||
using (var md5 = System.Security.Cryptography.MD5.Create())
|
using (var md5 = System.Security.Cryptography.MD5.Create())
|
||||||
{
|
{
|
||||||
if (file.Length < (500 * 1024 * 1024))
|
if (file.Length < (500 * 1024 * 1024))
|
||||||
@@ -138,11 +132,7 @@ namespace PhotosImporter
|
|||||||
dynamic results = ps.Invoke();
|
dynamic results = ps.Invoke();
|
||||||
foreach (var item in results)
|
foreach (var item in results)
|
||||||
{
|
{
|
||||||
//Type t = item.GetType();
|
res = item.Hash.ToString().ToLowerInvariant();
|
||||||
//if (t is FileHashInfo)
|
|
||||||
//{
|
|
||||||
res = item.Hash.ToString().ToLowerInvariant();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
@@ -188,7 +178,6 @@ namespace PhotosImporter
|
|||||||
|
|
||||||
private static double GetFileSizeMB(FileInfo file)
|
private static double GetFileSizeMB(FileInfo file)
|
||||||
{
|
{
|
||||||
|
|
||||||
return (double)file.Length / (1024 * 1024);
|
return (double)file.Length / (1024 * 1024);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -249,11 +238,13 @@ namespace PhotosImporter
|
|||||||
|
|
||||||
public override int Run(string[] remainingArguments)
|
public override int Run(string[] remainingArguments)
|
||||||
{
|
{
|
||||||
string sqlFilePath = Path.Combine(System.Reflection.Assembly.GetExecutingAssembly().Location, "output.sql");
|
string sqlFilePath = Path.Combine(System.AppContext.BaseDirectory, "output.sql");
|
||||||
System.IO.FileStream sqlFile = File.OpenWrite(sqlFilePath); // base class not allowed???
|
StreamWriter sqlFile = File.CreateText(sqlFilePath); // base class not allowed??? // null;
|
||||||
|
sqlFile.AutoFlush = true;
|
||||||
|
sqlFile.WriteLine($"DELETE FROM {PhotosTable}");
|
||||||
|
sqlFile.WriteLine($"ALTER TABLE {PhotosTable} AUTO_INCREMENT = 1;");
|
||||||
|
|
||||||
|
string bannedExtensionsPath = "C:/Deps/bannedExtensions.json";
|
||||||
string bannedExtensionsPath = "C:/Deps/bannedExtensions.json";
|
|
||||||
string videoExtensionsPath = "C:/Deps/videoExtensions.json";
|
string videoExtensionsPath = "C:/Deps/videoExtensions.json";
|
||||||
|
|
||||||
string[] bannedExtensions = GetBannedExtensions(bannedExtensionsPath);
|
string[] bannedExtensions = GetBannedExtensions(bannedExtensionsPath);
|
||||||
@@ -265,30 +256,26 @@ namespace PhotosImporter
|
|||||||
|
|
||||||
DeleteRecords(PhotosTable);
|
DeleteRecords(PhotosTable);
|
||||||
ReIndexRecords(PhotosTable);
|
ReIndexRecords(PhotosTable);
|
||||||
foreach (string filename in Directory.GetFiles(PhotosPath))
|
foreach (string filename in Directory.GetFiles(PhotosPath))
|
||||||
{
|
{
|
||||||
FileInfo file = new(filename);
|
FileInfo file = new(filename);
|
||||||
if (!file.Name.StartsWith('.'))
|
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) && !IsBannedExtension(file.Name, bannedExtensions))
|
{
|
||||||
{
|
badFileNotifications.Add($"Bad Filename: {file}");
|
||||||
badFileNotifications.Add($"Bad Filename: {file}");
|
Console.WriteLine($"Bad Filename: {file.Name}");
|
||||||
Console.WriteLine($"Bad Filename: {file.Name}");
|
sqlFile.WriteLine(FixBadFilenameAndUpdateRecord(file, videoExtensions, PhotosTable));
|
||||||
FixBadFilenameAndUpdateRecord(file, videoExtensions, PhotosTable);
|
} else {;
|
||||||
|
sqlFile.WriteLine(UpdateRecord(file, videoExtensions, PhotosTable));
|
||||||
|
}
|
||||||
|
Console.WriteLine("Processed: " + file.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!IsBannedExtension(file.Name, bannedExtensions))
|
|
||||||
{
|
|
||||||
UpdateRecord(file, videoExtensions, PhotosTable);
|
|
||||||
}
|
|
||||||
Console.WriteLine("Processed: " + file.Name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
WriteBadFileNotifications(badFileNotifications);
|
|
||||||
}
|
|
||||||
foreach (var item in badFileNotifications)
|
foreach (var item in badFileNotifications)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"{item}");
|
Console.WriteLine($"{item}");
|
||||||
|
|||||||
Reference in New Issue
Block a user