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" + } +} diff --git a/Common.Library.DataLayer/DAO/Websites/DMCDynamics.LLC/TestimonialGuidsDAO.cs b/Common.Library.DataLayer/DAO/Websites/DMCDynamics.LLC/TestimonialGuidsDAO.cs index 0f4f567..86de327 100644 --- a/Common.Library.DataLayer/DAO/Websites/DMCDynamics.LLC/TestimonialGuidsDAO.cs +++ b/Common.Library.DataLayer/DAO/Websites/DMCDynamics.LLC/TestimonialGuidsDAO.cs @@ -30,7 +30,6 @@ namespace Common.Library.DataLayer.DAO.Websites.DMCDynamics.LLC DBCommand.Parameters.Add(new MySqlParameter("GUID", MySqlDbType.VarChar, 255, ParameterDirection.Input, false, 0, 0, string.Empty, DataRowVersion.Proposed, guid)); DBCommand.Parameters.Add(new MySqlParameter("IsValid", MySqlDbType.Bit, 1, ParameterDirection.InputOutput, false, 0, 0, string.Empty, DataRowVersion.Proposed, isValid)); - DBCommand.ExecuteNonQuery(); isValid = Convert.ToBoolean(DBCommand.Parameters["IsValid"].Value); diff --git a/Common.Library.DataLayer/DAO/Websites/DMCDynamics.LLC/TestimonialImagesDAO.cs b/Common.Library.DataLayer/DAO/Websites/DMCDynamics.LLC/TestimonialImagesDAO.cs index 6fba3a4..353464a 100644 --- a/Common.Library.DataLayer/DAO/Websites/DMCDynamics.LLC/TestimonialImagesDAO.cs +++ b/Common.Library.DataLayer/DAO/Websites/DMCDynamics.LLC/TestimonialImagesDAO.cs @@ -8,10 +8,9 @@ namespace Common.Library.DataLayer.DAO.Websites.DMCDynamics.LLC { public class TestimonialImagesDAO : MySQL { - public TestimonialImagesDAO(string server, string database, string username, string password) : base(server, database, username, password) - { + public TestimonialImagesDAO(string server, string database, string username, string password) : base(server, database, username, password) { } - } + public TestimonialImagesDAO(DBConnectionInformation dBConnectionInformation) : base(dBConnectionInformation) { } public TestimonialImagesDTO SelectOne(int testimonialID) { diff --git a/Common.Library.DataLayer/DAO/Websites/DMCDynamics.LLC/TestimonialsDAO.cs b/Common.Library.DataLayer/DAO/Websites/DMCDynamics.LLC/TestimonialsDAO.cs index 2dcbbb2..6a3e69c 100644 --- a/Common.Library.DataLayer/DAO/Websites/DMCDynamics.LLC/TestimonialsDAO.cs +++ b/Common.Library.DataLayer/DAO/Websites/DMCDynamics.LLC/TestimonialsDAO.cs @@ -9,10 +9,9 @@ namespace Common.Library.DataLayer.DAO.Websites.DMCDynamics.LLC { public class TestimonialsDAO : MySQL { - public TestimonialsDAO(string server, string database, string username, string password) : base(server, database, username, password) - { + public TestimonialsDAO(string server, string database, string username, string password) : base(server, database, username, password) { } - } + public TestimonialsDAO(DBConnectionInformation dBConnectionInformation) : base(dBConnectionInformation) { } public TestimonialsDTOCollection SelectAll() { @@ -65,7 +64,13 @@ namespace Common.Library.DataLayer.DAO.Websites.DMCDynamics.LLC DBCommand.CommandText = "Testimonials_Insert"; //> Add parameters - + DBCommand.Parameters.Add(GetParameter("companyName", MySqlDbType.VarChar, 255, ParameterDirection.Input, testimonial.CompanyName)); + DBCommand.Parameters.Add(GetParameter("companySpokesPerson", MySqlDbType.VarChar, 255, ParameterDirection.Input, testimonial.CompanySpokesPerson)); + DBCommand.Parameters.Add(GetParameter("companyTitle", MySqlDbType.VarChar, 255, ParameterDirection.Input, testimonial.CompanyTitle)); + DBCommand.Parameters.Add(GetParameter("testimonialText", MySqlDbType.Text, 65535, ParameterDirection.Input, testimonial.Testimonial)); + DBCommand.Parameters.Add(GetParameter("companyWebsite", MySqlDbType.VarChar, 255, ParameterDirection.Input, testimonial.CompanyWebsite)); + DBCommand.Parameters.Add(GetParameter("testimonialGUID", MySqlDbType.VarChar, 255, ParameterDirection.Input, testimonial.TestimonialGUID)); + DBCommand.Parameters.Add(GetParameter("testimonialID", MySqlDbType.Int32, 0, ParameterDirection.Output, testimonial.TestimonialID)); DBCommand.ExecuteNonQuery(); diff --git a/Common.Library/SQL/MySQL/MySQL.cs b/Common.Library/SQL/MySQL/MySQL.cs index 7b783d4..3bd0bff 100644 --- a/Common.Library/SQL/MySQL/MySQL.cs +++ b/Common.Library/SQL/MySQL/MySQL.cs @@ -89,20 +89,22 @@ namespace Common.Library.SQL.MySQL if (!DBReader.IsDBNull(columnID)) { - const int CHUNK_SIZE = 2 * 1024; - byte[] buffer = new byte[CHUNK_SIZE]; - long bytesRead; - long fieldOffset = 0; - - using (var stream = new MemoryStream()) - { - while ((bytesRead = DBReader.GetBytes(columnID, fieldOffset, buffer, 0, buffer.Length)) == buffer.Length) - { - stream.Write(buffer, 0, (int)bytesRead); - fieldOffset += bytesRead; - } - rv = stream.ToArray(); - } + // const int CHUNK_SIZE = 2 * 1024; + // byte[] buffer = new byte[CHUNK_SIZE]; + // long bytesRead; + // long fieldOffset = 0; + + // using (var stream = new MemoryStream()) + // { + // while ((bytesRead = DBReader.GetBytes(columnID, fieldOffset, buffer, 0, buffer.Length)) == buffer.Length) + // { + // stream.Write(buffer, 0, (int)bytesRead); + // fieldOffset += bytesRead; + // } + // rv = stream.ToArray(); + // } + + rv = (byte[])DBReader[columnID]; } return rv; @@ -214,5 +216,17 @@ namespace Common.Library.SQL.MySQL { return GetDateTime(GetOrdinal(columnName)); } + + + //new MySqlParameter("GUID", MySqlDbType.VarChar, 255, ParameterDirection.Input, false, 0, 0, string.Empty, DataRowVersion.Proposed, guid) + protected MySqlParameter GetParameter(string columnName, MySqlDbType dataType, ParameterDirection direction, object data) + { + return null; + } + + protected MySqlParameter GetParameter(string columnName, MySqlDbType dataType, int size, ParameterDirection direction, object data) + { + return new MySqlParameter(columnName, dataType, size, direction, false, 0, 0, string.Empty, DataRowVersion.Proposed, data); + } } }