public void Delete(string filePath, string fileName)
        {
            string physicalPath     = _attachmentPathUtil.ConvertUrlPathToPhysicalPath(filePath);
            string physicalFilePath = Path.Combine(physicalPath, fileName);

            if (!_attachmentPathUtil.IsAttachmentPathValid(physicalPath))
            {
                throw new SecurityException(null, "Attachment path was invalid when deleting {0}", fileName);
            }

            try
            {
                if (System.IO.File.Exists(physicalFilePath))
                {
                    System.IO.File.Delete(physicalFilePath);
                }
            }
            catch (IOException e)
            {
                throw new FileException(e, "Unable to delete {0} from {1}", fileName, filePath);
            }
        }
Exemple #2
0
        public JsonResult DeleteFile(string filePath, string fileName)
        {
            string physicalPath     = _attachmentPathUtil.ConvertUrlPathToPhysicalPath(filePath);
            string physicalFilePath = Path.Combine(physicalPath, fileName);

            if (!_attachmentPathUtil.IsAttachmentPathValid(physicalPath))
            {
                throw new SecurityException(null, "Attachment path was invalid when deleting {0}", fileName);
            }

            try
            {
                if (System.IO.File.Exists(physicalFilePath))
                {
                    System.IO.File.Delete(physicalFilePath);
                }

                return(Json(new { status = "ok", message = "" }));
            }
            catch (IOException e)
            {
                return(Json(new { status = "error", message = e.Message }));
            }
        }
        public void IsAttachmentPathValid_Should_Be_True_For_Valid_SubDirectory()
        {
            // Arrange
            string physicalPath = Path.Combine(_settings.AttachmentsDirectoryPath, "images", "test");

            Directory.CreateDirectory(physicalPath);
            bool expectedResult = true;

            // Act
            bool actualResult = _attachmentPathUtil.IsAttachmentPathValid(physicalPath);

            // Assert
            Assert.That(actualResult, Is.EqualTo(expectedResult));
        }