public FileViewModel GetFile(ListItem file)
        {
            var path = Path.Combine(ConfigurationHelper.GetTemporaryFilesFolder(), file.Id, file.Value);
            if (File.Exists(path))
            {
                FileStream fsSource = null;
                try
                {
                    fsSource = new FileStream(path, FileMode.Open, FileAccess.Read);
                    var viewModel = new FileViewModel
                    {
                        FileName = file.Value,
                        InputStream = fsSource
                    };
                    return viewModel;
                }
                catch (Exception ex)
                {
                    if (fsSource != null)
                    {
                        fsSource.Dispose();
                    }

                    throw;
                }
            }

            return null;
        }
 public bool FileExists(ListItem file)
 {
     if (IsAttachmentSaved(file))
     {
         return ExistsInSavedStorage(file);
     }
     return ExistsInStorage(file, ConfigurationHelper.GetTemporaryFilesFolder());
 }
 public byte[] GetFileBytes(ListItem file)
 {
     if (IsAttachmentSaved(file))
     {
         return ReadSavedAttachment(file);
     }
     return ReadFileFromTemplateStorageBytes(file);
 }
 public string GetFilePath(ListItem selectedFile)
 {
     string storage = IsAttachmentSaved(selectedFile)
         ? ConfigurationHelper.GetFilesStorageFolder()
         : ConfigurationHelper.GetTemporaryFilesFolder();
     var path = Path.Combine(storage, selectedFile.Id, selectedFile.Value);
     return path;
 }
        private Stream GetFileStreamFromNetworkStorage(ListItem file)
        {
            var networkStorage = ConfigurationHelper.GetFilesStorageFolder();

            return this.ExistsInStorage(file, networkStorage)
                       ? this.fileService.GetFileStream(Path.Combine(networkStorage, file.Id, file.Value))
                       : null;
        }
 private bool TryRemoveFile(ListItem attachment, string storagePath)
 {
     var path = Path.Combine(storagePath, attachment.Id);
     return fileService.DeleteDirectoryIfExists(path);
 }
 private byte[] ReadSavedAttachment(ListItem file)
 {
     return ReadFileFromNetworkStorageBytes(file) ?? ReadFileFromTemplateStorageBytes(file);
 }
        private byte[] ReadFileFromTemplateStorageBytes(ListItem file)
        {
            var templateFilesFolder = ConfigurationHelper.GetTemporaryFilesFolder();
            if (ExistsInStorage(file, templateFilesFolder))
            {
                return fileService.ReadAllBytes(Path.Combine(templateFilesFolder, file.Id, file.Value));
            }

            return null;
        }
        private byte[] ReadFileFromNetworkStorageBytes(ListItem file)
        {
            var networkStorage = ConfigurationHelper.GetFilesStorageFolder();

            if (ExistsInStorage(file, networkStorage))
            {
                return fileService.ReadAllBytes(Path.Combine(networkStorage, file.Id, file.Value));
            }

            return null;
        }
 private bool IsAttachmentSaved(ListItem file)
 {
     long attachmentId;
     return long.TryParse(file.AdditionalInfo, out attachmentId);
 }
 public HttpResponseMessage Deny(ListItem data)
 {
     var updatedRequest = creditRequestService.Deny(long.Parse(data.Id));
     return Request.CreateResponse(HttpStatusCode.OK, updatedRequest);
 }
 public HttpResponseMessage DeleteAttachment(ListItem file)
 {
     return Request.CreateResponse(HttpStatusCode.OK, ProcessViewModel(file, null, fileManagementService.RemoveTempAttachment));
 }
 private bool ExistsInStorage(ListItem file, string storagePath)
 {
     var path = GenerateFilepath(file.Value, file.Id, storagePath);
     return fileService.CheckIfFileExist(path);
 }
        private bool ExistsInSavedStorage(ListItem selectedFile)
        {
            if (ExistsInStorage(selectedFile, ConfigurationHelper.GetFilesStorageFolder()))
            {
                return true;
            }

            return ExistsInStorage(selectedFile, ConfigurationHelper.GetTemporaryFilesFolder());
        }
 public void RenameFile(ListItem file, string newName, string storage)
 {
     if (ExistsInStorage(file, storage))
     {
         var oldPath = GenerateFilepath(file.Value, file.Id, storage);
         var newPath = GenerateFilepath(newName, file.Id, storage);
         fileService.Rename(oldPath, newPath);
     }
 }
 public void RemoveTempAttachment(ListItem attachment)
 {
     if (!IsAttachmentSaved(attachment))
     {
         TryRemoveFile(attachment, ConfigurationHelper.GetTemporaryFilesFolder());
     }
 }
 public void RemoveAttachment(ListItem attachment)
 {
     if (attachment == null)
     {
         CommonLogger.Info("Attachment is null");
         return;
     }
     if (IsAttachmentSaved(attachment))
     {
         if (ExistsInSavedStorage(attachment))
         {
             var isFileRemoved = TryRemoveFile(attachment, ConfigurationHelper.GetFilesStorageFolder()) ||
                 TryRemoveFile(attachment, ConfigurationHelper.GetTemporaryFilesFolder());
             return;
         }
     }
     RemoveTempAttachment(attachment);
 }
 private Stream GetFileStreamFromTemplateStorage(ListItem file)
 {
     var templateFilesFolder = ConfigurationHelper.GetTemporaryFilesFolder();
     return this.ExistsInStorage(file, templateFilesFolder)
                ? this.fileService.GetFileStream(Path.Combine(templateFilesFolder, file.Id, file.Value))
                : null;
 }
 private Stream GetSavedAttachment(ListItem file)
 {
     return this.GetFileStreamFromNetworkStorage(file) ?? this.GetFileStreamFromTemplateStorage(file);
 }
 public Stream GetFileStream(ListItem file)
 {
     if (IsAttachmentSaved(file))
     {
         return this.GetSavedAttachment(file);
     }
     return this.GetFileStreamFromTemplateStorage(file);
 }
 public HttpResponseMessage ApproveByCreditComission(ListItem data)
 {
     var updatedRequest = creditRequestService.ApproveByCreditComission(long.Parse(data.Id));
     return Request.CreateResponse(HttpStatusCode.OK, updatedRequest);
 }