Skip to content

Commit

Permalink
update to add thumb creator work
Browse files Browse the repository at this point in the history
  • Loading branch information
JackLewis-digirati committed Apr 11, 2024
1 parent 64247d4 commit 3800b08
Show file tree
Hide file tree
Showing 16 changed files with 208 additions and 113 deletions.
6 changes: 6 additions & 0 deletions src/protagonist/DLCS.Model/Assets/Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using DLCS.Core.Collections;
using DLCS.Core.Types;
using DLCS.Model.Assets.Metadata;

namespace DLCS.Model.Assets;

Expand Down Expand Up @@ -99,6 +100,11 @@ public IEnumerable<string> TagsList
/// A list of image delivery channels attached to this asset
/// </summary>
public ICollection<ImageDeliveryChannel> ImageDeliveryChannels { get; set; }

/// <summary>
/// A list of metadata attached to this asset
/// </summary>
public ICollection<AssetApplicationMetadata> AssetApplicationMetadata { get; set; }

public Asset()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using System.Collections.Generic;
using DLCS.Core.Types;

namespace DLCS.Model.Assets;
namespace DLCS.Model.Assets.Metadata;

public class AssetApplicationMetadata
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Dynamic;

namespace DLCS.Model.Assets.Metadata;

public static class AssetApplicationMetadataKeys
{
public const string ThumbnailPolicy = "ThumbnailPolicy";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using DLCS.Core.Types;

namespace DLCS.Model.Assets.Metadata;

public interface IAssetApplicationMetadataRepository
{
public Task<List<int[]>> GetThumbnailSizes(AssetId assetId);

public Task<AssetApplicationMetadata> AddApplicationMetadata(
AssetApplicationMetadata metadata,
CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using DLCS.Core.Types;
using DLCS.Model.Assets.Metadata;

namespace DLCS.Repository.Assets;

public class AssetApplicationMetadataRepository : IAssetApplicationMetadataRepository
{
private readonly DlcsContext dlcsContext;

public AssetApplicationMetadataRepository(DlcsContext dlcsContext)
{
this.dlcsContext = dlcsContext;
}

public async Task<List<int[]>> GetThumbnailSizes(AssetId assetId)
{


return new List<int[]>();
}

public async Task<AssetApplicationMetadata> AddApplicationMetadata(
AssetApplicationMetadata metadata,
CancellationToken cancellationToken = default)
{
var databaseMetadata= await dlcsContext.AssetApplicationMetadata.AddAsync(metadata, cancellationToken);
await dlcsContext.SaveChangesAsync(cancellationToken);

return databaseMetadata.Entity;
}
}
19 changes: 17 additions & 2 deletions src/protagonist/DLCS.Repository/Assets/Thumbs/ThumbsManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Threading.Tasks;
using DLCS.AWS.S3;
using DLCS.Core.Types;
using DLCS.Model.Assets;
using DLCS.Model.Assets.Metadata;
using DLCS.Model.Policies;
using IIIF;
using Newtonsoft.Json;
Expand All @@ -15,14 +17,17 @@ public abstract class ThumbsManager
{
protected readonly IBucketWriter BucketWriter;
protected readonly IStorageKeyGenerator StorageKeyGenerator;
protected readonly IAssetApplicationMetadataRepository AssetApplicationMetadataRepository;

public ThumbsManager(
IBucketWriter bucketWriter,
IStorageKeyGenerator storageKeyGenerator
IStorageKeyGenerator storageKeyGenerator,
IAssetApplicationMetadataRepository assetApplicationMetadataRepository
)
{
BucketWriter = bucketWriter;
StorageKeyGenerator = storageKeyGenerator;
AssetApplicationMetadataRepository = assetApplicationMetadataRepository;
}

protected static Size GetMaxAvailableThumb(Asset asset, ThumbnailPolicy policy)
Expand All @@ -33,8 +38,18 @@ protected static Size GetMaxAvailableThumb(Asset asset, ThumbnailPolicy policy)

protected async Task CreateSizesJson(AssetId assetId, ThumbnailSizes thumbnailSizes)
{
var serializedThumbnailSizes = JsonConvert.SerializeObject(thumbnailSizes);
var sizesDest = StorageKeyGenerator.GetThumbsSizesJsonLocation(assetId);
await BucketWriter.WriteToBucket(sizesDest, JsonConvert.SerializeObject(thumbnailSizes),
await BucketWriter.WriteToBucket(sizesDest, serializedThumbnailSizes,
"application/json");
await AssetApplicationMetadataRepository.AddApplicationMetadata(new AssetApplicationMetadata()
{
ImageId = assetId,
MetadataType = AssetApplicationMetadataKeys.ThumbnailPolicy,
MetadataValue = serializedThumbnailSizes,
Created = DateTime.UtcNow,
Modified = DateTime.UtcNow
});

}
}
6 changes: 6 additions & 0 deletions src/protagonist/DLCS.Repository/DlcsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using DLCS.Core.Types;
using DLCS.Model.Assets;
using DLCS.Model.Assets.CustomHeaders;
using DLCS.Model.Assets.Metadata;
using DLCS.Model.Assets.NamedQueries;
using DLCS.Model.Auth.Entities;
using DLCS.Model.Customers;
Expand Down Expand Up @@ -683,11 +684,16 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

modelBuilder.Entity<AssetApplicationMetadata>(entity =>
{
entity.Property(e => e.Id).HasMaxLength(100);
entity.Property(e => e.ImageId).IsRequired().HasConversion(
aId => aId.ToString(),
id => AssetId.FromString(id));
entity.Property(e => e.MetadataType).IsRequired();
entity.Property(e => e.MetadataValue).IsRequired().HasColumnType("jsonb");
entity.HasOne(e => e.Asset)
.WithMany(e => e.AssetApplicationMetadata)
.HasForeignKey(e => e.ImageId);
});

OnModelCreatingPartial(modelBuilder);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ protected override void Up(MigrationBuilder migrationBuilder)
name: "AssetApplicationMetadata",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
Id = table.Column<int>(type: "integer", maxLength: 100, nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
ImageId = table.Column<string>(type: "text", nullable: false),
AssetId = table.Column<string>(type: "character varying(500)", nullable: false),
ImageId = table.Column<string>(type: "character varying(500)", nullable: false),
MetadataType = table.Column<string>(type: "text", nullable: false),
MetadataValue = table.Column<string>(type: "jsonb", nullable: false),
Created = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
Expand All @@ -27,17 +26,17 @@ protected override void Up(MigrationBuilder migrationBuilder)
{
table.PrimaryKey("PK_AssetApplicationMetadata", x => x.Id);
table.ForeignKey(
name: "FK_AssetApplicationMetadata_Images_AssetId",
column: x => x.AssetId,
name: "FK_AssetApplicationMetadata_Images_ImageId",
column: x => x.ImageId,
principalTable: "Images",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});

migrationBuilder.CreateIndex(
name: "IX_AssetApplicationMetadata_AssetId",
name: "IX_AssetApplicationMetadata_ImageId",
table: "AssetApplicationMetadata",
column: "AssetId");
column: "ImageId");
}

protected override void Down(MigrationBuilder migrationBuilder)
Expand Down
Loading

0 comments on commit 3800b08

Please sign in to comment.