public async Task RunExifToolWithThreeCommands() { // arrange var sut = new AsyncExifTool(AsyncExifToolConfigurationFactory.Create()); sut.Initialize(); // act var task1 = sut.ExecuteAsync(image); var task2 = sut.ExecuteAsync(image); var task3 = sut.ExecuteAsync(image); // assert var result3 = await task3.ConfigureAwait(false); result3.Should().NotBeNullOrEmpty(); var result2 = await task2.ConfigureAwait(false); result2.Should().NotBeNullOrEmpty(); var result1 = await task1.ConfigureAwait(false); result1.Should().NotBeNullOrEmpty(); await sut.DisposeAsync().ConfigureAwait(false); }
public async Task WriteCustomXmpTagsToImageTest() { // arrange #if NETCOREAPP3_0 await using var sut = new AsyncExifTool(AsyncExifToolConfigurationFactory.CreateWithCustomConfig()); #else using var sut = new AsyncExifTool(AsyncExifToolConfigurationFactory.CreateWithCustomConfig()); #endif sut.Initialize(); // act var @params = new[] { "-XMP-CoenmAsyncExifTool:MyCustomId=test123", "-XMP-CoenmAsyncExifTool:MyCustomTimestamp=2020:05:08 12:00:45+02:00", "-XMP-CoenmAsyncExifTool:MyCustomTags+=holidays", "-XMP-CoenmAsyncExifTool:MyCustomTags+=summer", image, }; var readResultBefore = await sut.ExecuteAsync(image).ConfigureAwait(false); var writeResult = await sut.ExecuteAsync(@params).ConfigureAwait(false); var readResultAfter = await sut.ExecuteAsync(image).ConfigureAwait(false); // assert writeResult.Trim().Should().Be("1 image files updated"); readResultBefore.Should().NotContain("My Custom Id : test123"); readResultBefore.Should().NotContain("My Custom Tags : holidays, summer"); readResultBefore.Should().NotContain("My Custom Timestamp : 2020:05:08 12:00:45"); readResultAfter.Should().Contain("My Custom Id : test123"); readResultAfter.Should().Contain("My Custom Tags : holidays, summer"); readResultAfter.Should().Contain("My Custom Timestamp : 2020:05:08 12:00:45+02:00"); // just for fun output.WriteLine(readResultAfter); }
public async Task WriteXmpSubjectsToImageTest() { // arrange #if NETCOREAPP3_0 await using var sut = new AsyncExifTool(AsyncExifToolConfigurationFactory.Create()); #else using var sut = new AsyncExifTool(AsyncExifToolConfigurationFactory.Create()); #endif sut.Initialize(); // act var @params = new[] { "-XMP-dc:Subject+=def", "-XMP-dc:Subject+=abc", "-XMP-dc:Subject=xyz", image, }; var readResultBefore = await sut.ExecuteAsync(image).ConfigureAwait(false); var writeResult = await sut.ExecuteAsync(@params).ConfigureAwait(false); var readResultAfter = await sut.ExecuteAsync(image).ConfigureAwait(false); // assert readResultBefore.Should().Contain("Subject : dog, new york, puppy"); readResultBefore.Should().NotContain("Subject : dog, new york, puppy, def, abc, xyz"); writeResult.Should().Be(" 1 image files updated" + Environment.NewLine); readResultAfter.Should().Contain("Subject : dog, new york, puppy, def, abc, xyz" + Environment.NewLine); // just for fun output.WriteLine(readResultAfter); }
private static void ParseImageFile(string path, AsyncExifTool asyncExifTool, Dictionary <string, Dictionary <string, ExifData> > data) { Console.WriteLine("Processing {0}...", path); var fileData = new Dictionary <string, ExifData>(); data.Add(path, fileData); ParseFileData(asyncExifTool.ExecuteAsync(new[] { "-a", "-g", "-sort", path }).ConfigureAwait(false).GetAwaiter().GetResult(), fileData, true); ParseFileData(asyncExifTool.ExecuteAsync(new[] { "-a", "-g", "-n", "-sort", path }).ConfigureAwait(false).GetAwaiter().GetResult(), fileData, false); }
public async Task RunExiftoolGetImageSizeAndExposureTime() { // arrange var sut = new AsyncExifTool(AsyncExifToolConfigurationFactory.Create()); sut.Initialize(); // act var result = await sut.ExecuteAsync( new[] { "-s", "-ImageSize", "-ExposureTime", image, }) .ConfigureAwait(false); // assert result.Should().Be("ImageSize : 1712x2288" + Environment.NewLine); await sut.DisposeAsync().ConfigureAwait(false); // just for fun output.WriteLine(image); output.WriteLine(result); }
public async Task RunExiftool_ShouldReturnEmpty_WhenQueriedTagDoesNotExist() { // arrange var sut = new AsyncExifTool(AsyncExifToolConfigurationFactory.Create()); sut.Initialize(); // act var result = await sut.ExecuteAsync( new[] { "-ExposureTime", image, }) .ConfigureAwait(false); // assert result.Should().Be(string.Empty); await sut.DisposeAsync().ConfigureAwait(false); // just for fun output.WriteLine(image); output.WriteLine(result); }
public async Task <int> GetImageRotationDegreesAsync(string filename) { var result = await asyncExifTool.ExecuteAsync(new[] { "-Orientation", "-n", filename }); if (result == null) { return(0); } result = result.ToLower(); result = result.Replace("orientation", string.Empty); result = result.Replace(":", string.Empty); result = result.Trim(); if (!int.TryParse(result, NumberStyles.None, new NumberFormatInfo(), out var intResult)) { return(0); } return(intResult switch { 1 => 0, 3 => 180, 6 => 90, 8 => 270, _ => 0 });
/// <summary> /// Execute single command on ExifTool instance. /// </summary> /// <param name="this">Instance of <see cref="AsyncExifTool"/> to use to execute this command. Cannot be <c>null</c>.</param> /// <param name="singleArg">The command to run on exiftool. For instance `-ver`. See the ExifTool website for all options.</param> /// <param name="ct">Cancellation token. Optional. Defaults to <see cref="CancellationToken.None"/>.</param> /// <returns>ExifTool result containing the version information.</returns> /// <exception cref="ArgumentNullException">Thrown when <paramref name="this"/> is <c>null</c>.</exception> public static Task <string> ExecuteAsync([NotNull] this AsyncExifTool @this, string singleArg, CancellationToken ct = default) { if (@this == null) { throw new ArgumentNullException(nameof(@this)); } return(@this.ExecuteAsync(new[] { singleArg }, ct)); }
public async Task RunExiftoolForVersionAndImageErrorTest() { // arrange var sut = new AsyncExifTool(AsyncExifToolConfigurationFactory.Create()); sut.Initialize(); // act var getMetadataNonExistingImageTask = sut.ExecuteAsync(image + "does not exist"); Func <Task> act = async() => await getMetadataNonExistingImageTask; var version = await sut.GetVersionAsync().ConfigureAwait(false); // assert act.Should().Throw <Exception>(); version.Should().NotBeNullOrEmpty(); await sut.DisposeAsync().ConfigureAwait(false); }
public async Task RunExiftoolForVersionAndImageTest() { // arrange var sut = new AsyncExifTool(AsyncExifToolConfigurationFactory.Create()); sut.Initialize(); // act var version = await sut.GetVersionAsync().ConfigureAwait(false); var result = await sut.ExecuteAsync(image).ConfigureAwait(false); // assert version.Should().NotBeNullOrEmpty(); result.Should().NotBeNullOrEmpty(); await sut.DisposeAsync().ConfigureAwait(false); // just for fun output.WriteLine(version); output.WriteLine(result); }
public static async Task Main() { ConfigureNLog(); var nlogLogger = LogManager.GetCurrentClassLogger(typeof(AsyncExifTool)); Console.WriteLine("Sample application using AsyncExifTool in combination with NLog"); // AsyncExifTool configuration. Please make sure exiftool.exe is accessible. var commonArgs = new List <string>(); var asyncExifToolConfiguration = new AsyncExifToolConfiguration( "exiftool.exe", Encoding.UTF8, commonArgs); // Create a logger for AsyncExifTool. AsyncExifTool does not require any logging framework. You have to write your own adapter. var logger = new AsyncExifToolToNLogAdapter(nlogLogger); // Create AsyncExifTool instance. You can also do this without a logger. // ie: // await using var exiftool = new AsyncExifTool(asyncExifToolConfiguration); await using var exiftool = new AsyncExifTool(asyncExifToolConfiguration, logger); try { // initialize. At this point the exiftool process is started. // this call might throw an exception. exiftool.Initialize(); } catch (AsyncExifToolInitialisationException e) { Console.WriteLine(e); Console.WriteLine("press enter to exit"); Console.ReadKey(); return; } // Just some calls to ExifTool (using an extension method) for (int i = 0; i < 100; i++) { Console.WriteLine(await exiftool.GetVersionAsync()); } try { var result = await exiftool.ExecuteAsync("dummy command"); Console.WriteLine("dummy command result: " + result); } catch (Exception e) { Console.WriteLine("dummy command result exception: " + e.Message); } var file = @"1.jpg"; try { Console.WriteLine("Add person to existing image."); var result = await exiftool.ExecuteAsync(new[] { "-Xmp:PersonInImage+=\"Test person\"", file }); Console.WriteLine($"RESULT: {result}"); } catch (Exception e) { Console.WriteLine($"ERROR MSG: {e.Message}"); } Console.WriteLine(string.Empty); try { Console.WriteLine("Add person to NON existing image."); var result = await exiftool.ExecuteAsync(new[] { "-Xmp:PersonInImage+=\"Test person\"", file + @"xxx" }); Console.WriteLine($"RESULT: {result}"); } catch (Exception e) { Console.WriteLine($"ERROR MSG: {e.Message}"); } Console.WriteLine(string.Empty); try { Console.WriteLine("Get person information from image."); var result = await exiftool.ExecuteAsync(new[] { "-Xmp:PersonInImage", file }); Console.WriteLine($"RESULT: {result}"); } catch (Exception e) { Console.WriteLine($"ERROR MSG: {e.Message}"); } Console.WriteLine(string.Empty); // we are done Console.WriteLine("press enter to exit"); Console.ReadLine(); }