Skip to content

Commit b10448c

Browse files
committed
Updates to media with crops, rendering component, authorization service and bug fixing
1 parent d69bdc6 commit b10448c

File tree

12 files changed

+158
-82
lines changed

12 files changed

+158
-82
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Umbraco.Cms.Integrations.DAM.Aprimo.Models.ViewModels.AprimoAssetViewModel>
22

3+
<!-- Thumbnail -->
34
<div class="aprimo-asset-row">
45
@if (!string.IsNullOrEmpty(Model.Thumbnail))
56
{
@@ -9,22 +10,36 @@
910
<h3>@Model.Title</h3>
1011
</div>
1112
</div>
12-
@if (Model.Crops.Any())
13+
<!-- Crops -->
14+
@if (Model.MediaWithCrops != null)
1315
{
1416
<div class="aprimo-asset-row">
15-
<h3>Crop Items</h3>
16-
<div class="aprimo-crops-row">
17-
@foreach (var cropItem in Model.Crops)
17+
<h3>Cropped images</h3>
18+
@if (Model.MediaWithCrops.Original != null)
19+
{
20+
<div class="column">
21+
<h3>@Model.MediaWithCrops.Original.Name</h3>
22+
@if (!string.IsNullOrEmpty(Model.MediaWithCrops.Original.Url))
23+
{
24+
<img src="@Model.MediaWithCrops.Original.Url" class="thumbnail" alt="@Model.MediaWithCrops.Original.Name" />
25+
}
26+
</div>
27+
}
28+
@if (Model.MediaWithCrops.Crops.Any())
29+
{
30+
@foreach (var cropItem in Model.MediaWithCrops.Crops)
1831
{
1932
<div class="column">
20-
@if (!string.IsNullOrEmpty(cropItem.PublicLink))
33+
@if (!string.IsNullOrEmpty(cropItem.Url))
2134
{
22-
<img src="@cropItem.PublicLink" class="thumbnail" alt="@cropItem.Label" />
35+
<img src="@cropItem.Url" class="thumbnail" alt="@cropItem.Name" />
2336
}
24-
<p><b>Label:</b> @cropItem.Label</p>
25-
<p><b>Size:</b> @cropItem.ResizeWidth x @cropItem.ResizeHeight</p>
37+
<h3>@cropItem.Name</h3>
38+
<p>
39+
@cropItem.Extension - @($"{cropItem.ResizeWidth} x {cropItem.ResizeHeight} px")
40+
</p>
2641
</div>
2742
}
28-
</div>
43+
}
2944
</div>
3045
}

src/Umbraco.Cms.Integrations.DAM.Aprimo/Controllers/AprimoAuthorizationController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public async Task<IActionResult> OAuth(string code)
2929
};
3030
}
3131

32-
var response = await _aprimoAuthorizationService.GetAccessToken(code);
32+
var response = await _aprimoAuthorizationService.GetAccessTokenAsync(code);
3333

3434
return new ContentResult
3535
{

src/Umbraco.Cms.Integrations.DAM.Aprimo/Controllers/AssetsController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public async Task<IActionResult> CheckApiConfiguration()
5757
var result = await _assetsService.GetRecordByIdAsync(Guid.NewGuid());
5858
if (!result.IsAuthorized)
5959
{
60-
await _authorizationService.RefreshAccessToken();
60+
await _authorizationService.RefreshAccessTokenAsync();
6161

6262
var updatedResult = await _assetsService.GetRecordByIdAsync(Guid.NewGuid());
6363

@@ -105,7 +105,7 @@ public string GetContentSelectorUrl()
105105

106106
[HttpPost]
107107
public async Task<string> GetAccessToken([FromBody] OAuthRequest request) =>
108-
await _authorizationService.GetAccessToken(request.Code);
108+
await _authorizationService.GetAccessTokenAsync(request.Code);
109109

110110
[HttpGet]
111111
public async Task<IActionResult> GetRecordDetails(string id)

src/Umbraco.Cms.Integrations.DAM.Aprimo/Editors/AssetPickerValueConverter.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public override object ConvertSourceToIntermediate(IPublishedElement owner, IPub
5353
var response = _assetsService.GetRecordById(Guid.Parse(vm.Id));
5454
if(!response.IsAuthorized)
5555
{
56-
var tokenResponse = _authorizationService.RefreshAccessToken()
57-
.ConfigureAwait(false).GetAwaiter().GetResult();
56+
var tokenResponse = _authorizationService.RefreshAccessToken();
5857
if (string.IsNullOrEmpty(tokenResponse))
5958
{
6059
response = _assetsService.GetRecordById(Guid.Parse(vm.Id));
@@ -81,12 +80,19 @@ private void ToViewModel(Record record, ref AprimoAssetViewModel vm)
8180
vm.Thumbnail = record.Thumbnail.Uri;
8281

8382
if(record.MasterFileLatestVersion != null
84-
&& record.MasterFileLatestVersion.AdditionalFiles!= null
85-
&& record.MasterFileLatestVersion.AdditionalFiles.Items != null)
83+
&& record.MasterFileLatestVersion.Renditions!= null
84+
&& record.MasterFileLatestVersion.Renditions.Items != null)
8685
{
87-
vm.Crops = record.MasterFileLatestVersion.AdditionalFiles.Items
86+
var originalItem = record.MasterFileLatestVersion.Renditions.Items
87+
.FirstOrDefault(p => p.Type == "Original");
88+
if(originalItem != null)
89+
{
90+
vm.MediaWithCrops.Original = originalItem.ToAprimoMediaItemViewModel();
91+
}
92+
93+
vm.MediaWithCrops.Crops = record.MasterFileLatestVersion.Renditions.Items
8894
.Where(p => p.Type == "Crop")
89-
.Select(p => p.ToAprimoCropItemViewModel());
95+
.Select(p => p.ToAprimoMediaItemViewModel());
9096
}
9197

9298
if(record.Fields != null

src/Umbraco.Cms.Integrations.DAM.Aprimo/Helpers/RecordHelper.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@ namespace Umbraco.Cms.Integrations.DAM.Aprimo.Helpers
55
{
66
public static class RecordHelper
77
{
8-
public static AprimoCropViewModel ToAprimoCropItemViewModel(this RecordFileItem item) => new(item.X, item.Y,
9-
item.Width, item.Height,
10-
item.ResizeWidth, item.ResizeHeight,
11-
item.PresetName, item.Label, item.FileName,
12-
item.PublicLink);
8+
public static AprimoMediaItemViewModel ToAprimoMediaItemViewModel(this RecordRenditionItem item)
9+
{
10+
string url = string.Empty;
11+
12+
if (item.PublicLinks != null && item.PublicLinks.Items != null && item.PublicLinks.Items.Any())
13+
url = item.PublicLinks.Items.First().Uri;
14+
15+
return new(item.ResizeWidth, item.ResizeHeight,
16+
item.Name, item.PresetName,
17+
item.Label, item.FileName,
18+
item.Extension, url);
19+
}
1320
}
1421
}

src/Umbraco.Cms.Integrations.DAM.Aprimo/Models/Record.cs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ public class RecordThumbnail
4040

4141
public class RecordMasterFile
4242
{
43-
[JsonPropertyName("additionalFiles")]
44-
public RecordAdditionalFiles AdditionalFiles { get; set; }
43+
[JsonPropertyName("renditions")]
44+
public RecordRenditions Renditions { get; set; }
4545
}
4646

47-
public class RecordAdditionalFiles
47+
public class RecordRenditions
4848
{
4949
[JsonPropertyName("items")]
50-
public IEnumerable<RecordFileItem> Items { get; set; }
50+
public IEnumerable<RecordRenditionItem> Items { get; set; }
5151
}
5252

53-
public class RecordFileItem
53+
public class RecordRenditionItem
5454
{
5555
[JsonPropertyName("x")]
5656
public int X { get; set; }
@@ -70,6 +70,9 @@ public class RecordFileItem
7070
[JsonPropertyName("resizeHeight")]
7171
public int ResizeHeight { get; set; }
7272

73+
[JsonPropertyName("name")]
74+
public string Name { get; set; }
75+
7376
[JsonPropertyName("presetName")]
7477
public string PresetName { get; set; }
7578

@@ -82,8 +85,11 @@ public class RecordFileItem
8285
[JsonPropertyName("type")]
8386
public string Type { get; set; }
8487

85-
[JsonPropertyName("publicLink")]
86-
public string PublicLink { get; set; }
88+
[JsonPropertyName("extension")]
89+
public string Extension { get; set; }
90+
91+
[JsonPropertyName("publicLinks")]
92+
public RenditionPublicLinks PublicLinks { get; set; } = new RenditionPublicLinks();
8793
}
8894

8995
public class RecordFields
@@ -113,4 +119,21 @@ public class RecordFieldItemValue
113119
public string[] Values { get; set; }
114120
}
115121

122+
public class RenditionPublicLinks
123+
{
124+
public RenditionPublicLinks()
125+
{
126+
Items = Enumerable.Empty<PublicLinkItem>();
127+
}
128+
129+
[JsonPropertyName("items")]
130+
public IEnumerable<PublicLinkItem> Items { get; set; }
131+
}
132+
133+
public class PublicLinkItem
134+
{
135+
[JsonPropertyName("uri")]
136+
public string Uri { get; set; }
137+
}
138+
116139
}

src/Umbraco.Cms.Integrations.DAM.Aprimo/Models/ViewModels/AprimoAssetViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ public class AprimoAssetViewModel
99

1010
public string Thumbnail { get; set; } = string.Empty;
1111

12-
public IEnumerable<AprimoCropViewModel> Crops { get; set; }
12+
public AprimoMediaWithCropsViewModel MediaWithCrops { get; set; }
1313

1414
public List<AprimoFieldViewModel> Fields { get; set; }
1515

1616
public AprimoAssetViewModel()
1717
{
18-
Crops = Enumerable.Empty<AprimoCropViewModel>();
18+
MediaWithCrops = new AprimoMediaWithCropsViewModel();
1919
Fields = new List<AprimoFieldViewModel>();
2020
}
2121
}

src/Umbraco.Cms.Integrations.DAM.Aprimo/Models/ViewModels/AprimoCropViewModel.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+

2+
namespace Umbraco.Cms.Integrations.DAM.Aprimo.Models.ViewModels
3+
{
4+
public class AprimoMediaWithCropsViewModel
5+
{
6+
public AprimoMediaItemViewModel Original { get; set; }
7+
8+
public IEnumerable<AprimoMediaItemViewModel> Crops { get; set; }
9+
10+
/// <summary>
11+
/// Get image URL by crop name
12+
/// </summary>
13+
/// <param name="name"></param>
14+
/// <returns>Crop URL</returns>
15+
public string GetImageUrl(string name)
16+
{
17+
var crop = Crops.FirstOrDefault(p => p.Name == name);
18+
if (crop != null)
19+
return crop.Url;
20+
21+
return string.Empty;
22+
}
23+
}
24+
25+
public class AprimoMediaItemViewModel
26+
{
27+
public int ResizeWidth { get; set; }
28+
29+
public int ResizeHeight { get; set; }
30+
31+
public string Name { get; set; }
32+
33+
public string PresetName { get; set; }
34+
35+
public string Label { get; set; }
36+
37+
public string FileName { get; set; }
38+
39+
public string Extension { get; set; }
40+
41+
public string Url { get; set; }
42+
43+
public AprimoMediaItemViewModel(
44+
int resizeWidth, int resizeHeight,
45+
string name, string presetName,
46+
string label, string fileName,
47+
string extension, string url)
48+
{
49+
ResizeWidth = resizeWidth;
50+
ResizeHeight = resizeHeight;
51+
Name = name;
52+
PresetName = presetName;
53+
Label = label;
54+
FileName = fileName;
55+
Extension = extension;
56+
Url = url;
57+
}
58+
}
59+
}

src/Umbraco.Cms.Integrations.DAM.Aprimo/Services/AprimoAuthorizationService.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ public string GetAuthorizationUrl(OAuthCodeExchange oauthCodeExchange)
5151
oauthCodeExchange.CodeChallenge);
5252
}
5353

54-
public async Task<string> GetAccessToken(string code)
54+
public string GetAccessToken(string code) => GetAccessTokenAsync(code).ConfigureAwait(false).GetAwaiter().GetResult();
55+
56+
public async Task<string> GetAccessTokenAsync(string code)
5557
{
5658
var configurationEntity = _storage.Get();
5759
if (configurationEntity == null) return "Error: " + Constants.ErrorResources.InvalidCodeChallenge;
@@ -88,7 +90,9 @@ public async Task<string> GetAccessToken(string code)
8890
return "Error: " + content;
8991
}
9092

91-
public async Task<string> RefreshAccessToken()
93+
public string RefreshAccessToken() => RefreshAccessTokenAsync().ConfigureAwait(false).GetAwaiter().GetResult();
94+
95+
public async Task<string> RefreshAccessTokenAsync()
9296
{
9397
var configurationEntity = _storage.Get();
9498
if (configurationEntity == null) return Constants.ErrorResources.MissingRefreshToken;
@@ -115,6 +119,8 @@ public async Task<string> RefreshAccessToken()
115119
configurationEntity.RefreshToken = data.RefreshToken;
116120

117121
_storage.AddOrUpdate(configurationEntity);
122+
123+
return string.Empty;
118124
}
119125

120126
return "Error: " + content;

0 commit comments

Comments
 (0)