Skip to content

Commit

Permalink
Fix content hub previews of items with image asset
Browse files Browse the repository at this point in the history
Fix: Fix content hub previews of items with image asset
  • Loading branch information
akfakmot committed Nov 19, 2024
1 parent 3e359cd commit ad9a431
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using CMS.ContentEngine;
using System.Text.Json;

using CMS.ContentEngine;
using CMS.ContentEngine.Internal;
using CMS.Core;
using CMS.Core.Internal;
using CMS.DataEngine;
using CMS.FormEngine;
using CMS.Helpers;
using CMS.Membership;
using CMS.Websites;
using CMS.Websites.Internal;
Expand Down Expand Up @@ -244,7 +247,7 @@ public ContentItemInfo Adapt(IUmtModel input)
ContentItemLanguageMetadataCreatedByUserGuid = languageData.UserGuid,
ContentItemLanguageMetadataModifiedWhen = null,
ContentItemLanguageMetadataModifiedByUserGuid = languageData.UserGuid,
// ContentItemLanguageMetadataHasImageAsset = null,
ContentItemLanguageMetadataHasImageAsset = languageData.ContentItemData?.Values.Any(IsImageAsset) ?? false,
ContentItemLanguageMetadataContentLanguageGuid = contentLanguageInfo.ContentLanguageGUID,
ContentItemLanguageMetadataScheduledPublishWhen = languageData.ScheduledPublishWhen,
ContentItemLanguageMetadataScheduledUnpublishWhen = languageData.ScheduledUnpublishWhen
Expand Down Expand Up @@ -454,4 +457,24 @@ private static IEnumerable<FormFieldInfo> UnpackReusableFieldSchemas(IEnumerable
}

Guid? IInfoAdapter<IUmtModel>.GetUniqueIdOrNull(IUmtModel input) => input is ContentItemSimplifiedModel sm ? sm.ContentItemGUID : null;

private static bool IsImageAsset(object? value)
{
if (value is JsonElement { ValueKind: JsonValueKind.Object } element && element.GetProperty(AssetSource.DISCRIMINATOR_PROPERTY).GetString() is { })
{
var assetSource = element.Deserialize<AssetSource>();
if (assetSource is not null)
{
return ImageHelper.IsImage(assetSource.InferExtension());
}
else
{
throw new InvalidOperationException($"JSON object with property {AssetSource.DISCRIMINATOR_PROPERTY} cannot be deserialized");
}
}
else
{
return false;
}
}
}
8 changes: 5 additions & 3 deletions src/Kentico.Xperience.UMT/Model/Auxiliary/AssetSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ namespace Kentico.Xperience.UMT.Model;
public class AssetSource
{
public const string DISCRIMINATOR_PROPERTY = "$assetType";

[Required]
public Guid? ContentItemGuid { get; set; }

[Required]
public Guid? Identifier { get; set; }
[Required]
Expand All @@ -26,13 +24,17 @@ public class AssetSource
public string? Extension { get; set; }
public long? Size { get; set; }
public DateTime? LastModified { get; set; }

public virtual string InferExtension() => Extension ?? throw new InvalidOperationException($"{nameof(AssetFileSource)} has unknown extension. Specify explicitly by {nameof(Extension)} property");
}


public class AssetFileSource : AssetSource
{
[Required]
public string? FilePath { get; set; }

public override string InferExtension() => Extension ?? CMS.IO.FileInfo.New(FilePath).Extension ?? throw new InvalidOperationException($"{nameof(AssetFileSource)} has unknown extension. Specify explicitly by {nameof(Extension)} property");
}

public class AssetUrlSource : AssetSource
Expand All @@ -44,5 +46,5 @@ public class AssetUrlSource : AssetSource
public class AssetDataSource : AssetSource
{
[Required]
public byte[]? Data { get; set; }
public byte[]? Data { get; set; }
}
6 changes: 3 additions & 3 deletions src/Kentico.Xperience.UMT/Services/AssetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task<ContentItemAssetMetadata> SetAsset(string className, AssetSour

var assetMetadata = new ContentItemAssetMetadata
{
Extension = byteSource.Extension,
Extension = assetSource.InferExtension(),
Identifier = byteSource.Identifier.Value,
LastModified = byteSource.LastModified ?? dateTimeNowService.GetDateTimeNow(),
Name = byteSource.Name,
Expand All @@ -58,7 +58,7 @@ public async Task<ContentItemAssetMetadata> SetAsset(string className, AssetSour
var file = CMS.IO.FileInfo.New(fileSource.FilePath);
var assetMetadata = new ContentItemAssetMetadata
{
Extension = fileSource.Extension ?? file.Extension,
Extension = assetSource.InferExtension(),
Identifier = fileSource.Identifier.Value,
LastModified = fileSource.LastModified ?? dateTimeNowService.GetDateTimeNow(),
Name = fileSource.Name ?? file.Name,
Expand All @@ -77,7 +77,7 @@ public async Task<ContentItemAssetMetadata> SetAsset(string className, AssetSour

var assetMetadata = new ContentItemAssetMetadata
{
Extension = urlSource.Extension,
Extension = assetSource.InferExtension(),
Identifier = urlSource.Identifier.Value,
LastModified = urlSource.LastModified ?? dateTimeNowService.GetDateTimeNow(),
Name = urlSource.Name,
Expand Down

0 comments on commit ad9a431

Please sign in to comment.