From e705d0d286df38577cd1d4ba8f34c0b17a8f17db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20B=C3=B6rchers?= Date: Thu, 1 Dec 2022 14:07:25 +0100 Subject: [PATCH] code cleanup --- PhotoRenamer/Renamer.cs | 217 ++++++++++++++++---------------- PhotoRenamer/Types/MediaFile.cs | 7 +- 2 files changed, 111 insertions(+), 113 deletions(-) diff --git a/PhotoRenamer/Renamer.cs b/PhotoRenamer/Renamer.cs index 9353c2e..8b3cf56 100644 --- a/PhotoRenamer/Renamer.cs +++ b/PhotoRenamer/Renamer.cs @@ -6,122 +6,121 @@ using Serilog; using ShellProgressBar; using Directory = System.IO.Directory; -namespace PhotoRenamer +namespace PhotoRenamer; + +internal class Renamer { - internal class Renamer + private readonly ProgressBarOptions _childOptions; + private readonly string _sourcePath; + private readonly string _targetPath; + private int _currentCount; + + public Renamer(IConfiguration configuration) { - private readonly ProgressBarOptions _childOptions; - private readonly string _sourcePath; - private readonly string _targetPath; - private int _currentCount; - - public Renamer(IConfiguration configuration) + _sourcePath = Path.GetFullPath(configuration["Source"]); + _targetPath = Path.GetFullPath(configuration["Target"]); + Log.Information($"Source path: {_sourcePath}"); + Log.Information($"Target path: {_targetPath}"); + _childOptions = new ProgressBarOptions { - _sourcePath = Path.GetFullPath(configuration["Source"]); - _targetPath = Path.GetFullPath(configuration["Target"]); - Log.Information($"Source path: {_sourcePath}"); - Log.Information($"Target path: {_targetPath}"); - _childOptions = new ProgressBarOptions - { - ForegroundColor = ConsoleColor.Yellow, - BackgroundColor = ConsoleColor.DarkGreen, - ProgressCharacter = '─', - CollapseWhenFinished = true - }; + ForegroundColor = ConsoleColor.Yellow, + BackgroundColor = ConsoleColor.DarkGreen, + ProgressCharacter = '─', + CollapseWhenFinished = true + }; + } + + public int Run() + { + var files = Directory.GetFiles(_sourcePath, "*", SearchOption.AllDirectories); + var options = new ProgressBarOptions + { + ForegroundColor = ConsoleColor.Yellow, + ForegroundColorDone = ConsoleColor.DarkGreen, + BackgroundColor = ConsoleColor.DarkGray, + BackgroundCharacter = '\u2593' + }; + _currentCount = 0; + using var progressBar = new ProgressBar(files.Length, "Copying files", options); + var po = new ParallelOptions { MaxDegreeOfParallelism = 1 }; + + Parallel.ForEach(files, po, file => Body(file, progressBar)); + + return 0; + } + + private void Body(string file, ProgressBar progressBar) + { + try + { + var directories = ImageMetadataReader.ReadMetadata(file); + var dateTime = GetDateTimeFromExif(directories) ?? GetDateTimeFromMp4(directories) ?? GetDateTimeFromLastWrite(file); + var folder = CreateFolder(dateTime); + CopyFile(folder, file, progressBar); } - - public int Run() + catch (ImageProcessingException) { - var files = Directory.GetFiles(_sourcePath, "*", SearchOption.AllDirectories); - var options = new ProgressBarOptions - { - ForegroundColor = ConsoleColor.Yellow, - ForegroundColorDone = ConsoleColor.DarkGreen, - BackgroundColor = ConsoleColor.DarkGray, - BackgroundCharacter = '\u2593' - }; - _currentCount = 0; - using var progressBar = new ProgressBar(files.Length, "Copying files", options); - var po = new ParallelOptions { MaxDegreeOfParallelism = 1 }; - - Parallel.ForEach(files, po, file => Body(file, progressBar)); - - return 0; + //silently ignore } - - private void Body(string file, ProgressBar progressBar) + finally { - try - { - var directories = ImageMetadataReader.ReadMetadata(file); - var dateTime = GetDateTimeFromExif(directories) ?? GetDateTimeFromMp4(directories) ?? GetDateTimeFromLastWrite(file); - var folder = CreateFolder(dateTime); - CopyFile(folder, file, progressBar); - } - catch (ImageProcessingException) - { - //silently ignore - } - finally - { - Interlocked.Increment(ref _currentCount); - progressBar.Tick(_currentCount); - } - } - - private static DateTime GetDateTimeFromLastWrite(string file) - { - var creationTime = File.GetLastWriteTime(file); - return creationTime; - } - - private static DateTime? GetDateTimeFromExif(IEnumerable directories) - { - return directories.OfType().FirstOrDefault()?.TryGetDateTime(ExifDirectoryBase.TagDateTime, out var dateTime) == true ? dateTime : null; - } - - private static DateTime? GetDateTimeFromMp4(IEnumerable directories) - { - return directories.OfType().FirstOrDefault()?.TryGetDateTime(QuickTimeMovieHeaderDirectory.TagCreated, out var dateTime) == true ? dateTime : null; - } - - private static void ResetTimes(FileSystemInfo destination, FileSystemInfo source) - { - destination.LastWriteTime = source.LastWriteTime; - destination.LastWriteTimeUtc = source.LastWriteTimeUtc; - destination.CreationTime = source.CreationTime; - destination.CreationTimeUtc = source.CreationTimeUtc; - } - - private async void CopyFile(string folder, string file, ProgressBarBase progressBar) - { - var destination = new FileInfo(Path.Combine(folder, Path.GetFileName(file))); - var source = new FileInfo(file); - if (destination.Exists && destination.Length == source.Length) - return; - - using var child = progressBar.Spawn(100, destination.FullName, _childOptions); - - await using var sourceStream = File.Open(source.FullName, FileMode.Open, FileAccess.Read, FileShare.Read); - await using var targetStream = File.Open(destination.FullName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); - - var progress = new Progress(o => progressBar.Tick((int)o * 100)); - await sourceStream.CopyToAsync(targetStream, 81920, progress); - - ResetTimes(destination, source); - - child.Tick(100); - } - - private string CreateFolder(DateTime dateTime) - { - var folder = Path.Combine(_targetPath, dateTime.Year.ToString(), dateTime.Month.ToString("D2")); - if (!Directory.Exists(folder)) - { - Directory.CreateDirectory(folder); - } - - return folder; + Interlocked.Increment(ref _currentCount); + progressBar.Tick(_currentCount); } } + + private static DateTime GetDateTimeFromLastWrite(string file) + { + var creationTime = File.GetLastWriteTime(file); + return creationTime; + } + + private static DateTime? GetDateTimeFromExif(IEnumerable directories) + { + return directories.OfType().FirstOrDefault()?.TryGetDateTime(ExifDirectoryBase.TagDateTime, out var dateTime) == true ? dateTime : null; + } + + private static DateTime? GetDateTimeFromMp4(IEnumerable directories) + { + return directories.OfType().FirstOrDefault()?.TryGetDateTime(QuickTimeMovieHeaderDirectory.TagCreated, out var dateTime) == true ? dateTime : null; + } + + private static void ResetTimes(FileSystemInfo destination, FileSystemInfo source) + { + destination.LastWriteTime = source.LastWriteTime; + destination.LastWriteTimeUtc = source.LastWriteTimeUtc; + destination.CreationTime = source.CreationTime; + destination.CreationTimeUtc = source.CreationTimeUtc; + } + + private async void CopyFile(string folder, string file, ProgressBarBase progressBar) + { + var destination = new FileInfo(Path.Combine(folder, Path.GetFileName(file))); + var source = new FileInfo(file); + if (destination.Exists && destination.Length == source.Length) + return; + + using var child = progressBar.Spawn(100, destination.FullName, _childOptions); + + await using var sourceStream = File.Open(source.FullName, FileMode.Open, FileAccess.Read, FileShare.Read); + await using var targetStream = File.Open(destination.FullName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); + + var progress = new Progress(o => progressBar.Tick((int)o * 100)); + await sourceStream.CopyToAsync(targetStream, 81920, progress); + + ResetTimes(destination, source); + + child.Tick(100); + } + + private string CreateFolder(DateTime dateTime) + { + var folder = Path.Combine(_targetPath, dateTime.Year.ToString(), dateTime.Month.ToString("D2")); + if (!Directory.Exists(folder)) + { + Directory.CreateDirectory(folder); + } + + return folder; + } } diff --git a/PhotoRenamer/Types/MediaFile.cs b/PhotoRenamer/Types/MediaFile.cs index b7c087c..9aa8d30 100644 --- a/PhotoRenamer/Types/MediaFile.cs +++ b/PhotoRenamer/Types/MediaFile.cs @@ -1,4 +1,3 @@ -namespace PhotoRenamer.Types -{ - public record MediaFile(string Path, DateTime CreationDate); -} +namespace PhotoRenamer.Types; + +public record MediaFile(string Path, DateTime CreationDate);