private string get_real_address(Guid?applicationId, ref bool isPublic)
        {
            _Encrypted = false;

            string folderPath = get_folder_address(applicationId, ref isPublic);

            if (string.IsNullOrEmpty(folderPath))
            {
                return(string.Empty);
            }

            string address = get_address(applicationId, encrypted: is_encrypted(applicationId));

            if (string.IsNullOrEmpty(address))
            {
                return(string.Empty);
            }

            string extLess = CephMode ? string.Empty :
                             get_address(applicationId, encrypted: is_encrypted(applicationId), ignoreExtension: true);

            if (CephMode)
            {
                return(CephStorage.file_exists(address) ? address : string.Empty);
            }
            else
            {
                return(File.Exists(address) ? address :
                       (!string.IsNullOrEmpty(extLess) && File.Exists(extLess) ? extLess : string.Empty));
            }
        }
 public int files_count_in_folder(Guid?applicationId)
 {
     try
     {
         string folderAddress = get_folder_address(applicationId);
         return(CephMode ? CephStorage.files(folderAddress).Count : Directory.GetFiles(folderAddress).Length);
     }
     catch { return(0); }
 }
        public bool store(Guid?applicationId, byte[] fileContent)
        {
            try
            {
                //Check for Encryption
                List <FolderNames> targetFolders =
                    new[] { FolderNames.TemporaryFiles, FolderNames.Attachments, FolderNames.WikiContent }.ToList();

                bool needsEncryption = targetFolders.Any(t => FolderName == t) &&
                                       RaaiVanSettings.FileEncryption(applicationId) &&
                                       ((Size.HasValue ? Size.Value : 0) / (1024 * 1024)) > 10;
                //end of Check for Encryption

                if (needsEncryption)
                {
                    fileContent = DocumentUtilities.encrypt_bytes_aes(fileContent);
                }

                bool   isPublic = false;
                string address  = get_address(applicationId, ref isPublic, encrypted: needsEncryption);

                if (CephMode)
                {
                    if (!CephStorage.add_file(address, fileContent, isPublic))
                    {
                        return(false);
                    }
                }
                else
                {
                    string folderPath = get_folder_address(applicationId);
                    if (string.IsNullOrEmpty(folderPath))
                    {
                        return(false);
                    }

                    if (!Directory.Exists(folderPath))
                    {
                        Directory.CreateDirectory(folderPath);
                    }

                    using (FileStream fs = new FileStream(address, FileMode.Create))
                        using (BinaryWriter bw = new BinaryWriter(fs))
                            bw.Write(fileContent);
                }

                _Encrypted = needsEncryption;

                return(true);
            }
            catch { return(false); }
        }
 public string url(Guid?applicationId)
 {
     if (CephMode)
     {
         bool   isPublic    = false;
         string realAddress = get_real_address(applicationId, ref isPublic);
         return(CephStorage.get_download_url(realAddress, isPublic));
     }
     else
     {
         return("../../download/" + FileID.ToString() +
                (FolderName.HasValue ? "?Category=" + FolderName.ToString() : string.Empty));
     }
 }
        public void delete(Guid?applicationId)
        {
            try
            {
                string address = get_real_address(applicationId);

                if (CephMode)
                {
                    CephStorage.delete_file(address);
                }
                else if (File.Exists(address))
                {
                    File.Delete(address);
                }
            }
            catch { }
        }
        public bool move(Guid?applicationId, FolderNames source, FolderNames destination, Guid?newGuidName = null)
        {
            try
            {
                if (!FileID.HasValue)
                {
                    return(false);
                }

                FolderName = source;

                string sourceAddress = get_real_address(applicationId);

                if (newGuidName.HasValue && newGuidName.Value != Guid.Empty)
                {
                    FileID = newGuidName;
                }
                FolderName = destination;

                if (string.IsNullOrEmpty(sourceAddress))
                {
                    return(!string.IsNullOrEmpty(get_real_address(applicationId)));
                }

                if (CephMode)
                {
                    bool   isPublic   = false;
                    string newAddress = get_address(applicationId, ref isPublic, encrypted: is_encrypted(applicationId));
                    return(CephStorage.rename_file(sourceAddress, newAddress, isPublic));
                }
                else
                {
                    string destFolder         = get_folder_address(applicationId);
                    string destinationAddress = get_address(applicationId, encrypted: is_encrypted(applicationId));

                    if (!Directory.Exists(destFolder))
                    {
                        Directory.CreateDirectory(destFolder);
                    }
                    File.Move(sourceAddress, destinationAddress);

                    return(true);
                }
            }
            catch { return(false); }
        }
        public byte[] toByteArray(Guid?applicationId)
        {
            try
            {
                string fileAddress = get_real_address(applicationId);

                if (string.IsNullOrEmpty(fileAddress))
                {
                    return(new byte[0]);
                }
                else if (CephMode)
                {
                    return(CephStorage.get_file(fileAddress));
                }
                else
                {
                    return(is_encrypted(applicationId) ?
                           DocumentUtilities.decrypt_bytes_aes(File.ReadAllBytes(fileAddress)) : File.ReadAllBytes(fileAddress));
                }
            }
            catch { return(new byte[0]); }
        }
 public bool file_exists_in_folder(Guid?applicationId)
 {
     return(CephMode ? CephStorage.folder_exists(get_folder_address(applicationId)) : files_count_in_folder(applicationId) > 0);
 }