public async Task FindPhotosAsync_TwoLevelDirectory_SingleFile_JpegFormatAsync() { // Create deeper directory structure var tempDirectory = PathHelper.GetTemporaryDirectory(); var newDirectory = Path.Combine(tempDirectory, Path.GetRandomFileName()); Directory.CreateDirectory(newDirectory); // Find expected path from the file creation var tempFileName = PathHelper.CreateImageFile(newDirectory, ImageFormat.Jpeg); var expectedPath = Path.Combine(newDirectory, tempFileName); // Find actual path var photoList = new List <Photo>(); await foreach (var p in PhotoHandler.FindPhotosAsync(tempDirectory)) { photoList.Add(p); } var photo = photoList.FirstOrDefault(); var actualPath = photo.FilePath; // Assert the two paths are equal Assert.Equal(expectedPath, actualPath); }
public async Task FindPhotosAsync_FlatDirectory_MultipleFiles_JpegFormatAsync() { var tempDirectory = PathHelper.GetTemporaryDirectory(); // Creates the expected paths set from the file creation var expectedPaths = new HashSet <string>(); for (var i = 0; i < 10; i++) { var path = Path.Combine(tempDirectory, PathHelper.CreateImageFile(tempDirectory, ImageFormat.Jpeg)); expectedPaths.Add(path); } // Creates set containing the actual paths var photoList = new List <Photo>(); await foreach (var p in PhotoHandler.FindPhotosAsync(tempDirectory)) { photoList.Add(p); } var actualPaths = new HashSet <string>(); foreach (var photo in photoList) { actualPaths.Add(photo.FilePath); } // Assert the two sets are equal Assert.Subset(expectedPaths, actualPaths); Assert.Superset(expectedPaths, actualPaths); }
public async Task FindPhotosAsync_InvalidPathAsync() { var path = @"BlahBlahBuh"; var photoList = new List <Photo>(); await foreach (var photo in PhotoHandler.FindPhotosAsync(path)) { photoList.Add(photo); } Assert.Empty(photoList); }
/// <summary> /// Runs the organizer by recursively searching every directory from the given <paramref name="inputDirectory" />. /// </summary> /// <param name="inputDirectory">Path to initial directory to search for media files.</param> /// <param name="database">Indicates whether a database is used.</param> public async Task RunOrganizerAsync(string inputDirectory) { // if input directory does not exist, throw exception and end run inputDirectory.EnsureDirectoryExists(); // preliminary setup var hashAlgorithm = _configuration.GetValue <Algorithm>("hash-algorithm"); var checksum = new Checksum(hashAlgorithm); var photoCounter = 0; if (!(_context is null)) { await _context.Database.EnsureCreatedAsync(); } _logger.LogInformation($"Begin organizing in { inputDirectory }"); await foreach (var photo in PhotoHandler.FindPhotosAsync(inputDirectory)) { using var fs = File.OpenRead(photo.FilePath); // Compute checksum photo.Checksum = checksum.ComputeChecksum(fs); // Reset filestream position fs.Position = 0; // Fetch metadata directories using MetadataExctractor and parse metadata to the Photo object var metadataDirectories = ImageMetadataReader.ReadMetadata(fs); ParseMetadata.Parse(photo, metadataDirectories); // Rename and sort photos _sortService.SortPhoto(photo); // Add photo to database context if it does not exist already if (!(_context is null) && !await _context.Photos.AnyAsync(p => p.Name == photo.Name)) { await _context.Photos.AddAsync(photo); } photoCounter++; } if (!(_context is null)) { // Save all additions to the database await _context.SaveChangesAsync(); } _logger.LogInformation($"End organizing. Organized { photoCounter } photos."); }
public async Task FindPhotosAsync_MultiLevelDirectory_MultipleFiles_JpegFormatAsync() { // Create temporary directory var tempDirectory = PathHelper.GetTemporaryDirectory(); // Find expected paths when creating files var expectedPaths = new HashSet <string>(); for (var i = 0; i < 10; i++) { // Creates list of directories used for creating path with max depth var directoryList = new List <string> { tempDirectory }; var rand = new Random().Next(0, MAXDEPTH); for (var k = 0; k < rand; k++) { directoryList.Add(Path.GetRandomFileName()); } // Combine the directory list and create path var tempPath = Path.Combine(directoryList.ToArray()); Directory.CreateDirectory(tempPath); // Create file inside the generated directory var tempFileName = PathHelper.CreateImageFile(tempPath, ImageFormat.Jpeg); // Adds path to expectedPaths var expectedPath = Path.Combine(tempPath, tempFileName); expectedPaths.Add(expectedPath); } // Find actual paths var photoList = new List <Photo>(); await foreach (var p in PhotoHandler.FindPhotosAsync(tempDirectory)) { photoList.Add(p); } var actualPaths = new HashSet <string>(); foreach (var photo in photoList) { actualPaths.Add(photo.FilePath); } // Assert the two sets are equal Assert.Subset(expectedPaths, actualPaths); Assert.Superset(expectedPaths, actualPaths); }
public async Task FindPhotosAsync_FlatDirectory_SingleFile_UnknownFormatAsync() { var tempDirectory = PathHelper.GetTemporaryDirectory(); PathHelper.CreateTmpFile(tempDirectory); // create tmp file with unsupported filetype var photoList = new List <Photo>(); await foreach (var photo in PhotoHandler.FindPhotosAsync(tempDirectory)) { photoList.Add(photo); } Assert.Empty(photoList); }
public async Task FindPhotosAsync_FlatDirectory_SingleFile_JpegFormatAsync() { var tempDirectory = PathHelper.GetTemporaryDirectory(); var tempPath = PathHelper.CreateImageFile(tempDirectory, ImageFormat.Jpeg); var expectedPath = Path.Combine(tempDirectory, tempPath); var photoList = new List <Photo>(); await foreach (var p in PhotoHandler.FindPhotosAsync(tempDirectory)) { photoList.Add(p); } var photo = photoList.FirstOrDefault(); var actualPath = photo.FilePath; Assert.Equal(expectedPath, actualPath); }