woring version

This commit is contained in:
Holger Börchers 2020-05-22 22:06:32 +02:00
parent aad679b3f1
commit 1d316fbe27
7 changed files with 111 additions and 37 deletions

7
.vscode/launch.json vendored Normal file
View File

@ -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": []
}

42
.vscode/tasks.json vendored Normal file
View File

@ -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"
}
]
}

View File

@ -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);
}
}
}

View File

@ -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<MediaFile> GetMediaFiles(IEnumerable<string> files)
{
if (files == null) throw new ArgumentNullException(nameof(files));
foreach (var file in files)
{
var imageFile = ImageFile.FromFile(file);
var dateTime = imageFile.Properties.Get<ExifDateTime>(ExifTag.DateTimeOriginal);
var mediaFile = new MediaFile(file, dateTime.Value);
yield return mediaFile;
}
}
}
}

View File

@ -6,14 +6,14 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ExifLibNet" Version="2.1.1" />
<PackageReference Include="MetadataExtractor" Version="2.4.2" />
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.4" />
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
</ItemGroup>
</Project>

View File

@ -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);
}
}

View File

@ -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<Renamer> _logger;
private readonly string _sourcePath;
private readonly string _targetPath;
public Renamer(IConfigurationRoot configuration, ILoggerFactory loggerFactory)
public Renamer(IConfigurationRoot configuration)
{
_configuration = configuration;
_logger = loggerFactory.CreateLogger<Renamer>();
_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<MetadataExtractor.Directory> directories)
{
return directories
.OfType<ExifSubIfdDirectory>()
.FirstOrDefault()?.GetDateTime(ExifDirectoryBase.TagDateTimeOriginal);
}
private static DateTime? GetDateTimeFromMp4(IEnumerable<MetadataExtractor.Directory> directories)
{
return directories
.OfType<QuickTimeMovieHeaderDirectory>()
.FirstOrDefault()?.GetDateTime(QuickTimeMovieHeaderDirectory.TagCreated);
}
}
}