Beispiel #1
0
        public bool SaveCustomerAvatarToHistory(int customerID, byte[] bytes)
        {
            //// Define the customer profile settings
            //var path = "customers/{0}/avatars".FormatWith(customerID);
            //var filename = "{0}.png".FormatWith(Path.GetRandomFileName());
            //var maxWidth = 300;
            //var maxHeight = 300;

            //// Resize the image
            //var resizedBytes = GlobalUtilities.ResizeImage(bytes, maxWidth, maxHeight);

            //// Determine if this avatar has already been uploaded before by looking at it's size
            //List<ImageFile> images;
            //using (var context = Exigo.Sql())
            //{
            //    string sqlProcedure = string.Format("GetImagesByPath '{0}'", path);
            //    images = context.Query<ImageFile>(sqlProcedure).ToList();
            //}
            //bool isUnique = images.Any(s => s.Size == resizedBytes.Length);
            //// Save the image
            //return (isUnique) ? SaveImage(path, filename, resizedBytes) : false;

            // Define the customer profile settings
            var       path      = $"/customers/{customerID}/avatars";
            var       filename  = $"{Path.GetRandomFileName()}.png";
            const int maxWidth  = 300;
            const int maxHeight = 300;

            // Resize the image
            var resizedBytes = GlobalUtilities.ResizeImage(bytes, maxWidth, maxHeight);
            int size         = resizedBytes.Length;

            bool result = false;

            using (var conn = Exigo.Sql())
            {
                conn.Open();
                //Check if this historical avatar exists by checking the customerID and filesize
                if (!GlobalUtilities.AvatarExists(customerID))
                {   //save the image
                    var cmd = new SqlCommand(@"
				INSERT INTO ImageFiles 
				(
				    [Path]
				    ,[Name]
				    ,[ModifiedDate]
				    ,[Size]
				    ,[ImageData]
				) 
				VALUES 
				(
				    @path
				    ,@filename
				    ,@modifiedDate
				    ,@size
				    ,@file
				)
				SELECT @@ROWCOUNT;
				"                , conn);

                    cmd.Parameters.Add("@path", System.Data.SqlDbType.NVarChar, 500).Value         = path;
                    cmd.Parameters.Add("@filename", System.Data.SqlDbType.NVarChar, 500).Value     = filename;
                    cmd.Parameters.Add("@modifiedDate", System.Data.SqlDbType.DateTime, 500).Value = DateTime.Now;
                    cmd.Parameters.Add("@size", System.Data.SqlDbType.NVarChar, 500).Value         = size;
                    cmd.Parameters.Add("@file", System.Data.SqlDbType.NVarChar, 500).Value         = resizedBytes;


                    try
                    {
                        bool savedToApi         = SaveImage(path, filename, resizedBytes);
                        bool savedToReportingDB = (bool)cmd.ExecuteScalar();

                        result = savedToApi && savedToReportingDB;
                    }
                    catch
                    {
                        return(false);
                    }
                }
            }
            return(result);
        }