public void ComputeChecksum_MD5_InputFile() { var tempDirectory = PathHelper.GetTemporaryDirectory(); var filepath = PathHelper.CreateTmpFile(tempDirectory); var photo = new Photo(""); var cs = new Checksum(Algorithm.MD5); using var fs = File.OpenRead(filepath); // Assign checksum to photo object photo.Checksum = cs.ComputeChecksum(fs); // Reset stream fs.Position = 0; // Compute checksum using expected behavior using var md5 = MD5.Create(); var md5hash = md5.ComputeHash(fs); var expected = BitConverter.ToString(md5hash) .Replace("-", string.Empty) .ToLowerInvariant(); Assert.Equal(expected, photo.Checksum); }
public void Checksum_ComputeChecksum_NullStream() { using var cs = new Checksum(Algorithm.MD5); var actual = cs.ComputeChecksum(null); Assert.Null(actual); }
public void Checksum_ComputeChecksum_NoneAlgorithm() { var stream = new MemoryStream(Encoding.UTF8.GetBytes("Hello, World!")); var cs = new Checksum(Algorithm.None); var actual = cs.ComputeChecksum(stream); Assert.Null(actual); }
/// <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 void Checksum_ComputeChecksum_SHA256() { var stream = new MemoryStream(Encoding.UTF8.GetBytes("Hello, World!")); using var cs = new Checksum(Algorithm.SHA256); // Compute checksum, consuming stream var actual = cs.ComputeChecksum(stream); // Reset stream stream.Position = 0; // Compute checksum using expected behavior using var sha256 = SHA256.Create(); var sha256hash = sha256.ComputeHash(stream); var expected = BitConverter.ToString(sha256hash) .Replace("-", string.Empty) .ToLowerInvariant(); Assert.Equal(expected, actual); }