From 1d316fbe27f3556ed6f8f92e0854f6448efaad58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20B=C3=B6rchers?= Date: Fri, 22 May 2020 22:06:32 +0200 Subject: [PATCH] woring version --- .vscode/launch.json | 7 ++++ .vscode/tasks.json | 42 +++++++++++++++++++++++ PhotoRenamer.Test/FilesTest.cs | 2 +- PhotoRenamer/FilesHelper.cs | 19 ++--------- PhotoRenamer/PhotoRenamer.csproj | 6 ++-- PhotoRenamer/Program.cs | 15 +++++---- PhotoRenamer/Renamer.cs | 57 ++++++++++++++++++++++++++------ 7 files changed, 111 insertions(+), 37 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5c7247b --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,7 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..d04cf29 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,42 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/PhotoRenamer/PhotoRenamer.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/PhotoRenamer/PhotoRenamer.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "${workspaceFolder}/PhotoRenamer/PhotoRenamer.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/PhotoRenamer.Test/FilesTest.cs b/PhotoRenamer.Test/FilesTest.cs index 2769679..03d4686 100644 --- a/PhotoRenamer.Test/FilesTest.cs +++ b/PhotoRenamer.Test/FilesTest.cs @@ -29,7 +29,7 @@ namespace PhotoRenamer.Test public void GetMetaData() { var expected = new[] {new MediaFile("r", DateTime.Now), new MediaFile("r", DateTime.Now)}; - var actual = FilesHelper.GetMediaFiles(_files); + // var actual = FilesHelper.GetMediaFiles(_files); } } } \ No newline at end of file diff --git a/PhotoRenamer/FilesHelper.cs b/PhotoRenamer/FilesHelper.cs index 3ea0cb5..254549b 100644 --- a/PhotoRenamer/FilesHelper.cs +++ b/PhotoRenamer/FilesHelper.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using ExifLibrary; using PhotoRenamer.Types; namespace PhotoRenamer @@ -16,25 +15,13 @@ namespace PhotoRenamer foreach (var file in files) { var fileExt = Path.GetExtension(file); - if(fileExt == null) continue; + if (fileExt == null) continue; foreach (var supportedFileExtension in SupportedFileExtensions) { - if (fileExt.Equals(supportedFileExtension, StringComparison.InvariantCultureIgnoreCase)) yield return file; + if (fileExt.Equals(supportedFileExtension, StringComparison.InvariantCultureIgnoreCase)) + yield return file; } } } - - public static IEnumerable GetMediaFiles(IEnumerable files) - { - if (files == null) throw new ArgumentNullException(nameof(files)); - - foreach (var file in files) - { - var imageFile = ImageFile.FromFile(file); - var dateTime = imageFile.Properties.Get(ExifTag.DateTimeOriginal); - var mediaFile = new MediaFile(file, dateTime.Value); - yield return mediaFile; - } - } } } diff --git a/PhotoRenamer/PhotoRenamer.csproj b/PhotoRenamer/PhotoRenamer.csproj index 52e0aca..2911fbb 100644 --- a/PhotoRenamer/PhotoRenamer.csproj +++ b/PhotoRenamer/PhotoRenamer.csproj @@ -6,14 +6,14 @@ - + - - + + diff --git a/PhotoRenamer/Program.cs b/PhotoRenamer/Program.cs index dab416a..f6e2e39 100644 --- a/PhotoRenamer/Program.cs +++ b/PhotoRenamer/Program.cs @@ -1,7 +1,7 @@ using System; using System.IO; using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; +using Serilog; namespace PhotoRenamer { @@ -10,16 +10,19 @@ namespace PhotoRenamer private static int Main(string[] args) { var configuration = CreateHostBuilder(args).Build(); - var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()); - var logger = loggerFactory.CreateLogger(nameof(Program)); + foreach (var (key, value) in configuration.AsEnumerable()) + { + Console.WriteLine($"{{{key}: {value}}}"); + } + Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateLogger(); try { - var p = new Renamer(configuration, loggerFactory); + var p = new Renamer(configuration); return p.Run(); } catch (Exception e) { - logger.LogError(e, "Error executing program"); + Log.Error(e, "Error executing program"); return -1; } } @@ -28,8 +31,6 @@ namespace PhotoRenamer .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile(AppDomain.CurrentDomain.BaseDirectory + "\\appsettings.json", optional: true, reloadOnChange: true) - .AddEnvironmentVariables() .AddCommandLine(args); - } } diff --git a/PhotoRenamer/Renamer.cs b/PhotoRenamer/Renamer.cs index 9439b3d..84b7f1c 100644 --- a/PhotoRenamer/Renamer.cs +++ b/PhotoRenamer/Renamer.cs @@ -1,27 +1,64 @@ using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using MetadataExtractor; +using MetadataExtractor.Formats.Exif; +using MetadataExtractor.Formats.QuickTime; using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; +using Serilog; +using Directory = System.IO.Directory; namespace PhotoRenamer { internal class Renamer { - private readonly IConfigurationRoot _configuration; - private readonly ILogger _logger; + private readonly string _sourcePath; + private readonly string _targetPath; - public Renamer(IConfigurationRoot configuration, ILoggerFactory loggerFactory) + public Renamer(IConfigurationRoot configuration) { - _configuration = configuration; - _logger = loggerFactory.CreateLogger(); + _sourcePath = Path.GetFullPath(configuration["Source"]); + _targetPath = Path.GetFullPath(configuration["Target"]); + Log.Information($"Source path {_sourcePath}"); + Log.Information($"Target path {_targetPath}"); } public int Run() { - var sourcePath = _configuration["Source"]; - var targetPath = _configuration["Target"]; - _logger.LogInformation($"Source path {sourcePath}"); - _logger.LogInformation($"Target path {targetPath}"); + var files = Directory.EnumerateFiles(_sourcePath); + foreach (var file in files) + { + var directories = ImageMetadataReader.ReadMetadata(file); + + var dateTime = GetDateTimeFromExif(directories) ?? GetDateTimeFromMp4(directories); + if (dateTime == null) continue; + var yearFolder = Path.Combine(_targetPath, dateTime.Value.Year.ToString()); + var monthFolder = Path.Combine(yearFolder, dateTime.Value.Month.ToString("D2")); + var dayFolder = Path.Combine(monthFolder, dateTime.Value.Day.ToString("D2")); + if (!Directory.Exists(yearFolder)) Directory.CreateDirectory(yearFolder); + if (!Directory.Exists(monthFolder)) Directory.CreateDirectory(monthFolder); + if (!Directory.Exists(dayFolder)) Directory.CreateDirectory(dayFolder); + var destination = Path.Combine(dayFolder, Path.GetFileName(file)); + if (File.Exists(destination)) continue; + File.Copy(file, destination); + } + return 0; } + + private static DateTime? GetDateTimeFromExif(IEnumerable directories) + { + return directories + .OfType() + .FirstOrDefault()?.GetDateTime(ExifDirectoryBase.TagDateTimeOriginal); + } + + private static DateTime? GetDateTimeFromMp4(IEnumerable directories) + { + return directories + .OfType() + .FirstOrDefault()?.GetDateTime(QuickTimeMovieHeaderDirectory.TagCreated); + } } } \ No newline at end of file