Skip to content

Commit

Permalink
#1833 move out of constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Jan 23, 2025
1 parent 0a714a5 commit 4e84139
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
14 changes: 7 additions & 7 deletions starsky/starsky.foundation.writemeta/Helpers/ExifTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public sealed class ExifTool : IExifTool
private readonly AppSettings _appSettings;
private readonly IStorage _iStorage;
private readonly IWebLogger _logger;
private readonly ExifToolStreamToStreamRunner _runner;
private readonly IStorage _thumbnailStorage;

public ExifTool(IStorage sourceStorage, IStorage thumbnailStorage, AppSettings appSettings,
Expand All @@ -32,6 +33,7 @@ public ExifTool(IStorage sourceStorage, IStorage thumbnailStorage, AppSettings a
_iStorage = sourceStorage;
_thumbnailStorage = thumbnailStorage;
_logger = logger;
_runner = new ExifToolStreamToStreamRunner(_appSettings, _logger);
}

/// <summary>
Expand Down Expand Up @@ -61,14 +63,13 @@ public async Task<bool> WriteTagsAsync(string subPath, string command)
_logger.LogInformation($"[WriteTagsAsync] Next update for {subPath}");

var inputStream = _iStorage.ReadStream(subPath);

var runner = new ExifToolStreamToStreamRunner(_appSettings, inputStream, _logger);
var stream = await runner.RunProcessAsync(command, subPath);
var stream = await _runner.RunProcessAsync(inputStream, command, subPath);

// Need to Dispose for Windows
// inputStream is disposed
await inputStream.DisposeAsync();

// stream is disposed in writeStreamAsync
return await _iStorage.WriteStreamAsync(stream, subPath);
}

Expand All @@ -81,8 +82,8 @@ public async Task<bool> WriteTagsAsync(string subPath, string command)
public async Task<bool> WriteTagsThumbnailAsync(string fileHash, string command)
{
var inputStream = _thumbnailStorage.ReadStream(fileHash);
var runner = new ExifToolStreamToStreamRunner(_appSettings, inputStream, _logger);
var stream = await runner.RunProcessAsync(command, fileHash);
var stream = await _runner.RunProcessAsync(inputStream,
command, fileHash);
// Need to Close/Dispose for Windows and needs before WriteStreamAsync
await inputStream.DisposeAsync();
return await _thumbnailStorage.WriteStreamAsync(stream, fileHash);
Expand All @@ -101,8 +102,7 @@ private async Task<KeyValuePair<bool, string>>
beforeFileHash ??= await FileHash.CalculateHashAsync(sourceStream,
false, cancellationToken);

var runner = new ExifToolStreamToStreamRunner(_appSettings, sourceStream, _logger);
var stream = await runner.RunProcessAsync(command, subPath);
var stream = await _runner.RunProcessAsync(sourceStream, command, subPath);

// Need to Close / Dispose for Windows
sourceStream.Close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ public class ExifToolStreamToStreamRunner
{
private readonly AppSettings _appSettings;
private readonly IWebLogger _logger;
private readonly Stream _sourceStream;

public ExifToolStreamToStreamRunner(AppSettings appSettings, Stream sourceStream,
public ExifToolStreamToStreamRunner(AppSettings appSettings,
IWebLogger logger)
{
_sourceStream = sourceStream ?? throw new ArgumentNullException(nameof(sourceStream));
_appSettings = appSettings;
_logger = logger;
}
Expand All @@ -32,9 +30,14 @@ public ExifToolStreamToStreamRunner(AppSettings appSettings, Stream sourceStream
/// <param name="referenceInfoAndPath">reference path (only for display)</param>
/// <returns>bool if success</returns>
/// <exception cref="ArgumentException">if exifTool is missing</exception>
public async Task<Stream> RunProcessAsync(string exifToolInputArguments,
public async Task<Stream> RunProcessAsync(Stream sourceStream, string exifToolInputArguments,
string referenceInfoAndPath = "")
{
ArgumentNullException.ThrowIfNull(sourceStream);

_logger.LogInformation(
$"info: {sourceStream.CanRead} {sourceStream.CanSeek} {sourceStream.CanWrite}");

var argumentsWithPipeEnd = $"{exifToolInputArguments} -o - -";

var memoryStream = new MemoryStream();
Expand All @@ -48,7 +51,7 @@ public async Task<Stream> RunProcessAsync(string exifToolInputArguments,
opts.StartInfo(si =>
si.Arguments = argumentsWithPipeEnd);
})
< _sourceStream > memoryStream;
< sourceStream > memoryStream;

var result = await command.Task.ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ await CreateStubFile(_exifToolExePosix,
}

[TestMethod]
public void StreamToStreamRunner_ArgumentNullException()
public async Task StreamToStreamRunner_ArgumentNullException()
{
Assert.ThrowsException<ArgumentNullException>(() =>
new ExifToolStreamToStreamRunner(new AppSettings(), null!,
new FakeIWebLogger()));
await Assert.ThrowsExceptionAsync<ArgumentNullException>(async () =>
await new ExifToolStreamToStreamRunner(new AppSettings(),
new FakeIWebLogger()).RunProcessAsync(null!, "test / unit test"));
}

[TestMethod]
Expand All @@ -83,9 +83,8 @@ public async Task RunProcessAsync_RunChildObject_UnixOnly()
return;
}

var runner = new ExifToolStreamToStreamRunner(appSettings,
new MemoryStream([]), new FakeIWebLogger());
var streamResult = await runner.RunProcessAsync(
var runner = new ExifToolStreamToStreamRunner(appSettings, new FakeIWebLogger());
var streamResult = await runner.RunProcessAsync(new MemoryStream([]),
string.Empty, "test / unit test");

await StreamToStringHelper.StreamToStringAsync(streamResult, false);
Expand All @@ -112,8 +111,8 @@ public async Task ExifTool_RunProcessAsync_Fuzzing(string argument)
}

var runner = new ExifToolStreamToStreamRunner(appSettings,
new MemoryStream([]), new FakeIWebLogger());
var streamResult = await runner.RunProcessAsync(
new FakeIWebLogger());
var streamResult = await runner.RunProcessAsync(new MemoryStream([]),
argument, "test / unit test");

var stringResult = await StreamToStringHelper.StreamToStringAsync(streamResult);
Expand All @@ -129,10 +128,10 @@ public async Task ExifTool_RunProcessAsync_HappyFlow()

var sourceStream = new MemoryStream([0x01, 0x02, 0x03]);

var sut = new ExifToolStreamToStreamRunner(_appSettingsWithExifTool, sourceStream,
var sut = new ExifToolStreamToStreamRunner(_appSettingsWithExifTool,
new FakeIWebLogger());

var stream = await sut.RunProcessAsync("arg1",
var stream = await sut.RunProcessAsync(sourceStream, "arg1",
"reference");

await sourceStream.DisposeAsync();
Expand All @@ -158,10 +157,10 @@ public async Task ExifTool_RunProcessAsync_ExitCode()
await CreateStubFile(_exifToolExePosix,
"#!/bin/bash\ntest_content\n exit 1");

var sut = new ExifToolStreamToStreamRunner(_appSettingsWithExifTool, sourceStream,
var sut = new ExifToolStreamToStreamRunner(_appSettingsWithExifTool,
new FakeIWebLogger());

var stream = await sut.RunProcessAsync("arg1",
var stream = await sut.RunProcessAsync(sourceStream, "arg1",
"reference");

Assert.IsNotNull(stream);
Expand All @@ -177,11 +176,10 @@ public async Task ExifTool_RunProcessAsync_WithInvalidFfmpegPath()
var sourceStream = new MemoryStream([0x01, 0x02, 0x03]);

var sut = new ExifToolStreamToStreamRunner(new AppSettings { ExifToolPath = "invalid" },
sourceStream,
new FakeIWebLogger());

await Assert.ThrowsExceptionAsync<ArgumentException>(async () =>
await sut.RunProcessAsync("-1",
await sut.RunProcessAsync(sourceStream, "-1",
"image2"));

await sourceStream.DisposeAsync();
Expand All @@ -191,10 +189,10 @@ await sut.RunProcessAsync("-1",
public async Task ExifTool_RunProcessAsync_WithInvalidPath_WithInvalidStream()
{
var sut = new ExifToolStreamToStreamRunner(new AppSettings { ExifToolPath = "invalid" },
Stream.Null,
new FakeIWebLogger());

await Assert.ThrowsExceptionAsync<ArgumentException>(async () =>
await sut.RunProcessAsync("-1", "image2"));
await sut.RunProcessAsync(Stream.Null,
"-1", "image2"));
}
}

0 comments on commit 4e84139

Please sign in to comment.