Skip to content

Commit

Permalink
use BinaryData for thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter-Juhasz committed Jan 1, 2025
1 parent b54a04c commit e1d70ff
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
23 changes: 15 additions & 8 deletions src/PhotoArchiver/Archiver.Upload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,37 +282,44 @@ public async Task<ArchiveResult> ArchiveAsync(IReadOnlyList<IFile> files, IProgr
if (item.Info.IsJpeg() && ThumbnailOptions.IsEnabled() && (result == UploadResult.Uploaded || ThumbnailOptions.Force))
{
await using var stream = await item.OpenReadAsync(cancellationToken);
using var thumbnail = await ThumbnailGenerator.GetThumbnailAsync(stream, ThumbnailOptions.MaxWidth!.Value, ThumbnailOptions.MaxHeight!.Value, cancellationToken);
var thumbnail = await ThumbnailGenerator.GetThumbnailAsync(stream, ThumbnailOptions.MaxWidth!.Value, ThumbnailOptions.MaxHeight!.Value, cancellationToken);

var thumbnailContainer = Client.GetBlobContainerClient(ThumbnailOptions.Container);
var thumbnailBlob = thumbnailContainer.GetBlockBlobClient(blob.Name);
var headers = new BlobHttpHeaders
var thumbnailBlob = thumbnailContainer.GetBlobClient(blob.Name);
var options = new BlobUploadOptions()
{
ContentType = "image/jpeg"
HttpHeaders = new BlobHttpHeaders
{
ContentType = thumbnail.MediaType,
},
Metadata = item.Metadata
};

try
{
await thumbnailBlob.UploadAsync(thumbnail, headers, item.Metadata, cancellationToken: cancellationToken);
await thumbnailBlob.UploadAsync(thumbnail, options, cancellationToken: cancellationToken);
CostEstimator.AddWrite(thumbnail.Length);
}
catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.ContainerNotFound)
{
await thumbnailContainer.CreateIfNotExistsAsync(cancellationToken: cancellationToken);
CostEstimator.AddListOrCreateContainer();
await thumbnailBlob.UploadAsync(thumbnail.Rewind(), headers, item.Metadata, cancellationToken: cancellationToken);

await thumbnailBlob.UploadAsync(thumbnail, options, cancellationToken: cancellationToken);
CostEstimator.AddWrite(thumbnail.Length);
}
catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.BlobAlreadyExists)
{
await thumbnailBlob.DeleteIfExistsAsync(cancellationToken: cancellationToken);
await thumbnailBlob.UploadAsync(thumbnail.Rewind(), headers, item.Metadata, cancellationToken: cancellationToken);

await thumbnailBlob.UploadAsync(thumbnail, options, cancellationToken: cancellationToken);
CostEstimator.AddWrite(thumbnail.Length);
}
catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.BlobArchived && ThumbnailOptions.Force)
{
await thumbnailBlob.DeleteIfExistsAsync(cancellationToken: cancellationToken);
await thumbnailBlob.UploadAsync(thumbnail.Rewind(), headers, item.Metadata, cancellationToken: cancellationToken);

await thumbnailBlob.UploadAsync(thumbnail, options, cancellationToken: cancellationToken);
CostEstimator.AddWrite(thumbnail.Length);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/PhotoArchiver/PhotoArchiver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<PackageReference Include="Microsoft.Extensions.Http" Version="*" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="*" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.*" />
<PackageReference Include="System.Memory.Data" Version="*" />
<PackageReference Include="System.Interactive.Async" Version="*" />
<PackageReference Include="System.Text.Json" Version="*" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/PhotoArchiver/Thumbnails/IThumbnailGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface IThumbnailGenerator
{
Task<Stream> GetThumbnailAsync(Stream image, int maxWidth, int maxHeight, CancellationToken cancellationToken);
Task<BinaryData> GetThumbnailAsync(Stream image, int maxWidth, int maxHeight, CancellationToken cancellationToken);
}
8 changes: 5 additions & 3 deletions src/PhotoArchiver/Thumbnails/SixLaborsThumbnailGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public SixLaborsThumbnailGenerator(IOptions<ThumbnailOptions> options)

public JpegEncoder Encoder { get; }

public Task<Stream> GetThumbnailAsync(Stream image, int maxWidth, int maxHeight, CancellationToken cancellationToken)
public Task<BinaryData> GetThumbnailAsync(Stream image, int maxWidth, int maxHeight, CancellationToken cancellationToken)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(maxWidth);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(maxHeight);
Expand All @@ -38,8 +38,10 @@ public Task<Stream> GetThumbnailAsync(Stream image, int maxWidth, int maxHeight,

img.Mutate(x => x.Resize(width, height));
}
var buffer = new MemoryStream();
using var buffer = new MemoryStream();
img.SaveAsJpeg(buffer, Encoder);
return Task.FromResult(buffer.Rewind());
buffer.Rewind();
var result = BinaryData.FromStream(buffer, "image/jpeg");
return Task.FromResult(result);
}
}
16 changes: 3 additions & 13 deletions src/PhotoArchiver/Update/GitHubUpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,18 @@

namespace PhotoArchiver.Update;

public class GitHubUpdateService : IUpdateService
public class GitHubUpdateService(HttpClient http, IOptions<UpdateOptions> options, ILogger<GitHubUpdateService> logger) : IUpdateService
{
public GitHubUpdateService(HttpClient http, IOptions<UpdateOptions> options, ILogger<GitHubUpdateService> logger)
{
Http = http;
Options = options;
Logger = logger;
}

protected HttpClient Http { get; }
protected IOptions<UpdateOptions> Options { get; }
protected ILogger<GitHubUpdateService> Logger { get; }

private static readonly JsonSerializerOptions jsonSerializerOptions = new(JsonSerializerDefaults.Web);

public async Task<bool> CheckForUpdatesAsync(CancellationToken cancellationToken)
{
var releases = await Http.GetFromJsonAsync<ReleaseDto[]>(Options.Value.Feed, jsonSerializerOptions);
var releases = await http.GetFromJsonAsync<ReleaseDto[]>(options.Value.Feed, jsonSerializerOptions, cancellationToken: cancellationToken);
var latest = releases!.OrderByDescending(r => r.Name).First();

var currentVersion = Version.Parse(FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion!);
var latestVersion = Version.Parse(latest.Name);
logger.LogInformation("Current version: {CurrentVersion}, Latest version: {LatestVersion}", currentVersion, latestVersion);

return currentVersion < latestVersion;
}
Expand Down

0 comments on commit e1d70ff

Please sign in to comment.