public void LogFileCreatesAPhysicalCopyOfTheSourceFile() { using (TemporaryFile sourceFile = new TemporaryFile()) { File.WriteAllText(sourceFile.FileName, "Source file content"); string copiedFilePath = null; bool exists = false; long sourceFileLength = sourceFile.GetSize(); using (FilesContainer filesContainer = new FilesContainer(new Logger())) { LoggedFile file = filesContainer.LogFile(sourceFile.FileName, null); copiedFilePath = file.FilePath; exists = File.Exists(copiedFilePath); Assert.IsTrue(exists); Assert.AreEqual(sourceFileLength, file.FileSize); } exists = File.Exists(copiedFilePath); Assert.IsFalse(exists); exists = File.Exists(sourceFile.FileName); Assert.IsTrue(exists); } }
public LoggedFile LogFile(string sourceFilePath, string fileName = null) { if (string.IsNullOrWhiteSpace(fileName)) { fileName = Path.GetFileName(sourceFilePath); } fileName = NormalizeFileName(fileName); FileInfo fi = new FileInfo(sourceFilePath); if (!fi.Exists) { _logger.Error(new LogFileException(sourceFilePath, new FileNotFoundException(null, sourceFilePath))); return(null); } if (fi.Length > Constants.MaximumAllowedFileSizeInBytes) { _logger.Error(new FileSizeTooLargeException(fi.Length, Constants.MaximumAllowedFileSizeInBytes)); return(null); } TemporaryFile temporaryFile = null; try { temporaryFile = new TemporaryFile(); File.Delete(temporaryFile.FileName); File.Copy(sourceFilePath, temporaryFile.FileName, true); long length = temporaryFile.GetSize(); LoggedFile loggedFile = new LoggedFile(fileName, temporaryFile.FileName, length); _temporaryFiles.Add(temporaryFile); _loggedFiles.Add(loggedFile); return(loggedFile); } catch (Exception ex) { if (temporaryFile != null) { temporaryFile.Dispose(); } _logger.Error(new LogFileException(sourceFilePath, ex)); return(null); } }