Working on the gui
This commit is contained in:
parent
52f07cf547
commit
3e40ab9eec
@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
|
@ -12,7 +12,12 @@ public static class FilesHelper
|
||||
var fileExt = Path.GetExtension(file);
|
||||
foreach (var supportedFileExtension in SupportedFileExtensions)
|
||||
{
|
||||
if (fileExt.Equals(supportedFileExtension, StringComparison.InvariantCultureIgnoreCase))
|
||||
if (
|
||||
fileExt.Equals(
|
||||
supportedFileExtension,
|
||||
StringComparison.InvariantCultureIgnoreCase
|
||||
)
|
||||
)
|
||||
yield return file;
|
||||
}
|
||||
}
|
||||
|
25
PhotoRenamer/FolderExtension.cs
Normal file
25
PhotoRenamer/FolderExtension.cs
Normal file
@ -0,0 +1,25 @@
|
||||
namespace PhotoRenamer;
|
||||
|
||||
public static class FolderExtension
|
||||
{
|
||||
public static string CreateFolderStructureByDate(string targetRoot, DateOnly dateTime)
|
||||
{
|
||||
var folder = BuildTargetDirectoryByDate(targetRoot, dateTime);
|
||||
if (!Directory.Exists(folder))
|
||||
{
|
||||
Directory.CreateDirectory(folder);
|
||||
}
|
||||
|
||||
return folder;
|
||||
}
|
||||
|
||||
public static string BuildTargetDirectoryByDate(string targetRoot, DateOnly dateTime)
|
||||
{
|
||||
return Path.Combine(targetRoot, dateTime.Year.ToString(), dateTime.Month.ToString("D2"));
|
||||
}
|
||||
|
||||
public static string[] GetAllFiles(string sourceRoot)
|
||||
{
|
||||
return Directory.GetFiles(sourceRoot, "*", SearchOption.AllDirectories);
|
||||
}
|
||||
}
|
61
PhotoRenamer/MediaFileExtension.cs
Normal file
61
PhotoRenamer/MediaFileExtension.cs
Normal file
@ -0,0 +1,61 @@
|
||||
using MetadataExtractor;
|
||||
using MetadataExtractor.Formats.Exif;
|
||||
using MetadataExtractor.Formats.QuickTime;
|
||||
using Serilog;
|
||||
|
||||
namespace PhotoRenamer;
|
||||
|
||||
public static class MediaFileExtension
|
||||
{
|
||||
public static DateTime GetDateTimeFromSourceFile(string file)
|
||||
{
|
||||
DateTime dateTime;
|
||||
try
|
||||
{
|
||||
var metadata = ImageMetadataReader.ReadMetadata(file);
|
||||
dateTime =
|
||||
GetDateTimeFromExif(metadata)
|
||||
?? GetDateTimeFromMp4(metadata)
|
||||
?? GetDateTimeFromLastWrite(file);
|
||||
}
|
||||
catch (ImageProcessingException e)
|
||||
{
|
||||
Log.Error(e, $"Error reading file information from {file}");
|
||||
dateTime = GetDateTimeFromLastWrite(file);
|
||||
}
|
||||
|
||||
return dateTime;
|
||||
}
|
||||
|
||||
private static DateTime GetDateTimeFromLastWrite(string file)
|
||||
{
|
||||
var creationTime = File.GetLastWriteTime(file);
|
||||
return creationTime;
|
||||
}
|
||||
|
||||
private static DateTime? GetDateTimeFromExif(
|
||||
IEnumerable<MetadataExtractor.Directory> directories
|
||||
)
|
||||
{
|
||||
return
|
||||
directories
|
||||
.OfType<ExifIfd0Directory>()
|
||||
.FirstOrDefault()
|
||||
?.TryGetDateTime(ExifDirectoryBase.TagDateTime, out var dateTime) == true
|
||||
? dateTime
|
||||
: null;
|
||||
}
|
||||
|
||||
private static DateTime? GetDateTimeFromMp4(
|
||||
IEnumerable<MetadataExtractor.Directory> directories
|
||||
)
|
||||
{
|
||||
return
|
||||
directories
|
||||
.OfType<QuickTimeMovieHeaderDirectory>()
|
||||
.FirstOrDefault()
|
||||
?.TryGetDateTime(QuickTimeMovieHeaderDirectory.TagCreated, out var dateTime) == true
|
||||
? dateTime
|
||||
: null;
|
||||
}
|
||||
}
|
@ -2,13 +2,13 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="PublishAotCompressed" Version="1.0.1" />
|
||||
<PackageReference Include="PublishAotCompressed" Version="1.0.2" />
|
||||
<PackageReference Include="MetadataExtractor" Version="2.8.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="7.0.0" />
|
||||
|
@ -28,6 +28,10 @@ internal static class Program
|
||||
public static IConfigurationBuilder CreateHostBuilder(string[] args) =>
|
||||
new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile(AppDomain.CurrentDomain.BaseDirectory + "\\appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile(
|
||||
AppDomain.CurrentDomain.BaseDirectory + "\\appsettings.json",
|
||||
optional: true,
|
||||
reloadOnChange: true
|
||||
)
|
||||
.AddCommandLine(args);
|
||||
}
|
||||
|
@ -1,11 +1,6 @@
|
||||
using System.Runtime.InteropServices.JavaScript;
|
||||
using MetadataExtractor;
|
||||
using MetadataExtractor.Formats.Exif;
|
||||
using MetadataExtractor.Formats.QuickTime;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Serilog;
|
||||
using Spectre.Console;
|
||||
using Directory = System.IO.Directory;
|
||||
|
||||
namespace PhotoRenamer;
|
||||
|
||||
@ -17,15 +12,21 @@ internal class Renamer
|
||||
|
||||
public Renamer(IConfiguration configuration)
|
||||
{
|
||||
_sourcePath = Path.GetFullPath(configuration["Source"]);
|
||||
_targetPath = Path.GetFullPath(configuration["Target"]);
|
||||
_sourcePath = Path.GetFullPath(
|
||||
configuration["Source"]
|
||||
?? throw new NotSupportedException("Source directory needs to be set.")
|
||||
);
|
||||
_targetPath = Path.GetFullPath(
|
||||
configuration["Target"]
|
||||
?? throw new NotSupportedException("Target directory needs to be set.")
|
||||
);
|
||||
Log.Information($"Source path: {_sourcePath}");
|
||||
Log.Information($"Target path: {_targetPath}");
|
||||
}
|
||||
|
||||
public async Task<int> RunAsync()
|
||||
{
|
||||
var files = Directory.GetFiles(_sourcePath, "*", SearchOption.AllDirectories);
|
||||
var files = FolderExtension.GetAllFiles(_sourcePath);
|
||||
_currentCount = 0;
|
||||
var po = new ParallelOptions { MaxDegreeOfParallelism = 3 };
|
||||
await AnsiConsole
|
||||
@ -37,24 +38,22 @@ internal class Renamer
|
||||
return 0;
|
||||
}
|
||||
|
||||
private async ValueTask Body(string file, ProgressContext progressContext, CancellationToken token)
|
||||
private async ValueTask Body(
|
||||
string file,
|
||||
ProgressContext progressContext,
|
||||
CancellationToken token
|
||||
)
|
||||
{
|
||||
if (token.IsCancellationRequested)
|
||||
return;
|
||||
var task = progressContext.AddTask($"[green]{file}[/]");
|
||||
try
|
||||
{
|
||||
DateTime dateTime;
|
||||
try
|
||||
{
|
||||
var directories = ImageMetadataReader.ReadMetadata(file);
|
||||
dateTime = GetDateTimeFromExif(directories) ?? GetDateTimeFromMp4(directories) ?? GetDateTimeFromLastWrite(file);
|
||||
}
|
||||
catch (ImageProcessingException e)
|
||||
{
|
||||
Log.Error(e, $"Error reading file information from {file}");
|
||||
dateTime = GetDateTimeFromLastWrite(file);
|
||||
}
|
||||
|
||||
var folder = CreateFolder(dateTime);
|
||||
var dateTime = MediaFileExtension.GetDateTimeFromSourceFile(file);
|
||||
var folder = FolderExtension.CreateFolderStructureByDate(
|
||||
_targetPath,
|
||||
DateOnly.FromDateTime(dateTime)
|
||||
);
|
||||
var progress = new Progress<double>(v => task.Value = v * 100);
|
||||
|
||||
await CopyFileAsync(folder, file, progress);
|
||||
@ -70,22 +69,6 @@ internal class Renamer
|
||||
}
|
||||
}
|
||||
|
||||
private static DateTime GetDateTimeFromLastWrite(string file)
|
||||
{
|
||||
var creationTime = File.GetLastWriteTime(file);
|
||||
return creationTime;
|
||||
}
|
||||
|
||||
private static DateTime? GetDateTimeFromExif(IEnumerable<MetadataExtractor.Directory> directories)
|
||||
{
|
||||
return directories.OfType<ExifIfd0Directory>().FirstOrDefault()?.TryGetDateTime(ExifDirectoryBase.TagDateTime, out var dateTime) == true ? dateTime : null;
|
||||
}
|
||||
|
||||
private static DateTime? GetDateTimeFromMp4(IEnumerable<MetadataExtractor.Directory> directories)
|
||||
{
|
||||
return directories.OfType<QuickTimeMovieHeaderDirectory>().FirstOrDefault()?.TryGetDateTime(QuickTimeMovieHeaderDirectory.TagCreated, out var dateTime) == true ? dateTime : null;
|
||||
}
|
||||
|
||||
private static void ResetTimes(FileSystemInfo destination, FileSystemInfo source)
|
||||
{
|
||||
destination.LastWriteTime = source.LastWriteTime;
|
||||
@ -94,7 +77,11 @@ internal class Renamer
|
||||
destination.CreationTimeUtc = source.CreationTimeUtc;
|
||||
}
|
||||
|
||||
private static async Task CopyFileAsync(string folder, string file, IProgress<double>? progress = null)
|
||||
private static async Task CopyFileAsync(
|
||||
string folder,
|
||||
string file,
|
||||
IProgress<double>? progress = null
|
||||
)
|
||||
{
|
||||
var destination = new FileInfo(Path.Combine(folder, Path.GetFileName(file)));
|
||||
var source = new FileInfo(file);
|
||||
@ -104,22 +91,21 @@ internal class Renamer
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
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
|
||||
);
|
||||
|
||||
await sourceStream.CopyToAsync(targetStream, 81920, progress: progress);
|
||||
|
||||
ResetTimes(destination, source);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,13 @@
|
||||
|
||||
public static class StreamExtensions
|
||||
{
|
||||
public static async Task CopyToAsync(this Stream source, Stream destination, int bufferSize, IProgress<double>? progress = null, CancellationToken cancellationToken = default)
|
||||
public static async Task CopyToAsync(
|
||||
this Stream source,
|
||||
Stream destination,
|
||||
int bufferSize,
|
||||
IProgress<double>? progress = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
if (source == null)
|
||||
throw new ArgumentNullException(nameof(source));
|
||||
@ -18,9 +24,14 @@ public static class StreamExtensions
|
||||
var buffer = new byte[bufferSize];
|
||||
long totalBytesRead = 0;
|
||||
int bytesRead;
|
||||
while ((bytesRead = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) != 0)
|
||||
while (
|
||||
(bytesRead = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false))
|
||||
!= 0
|
||||
)
|
||||
{
|
||||
await destination.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken).ConfigureAwait(false);
|
||||
await destination
|
||||
.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
totalBytesRead += bytesRead;
|
||||
progress?.Report(totalBytesRead / (double)source.Length);
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
{
|
||||
"Source": "C:\\Users\\Holger\\Nextcloud\\DCIM",
|
||||
"Target": "C:\\Users\\Holger\\Nextcloud\\DCIM"
|
||||
"Source": "/Volumes/EOS_DIGITAL/DCIM/100CANON",
|
||||
"Target": "/Users/holger/Nextcloud/SofortUploadCamera"
|
||||
}
|
@ -1,10 +1,12 @@
|
||||
<Application xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
x:Class="PhotoRenamerGui.App"
|
||||
RequestedThemeVariant="Default">
|
||||
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
|
||||
<Application
|
||||
RequestedThemeVariant="Default"
|
||||
x:Class="PhotoRenamerGui.App"
|
||||
xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->
|
||||
|
||||
<Application.Styles>
|
||||
<FluentTheme />
|
||||
<StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml" />
|
||||
</Application.Styles>
|
||||
</Application>
|
@ -57,5 +57,23 @@
|
||||
VerticalAlignment="Stretch" />
|
||||
</TextBox.InnerRightContent>
|
||||
</TextBox>
|
||||
|
||||
|
||||
<DataGrid
|
||||
AutoGenerateColumns="False"
|
||||
BorderBrush="Gray"
|
||||
BorderThickness="1"
|
||||
Grid.Column="0"
|
||||
Grid.ColumnSpan="2"
|
||||
Grid.Row="2"
|
||||
GridLinesVisibility="All"
|
||||
IsReadOnly="True"
|
||||
ItemsSource="{Binding MediaFiles}">
|
||||
<DataGrid.Columns>
|
||||
<DataGridCheckBoxColumn Binding="{Binding IsSelected}" />
|
||||
<DataGridTextColumn Binding="{Binding SourceDirectory}" Header="Source" />
|
||||
<DataGridTextColumn Binding="{Binding TargetDirectory}" Header="Target" />
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
</Grid>
|
||||
</Window>
|
||||
|
@ -1,9 +1,13 @@
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls.ApplicationLifetimes;
|
||||
using Avalonia.Platform.Storage;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using PhotoRenamer;
|
||||
|
||||
namespace PhotoRenamerGui;
|
||||
|
||||
@ -11,24 +15,64 @@ public partial class MainWindowViewModel : ObservableObject
|
||||
{
|
||||
[ObservableProperty] private string? _inputFolder;
|
||||
[ObservableProperty] private string? _outputFolder;
|
||||
public ObservableCollection<MediaFile> MediaFiles { get; } = new();
|
||||
|
||||
[RelayCommand]
|
||||
private async Task SelectInputFolder()
|
||||
private async Task SelectInputFolderAsync()
|
||||
{
|
||||
InputFolder = await SearchFolder();
|
||||
InputFolder = await SearchFolderAsync();
|
||||
}
|
||||
|
||||
|
||||
[RelayCommand]
|
||||
private async Task SelectOutputFolder()
|
||||
private async Task SelectOutputFolderAsync()
|
||||
{
|
||||
OutputFolder = await SearchFolder();
|
||||
OutputFolder = await SearchFolderAsync();
|
||||
}
|
||||
|
||||
private async Task<string?> SearchFolder()
|
||||
|
||||
private static async Task<string?> SearchFolderAsync()
|
||||
{
|
||||
var storageProvider = (App.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime).MainWindow
|
||||
var storageProvider = (Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)
|
||||
?.MainWindow?
|
||||
.StorageProvider;
|
||||
var result =await storageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions());
|
||||
return result.FirstOrDefault()?.TryGetLocalPath();
|
||||
if (storageProvider is null) return "<not accessible>";
|
||||
var result = await storageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions());
|
||||
return result.Count > 0 ? result[0].TryGetLocalPath() : null;
|
||||
}
|
||||
|
||||
partial void OnInputFolderChanged(string? value)
|
||||
{
|
||||
UpdatePreviewList();
|
||||
}
|
||||
|
||||
partial void OnOutputFolderChanged(string? value)
|
||||
{
|
||||
UpdatePreviewList();
|
||||
}
|
||||
|
||||
private void UpdatePreviewList()
|
||||
{
|
||||
MediaFiles.Clear();
|
||||
if (string.IsNullOrWhiteSpace(InputFolder)) return;
|
||||
if (string.IsNullOrWhiteSpace(OutputFolder)) return;
|
||||
var allFiles = FolderExtension.GetAllFiles(InputFolder);
|
||||
foreach (var file in allFiles)
|
||||
{
|
||||
var source = Path.GetRelativePath(InputFolder, file);
|
||||
var date = MediaFileExtension.GetDateTimeFromSourceFile(file);
|
||||
var target = FolderExtension.BuildTargetDirectoryByDate(OutputFolder, DateOnly.FromDateTime(date));
|
||||
|
||||
MediaFiles.Add(new MediaFile(true, source, Path.GetRelativePath(OutputFolder, target)));
|
||||
}
|
||||
|
||||
//TODO
|
||||
}
|
||||
}
|
||||
|
||||
public class MediaFile(bool isSelected, string? sourceDirectory, string? targetDirectory) : ObservableObject
|
||||
{
|
||||
public bool IsSelected { get=> isSelected;
|
||||
set => SetProperty(ref isSelected, value);
|
||||
}
|
||||
public string? SourceDirectory { get; } = sourceDirectory;
|
||||
public string? TargetDirectory { get; } = targetDirectory;
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
|
||||
<ApplicationManifest>app.manifest</ApplicationManifest>
|
||||
@ -10,12 +10,18 @@
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Avalonia" Version="11.0.4" />
|
||||
<PackageReference Include="Avalonia.Desktop" Version="11.0.4" />
|
||||
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.4" />
|
||||
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.4" />
|
||||
<PackageReference Include="Avalonia" Version="11.0.5" />
|
||||
<PackageReference Include="Avalonia.Controls.DataGrid" Version="11.0.5" />
|
||||
<PackageReference Include="Avalonia.Desktop" Version="11.0.5" />
|
||||
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.5" />
|
||||
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.5" />
|
||||
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
|
||||
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.4" />
|
||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
|
||||
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.5" />
|
||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PhotoRenamer\PhotoRenamer.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
Loading…
x
Reference in New Issue
Block a user