Skip to content

Commit

Permalink
Merge pull request #414 from dlcs/feature/thumb_fix
Browse files Browse the repository at this point in the history
Update thumbReorganiser to handle thumbnailPolicy larger than source
  • Loading branch information
donaldgray authored Nov 21, 2022
2 parents 33f8c88 + 4665368 commit 423bf72
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
58 changes: 58 additions & 0 deletions DLCS.Repository.Tests/Assets/ThumbReorganiserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,64 @@ public async Task EnsureNewLayout_CreatesExpectedResources_MixedAuthAndOpen()
"application/json"))
.MustHaveHappened();
}

[Fact]
public async Task EnsureNewLayout_CreatesExpectedResources_MixedAuthAndOpen_ImageSmallerThanThumbnail()
{
var rootKey = new ObjectInBucket {Bucket = "the-bucket", Key = "2/1/the-astronaut/"};
A.CallTo(() => bucketReader.GetMatchingKeys(rootKey))
.Returns(new[]
{
"2/1/the-astronaut/full/200,/0/default.jpg",
"2/1/the-astronaut/full/200,400/0/default.jpg",
"2/1/the-astronaut/full/100,/0/default.jpg",
"2/1/the-astronaut/full/100,200/0/default.jpg",
"2/1/the-astronaut/full/50,/0/default.jpg",
"2/1/the-astronaut/full/50,100/0/default.jpg"
});

A.CallTo(() => assetRepository.GetAsset(A<string>._))
.Returns(new Asset {Width = 300, Height = 600, ThumbnailPolicy = "TheBestOne", MaxUnauthorised = 350, Roles = "admin"});
A.CallTo(() => thumbPolicyRepository.GetThumbnailPolicy("TheBestOne"))
.Returns(new ThumbnailPolicy {Sizes = "1024,400,200,100"});

// Act
var response = await sut.EnsureNewLayout(rootKey);

// Assert
response.Should().Be(ReorganiseResult.Reorganised);

// move jpg per thumbnail size
A.CallTo(() =>
bucketReader.CopyWithinBucket("the-bucket",
"2/1/the-astronaut/low.jpg",
"2/1/the-astronaut/auth/600.jpg"))
.MustHaveHappened();
A.CallTo(() =>
bucketReader.CopyWithinBucket("the-bucket",
"2/1/the-astronaut/full/200,400/0/default.jpg",
"2/1/the-astronaut/auth/400.jpg"))
.MustHaveHappened();
A.CallTo(() =>
bucketReader.CopyWithinBucket("the-bucket",
"2/1/the-astronaut/full/100,200/0/default.jpg",
"2/1/the-astronaut/open/200.jpg"))
.MustHaveHappened();
A.CallTo(() =>
bucketReader.CopyWithinBucket("the-bucket",
"2/1/the-astronaut/full/50,100/0/default.jpg",
"2/1/the-astronaut/open/100.jpg"))
.MustHaveHappened();

// create sizes.json
const string expected = "{\"o\":[[100,200],[50,100]],\"a\":[[300,600],[200,400]]}";
A.CallTo(() =>
bucketReader.WriteToBucket(
A<ObjectInBucket>.That.Matches(o =>
o.Bucket == "the-bucket" && o.Key == "2/1/the-astronaut/s.json"), expected,
"application/json"))
.MustHaveHappened();
}

[Fact]
public async Task EnsureNewLayout_CreatesExpectedResources_HandlingRoundingDifference_Portrait()
Expand Down
24 changes: 13 additions & 11 deletions DLCS.Repository/Assets/ThumbReorganiser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public async Task<ReorganiseResult> EnsureNewLayout(ObjectInBucket rootKey)
}

// All the thumbnail jpgs will already exist and need copied up to root
await CreateThumbnails(rootKey, boundingSquares, thumbnailSizes, existingSizes);
await CreateThumbnails(rootKey, thumbnailSizes, existingSizes);

// Create sizes json file last, as this dictates whether this process will be attempted again
await CreateSizesJson(rootKey, thumbnailSizes);
Expand Down Expand Up @@ -125,32 +125,34 @@ private static List<Size> GetExistingSizesList(ThumbnailSizes thumbnailSizes, st
return existingSizes;
}

private async Task CreateThumbnails(ObjectInBucket rootKey, List<int> boundingSquares,
ThumbnailSizes thumbnailSizes, List<Size> existingSizes)
private async Task CreateThumbnails(ObjectInBucket rootKey, ThumbnailSizes thumbnailSizes,
List<Size> existingSizes)
{
var copyTasks = new List<Task>(thumbnailSizes.Count);

// low.jpg becomes the first in this list
var largestSize = boundingSquares[0];
var openSizes = thumbnailSizes.Open.Select(wh => Size.FromArray(wh)).ToList();
var authSizes = thumbnailSizes.Auth.Select(wh => Size.FromArray(wh)).ToList();
var largestSize = openSizes.Concat(authSizes).Max(sz => sz.MaxDimension);

// low.jpg becomes the largest sized thumb
var largestSlug = thumbnailSizes.Auth.IsNullOrEmpty() ? thumbConsts.OpenSlug : thumbConsts.AuthorisedSlug;
copyTasks.Add(bucketReader.CopyWithinBucket(rootKey.Bucket,
$"{rootKey.Key}low.jpg",
$"{rootKey.Key}{largestSlug}/{largestSize}.jpg"));

copyTasks.AddRange(ProcessThumbBatch(rootKey, thumbnailSizes.Auth, thumbConsts.AuthorisedSlug, largestSize,
copyTasks.AddRange(ProcessThumbBatch(rootKey, authSizes, thumbConsts.AuthorisedSlug, largestSize,
existingSizes));
copyTasks.AddRange(ProcessThumbBatch(rootKey, thumbnailSizes.Open, thumbConsts.OpenSlug, largestSize,
copyTasks.AddRange(ProcessThumbBatch(rootKey, openSizes, thumbConsts.OpenSlug, largestSize,
existingSizes));

await Task.WhenAll(copyTasks);
}

private IEnumerable<Task> ProcessThumbBatch(ObjectInBucket rootKey, IEnumerable<int[]> thumbnailSizes,
string slug, int largestSize, List<Size> existingSizes)
private IEnumerable<Task> ProcessThumbBatch(ObjectInBucket rootKey, IEnumerable<Size> thumbnailSizes,
string slug, int largestSize, IReadOnlyCollection<Size> existingSizes)
{
foreach (var wh in thumbnailSizes)
foreach (var currentSize in thumbnailSizes)
{
var currentSize = Size.FromArray(wh);
var maxDimension = currentSize.MaxDimension;
if (maxDimension == largestSize) continue;

Expand Down

0 comments on commit 423bf72

Please sign in to comment.