Exemple #1
0
 /// <summary>
 /// Return an array of bytes in order to download the file
 /// </summary>
 /// <param name="file"></param>
 /// <returns></returns>
 public static byte[] GetFileToDownLoad(string path)
 {
     try
     {
         if (!String.IsNullOrWhiteSpace(path))
         {
             var localFilePath = GetStorageRoot(path);
             if (FileHelper.FileExists(localFilePath))
             {
                 var FileBytes = File.ReadAllBytes(localFilePath);
                 // If the file is in the upload folder, it needs to be decrypted
                 if (path.ToLower().Contains("/encrypted/") || path.ToLower().Contains("\\encrypted\\"))
                 {
                     var decyptedFileBytes = RijndaelHelper.DecryptBytes(FileBytes, ConfigurationManager.AppSettings["FileEncryptPassPhrase"], EncryptionSalt);
                     return(decyptedFileBytes);
                 }
                 else
                 {
                     return(FileBytes);
                 }
             }
         }
     }
     catch (Exception e)
     {
         Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "path = " + path);
     }
     return(null);
 }
Exemple #2
0
        /// <summary>
        /// Encrypt the file
        /// </summary>
        /// <param name="path"></param>
        /// <param name="fileBytes"></param>
        /// <returns></returns>
        private static bool EncryptWriteBytes(string path, byte[] fileBytes)
        {
            bool result = false;

            try
            {
                var encryptedBytes = RijndaelHelper.EncryptBytes(fileBytes, ConfigurationManager.AppSettings["FileEncryptPassPhrase"], EncryptionSalt);
                File.WriteAllBytes(path, encryptedBytes);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
                Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "path = " + path);
            }
            return(result);
        }
Exemple #3
0
        /// <summary>
        /// get the path of the decrypted file
        /// </summary>
        /// <param name="path"></param>
        /// <param name="IsUserPicture"></param>
        /// <returns></returns>
        public static string GetDecryptedFilePath(string path, bool IsUserPicture = false, bool IsThumbnail = false)
        {
            string fileSrc = CommonsConst.DefaultImage.Default;

            try
            {
                if (!String.IsNullOrWhiteSpace(path))
                {
                    var localFilePath = GetStorageRoot(path);
                    if (FileHelper.FileExists(localFilePath))
                    {
                        // If the file is in the upload folder, it needs to be decrypted
                        if (path.Contains(CommonsConst.Const.BasePathUploadEncrypted))
                        {
                            var encyptedFileBytes = File.ReadAllBytes(localFilePath);
                            var decyptedFileBytes = RijndaelHelper.DecryptBytes(encyptedFileBytes, ConfigurationManager.AppSettings["FileEncryptPassPhrase"], EncryptionSalt);
                            fileSrc = String.Format("data:image/gif;base64,{0}", Convert.ToBase64String(decyptedFileBytes));
                        }
                        else
                        {
                            fileSrc = path.Replace("~", "");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                fileSrc = CommonsConst.DefaultImage.Default.Replace("~", "");
                Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "path = " + path + " and IsUserPicture = " + IsUserPicture);
            }

            if (IsUserPicture && fileSrc == CommonsConst.DefaultImage.Default.Replace("~", ""))
            {
                if (IsThumbnail)
                {
                    fileSrc = CommonsConst.DefaultImage.DefaultThumbnailUser.Replace("~", "");
                }
                else
                {
                    fileSrc = CommonsConst.DefaultImage.DefaultImageUser.Replace("~", "");
                }
            }

            return(fileSrc);
        }