diff --git a/Common.Library.Console/Common.Library.Console.Image.Uploader.csproj b/Common.Library.Console/Common.Library.Console.Image.Uploader.csproj new file mode 100644 index 0000000..f7ca267 --- /dev/null +++ b/Common.Library.Console/Common.Library.Console.Image.Uploader.csproj @@ -0,0 +1,33 @@ + + + + Exe + net5.0 + true + + + + + + + + + PreserveNewest + true + PreserveNewest + + + + + + + + + + + + + + + + diff --git a/Common.Library.Console/ImageUploader.cs b/Common.Library.Console/ImageUploader.cs new file mode 100644 index 0000000..1cfd227 --- /dev/null +++ b/Common.Library.Console/ImageUploader.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Data.Common; +using System.Text; +using Common.Library.SQL.MySQL; +using Microsoft.Extensions.Configuration; +using Common.Library.ImageHandling; +using Common.Library.Settings; +using MySql.Data.MySqlClient; + +namespace Common.Library.Console.Image.Uploader +{ + public class ImageUploader : MySQL + { + + private readonly string _patoToImage; + + public ImageUploader(string pathToImage) : base(SettingsLoader.DBHost, SettingsLoader.DBName, SettingsLoader.DBUserName, SettingsLoader.DBPassword) + { + _patoToImage = pathToImage; + } + + public void UploadImage(int testimonialID) + { + + ImageHandler imageHandler = new ImageHandler(); + + KeyValuePair imageData = imageHandler.ConvertImageFromFile(_patoToImage); + + InitConnection(); + try + { + DBCommand.CommandType = CommandType.StoredProcedure; + DBCommand.CommandText = "Testimonial_Images_Insert"; + + DBCommand.Parameters.AddWithValue("@testimonialID", testimonialID); + DBCommand.Parameters.AddWithValue("@imageType", imageData.Key); + //DBCommand.Parameters.AddWithValue("@imageData", imageData.Value); + MySqlParameter image = new MySqlParameter("@imageData", MySqlDbType.MediumBlob, imageData.Value.Length); + image.Value = imageData.Value; + DBCommand.Parameters.Add(image); + + + DBCommand.ExecuteNonQuery(); + + } + catch (Exception) + { + throw; + } + finally + { + DisposeConnection(); + } + + } + } +} diff --git a/Common.Library.Console/Program.cs b/Common.Library.Console/Program.cs new file mode 100644 index 0000000..75d8d67 --- /dev/null +++ b/Common.Library.Console/Program.cs @@ -0,0 +1,34 @@ +using Microsoft.Extensions.Configuration; +using Common.Library.Settings; + +namespace Common.Library.Console.Image.Uploader +{ + class Program + { + + static void Main(string[] args) + { + SettingsLoader.Configuration = new ConfigurationBuilder() + .AddJsonFile("appsettings.json", true, true) + .Build(); + + System.Console.Write("Path to Image for Uploading: "); + string pathToFile = System.Console.ReadLine().Replace("\"", string.Empty).Trim(); + + System.Console.WriteLine(); + + System.Console.Write("TestimonialID to tie image to: "); + if(int.TryParse(System.Console.ReadLine().Trim(), out int testimonialID) == false) + { + throw new System.Exception("Cannot parse TestimonalID - it is not a number"); + } + + ImageUploader uploader = new ImageUploader(pathToFile); + + uploader.UploadImage(testimonialID); + + System.Console.WriteLine("Complete"); + System.Console.ReadKey(); + } + } +} diff --git a/Common.Library.Console/appsettings.json b/Common.Library.Console/appsettings.json new file mode 100644 index 0000000..3323d47 --- /dev/null +++ b/Common.Library.Console/appsettings.json @@ -0,0 +1,16 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + + "Database": { + "DBName": "dmcdynamics", + "Host": "10.0.3.6", + "Password": "Iu5I03ZDYjtYtqfD", + "UserName": "dmcdynamics" + } +}