Пример #1
0
        public AvatarResponse GetCustomerAvatarResponse(int customerID, string filename)
        {
            var response = new AvatarResponse();

            var path = $"/customers/{customerID.ToString()}/avatars";

            byte[] bytes;

            using (var conn = Exigo.Sql())
            {
                conn.Open();

                var cmd = new SqlCommand(@"
			    SELECT
				TOP 1 ImageData 
			    FROM 
				ImageFiles 
			    WHERE 
				Path=@FilePath 
				AND Name=@FileName
			    "            , conn);
                cmd.Parameters.Add("@FilePath", System.Data.SqlDbType.NVarChar, 500).Value = path;
                cmd.Parameters.Add("@FileName", System.Data.SqlDbType.NVarChar, 500).Value = filename;
                bytes = (byte[])cmd.ExecuteScalar();
            }
            response.Bytes = bytes;

            var    extension   = Path.GetExtension(filename).ToLower();
            string contentType = "image/jpeg";

            switch (extension)
            {
            case ".gif":
                contentType = "image/gif";
                break;

            case ".jpeg":
                contentType = "image/png";
                break;

            case ".bmp":
                contentType = "image/bmp";
                break;

            case ".png":
                contentType = "image/png";
                break;

            case ".jpg":
            default:
                contentType = "image/jpeg";
                break;
            }
            response.FileType = contentType;
            return(response);
        }
Пример #2
0
        public AvatarResponse GetCustomerAvatarResponse(int customerID, AvatarType type, bool cache = true, byte[] bytes = null)
        {
            var response = new AvatarResponse();

            response.Bytes = bytes;

            var path     = "/customers/" + customerID.ToString();
            var filename = "avatar";

            switch (type)
            {
            //case AvatarType.Tiny: filename += "-xs"; break;
            case AvatarType.Small: filename += "-sm.png"; break;

            case AvatarType.Large: filename += "-lg.png"; break;
            }

            if (bytes == null)
            {
                using (var conn = new SqlConnection(GlobalSettings.Exigo.Api.Sql.ConnectionStrings.SqlReporting))
                {
                    conn.Open();

                    string query = "Select top 1 ImageData as Bytes, '' as FileType, '' as FileName, ModifiedDate From ImageFiles Where Path=@FilePath AND Name=@FileName";
                    var    res   = conn.Query <AvatarResponse>(query, new { FilePath = path, FileName = filename }).FirstOrDefault();
                    if (res != null)
                    {
                        response = res;
                    }
                }
            }

            // If we didn't find anything there, convert the default image (which is Base64) to a byte array.
            // We'll use that instead
            if (response.Bytes == null)
            {
                bytes             = Convert.FromBase64String(GlobalSettings.Avatars.DefaultAvatarAsBase64);
                response.FileName = filename; //We will respond with the generic avatar filename so we can let the browser cache it.
                return(GetCustomerAvatarResponse(customerID, type, cache, GlobalUtilities.ResizeImage(bytes, type)));
            }
            else
            {
                var    extension   = Path.GetExtension(filename).ToLower();
                string contentType = "image/jpeg";
                switch (extension)
                {
                case ".gif":
                    contentType = "image/gif";
                    break;

                case ".jpeg":
                    contentType = "image/png";
                    break;

                case ".bmp":
                    contentType = "image/bmp";
                    break;

                case ".png":
                    contentType = "image/png";
                    break;

                case ".jpg":
                default:
                    contentType = "image/jpeg";
                    break;
                }
                response.FileName = customerID.ToString() + "-" + Path.GetFileNameWithoutExtension(filename) + "-" + response.ModifiedDate.ToBinary() + extension; //If we have it, we will change the filename to the customerID and modifieddate so it will show up immediately.
                response.FileType = contentType;
            }

            return(response);
        }
Пример #3
0
        public AvatarResponse GetCustomerAvatarResponse(int customerID, AvatarType type, bool cache = true, byte[] bytes = null)
        {
            var response = new AvatarResponse();

            var path     = "/customers/" + customerID.ToString();
            var filename = "avatar";

            switch (type)
            {
            case AvatarType.Tiny: filename += "-xs"; break;

            case AvatarType.Small: filename += "-sm"; break;

            case AvatarType.Large: filename += "-lg"; break;
            }

            // All images set to png, so we have to have this work around for now
            filename = filename + ".png";
            if (bytes == null)
            {
                using (var conn = Exigo.Sql())
                {
                    conn.Open();

                    var cmd = new SqlCommand(@"
						SELECT 
							TOP 1 ImageData 
						FROM
							ImageFiles 
						WHERE
							Path=@FilePath 
							AND Name=@FileName
						"                        , conn);
                    cmd.Parameters.Add("@FilePath", System.Data.SqlDbType.NVarChar, 500).Value = path;
                    cmd.Parameters.Add("@FileName", System.Data.SqlDbType.NVarChar, 500).Value = filename;
                    bytes = (byte[])cmd.ExecuteScalar();
                }
            }

            response.Bytes = bytes;

            // If we didn't find anything there, convert the default image (which is Base64) to a byte array.
            // We'll use that instead
            if (bytes == null)
            {
                bytes = Convert.FromBase64String(GlobalSettings.Avatars.DefaultAvatarAsBase64);


                return(GetCustomerAvatarResponse(customerID, type, cache, GlobalUtilities.ResizeImage(bytes, type)));
            }
            else
            {
                var    extension   = Path.GetExtension(filename).ToLower();
                string contentType = "image/jpeg";
                switch (extension)
                {
                case ".gif":
                    contentType = "image/gif";
                    break;

                case ".jpeg":
                    contentType = "image/png";
                    break;

                case ".bmp":
                    contentType = "image/bmp";
                    break;

                case ".png":
                    contentType = "image/png";
                    break;

                case ".jpg":
                default:
                    contentType = "image/jpeg";
                    break;
                }

                response.FileType = contentType;
            }


            return(response);
        }