Skip to content

Commit 522e9f7

Browse files
Add support for ListResourcesByAssetFolder Admin API
Add support for `AssetFolder`, `DisplayName`, `UniqueDisplayName` and `UseAssetFolderAsPublicIdPrefix` parameters
1 parent 4f341ea commit 522e9f7

File tree

15 files changed

+316
-7
lines changed

15 files changed

+316
-7
lines changed

CloudinaryDotNet.IntegrationTests/AdminApi/BrowseResourcesTest.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,42 @@ public void TestListResourcesByAssetIds()
339339
Assert.AreEqual(publicId2, result.Resources[1].PublicId);
340340
}
341341

342+
[Test, IgnoreFeature("dynamic_folders"), RetryWithDelay]
343+
public void TestListResourcesByAssetFolder()
344+
{
345+
var publicId1 = GetUniquePublicId();
346+
var publicId2 = GetUniquePublicId();
347+
348+
var assetFolder = GetUniqueFolder();
349+
350+
var uploadParams = new ImageUploadParams()
351+
{
352+
File = new FileDescription(m_testImagePath),
353+
PublicId = publicId1,
354+
AssetFolder = assetFolder,
355+
Tags = m_apiTag
356+
};
357+
358+
m_cloudinary.Upload(uploadParams);
359+
360+
uploadParams = new ImageUploadParams()
361+
{
362+
File = new FileDescription(m_testImagePath),
363+
PublicId = publicId2,
364+
AssetFolder = assetFolder,
365+
Tags = m_apiTag
366+
};
367+
368+
m_cloudinary.Upload(uploadParams);
369+
370+
var result = m_cloudinary.ListResourcesByAssetFolder(assetFolder);
371+
372+
Assert.AreEqual(2, result.Resources.Length);
373+
374+
Assert.AreEqual(publicId1, result.Resources[1].PublicId);
375+
Assert.AreEqual(publicId2, result.Resources[0].PublicId);
376+
}
377+
342378
[Test, RetryWithDelay]
343379
public void TestListResourcesByTag()
344380
{

CloudinaryDotNet.IntegrationTests/AdminApi/UpdateResourcesTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,37 @@ public void TestManualModeration()
268268
Assert.AreEqual(ModerationStatus.Approved, updateResult.Moderation[0].Status);
269269
}
270270

271+
[Test, IgnoreFeature("dynamic_folders"), RetryWithDelay]
272+
public void TestUpdateDynamicFolderAttributes()
273+
{
274+
//should update quality
275+
var publicId = GetUniquePublicId();
276+
var assetFolder = GetUniqueFolder();
277+
var displayName = GetUniquePublicId();
278+
var upResult = m_cloudinary.Upload(new ImageUploadParams
279+
{
280+
File = new FileDescription(m_testImagePath),
281+
PublicId = publicId,
282+
Tags = m_apiTag
283+
}
284+
);
285+
286+
var updResult = m_cloudinary.UpdateResource(
287+
new UpdateParams(upResult.PublicId)
288+
{
289+
AssetFolder = assetFolder,
290+
DisplayName = displayName,
291+
UniqueDisplayName = true
292+
293+
});
294+
Assert.AreEqual(updResult.StatusCode, HttpStatusCode.OK, updResult.Error?.Message);
295+
Assert.Null(updResult.Error);
296+
297+
Assert.AreEqual(updResult.PublicId, publicId);
298+
Assert.AreEqual(assetFolder, updResult.AssetFolder);
299+
Assert.AreEqual(displayName, updResult.DisplayName);
300+
}
301+
271302
[Test, RetryWithDelay]
272303
public void TestUpdateResourceMetadata()
273304
{

CloudinaryDotNet.IntegrationTests/UploadApi/RenameMethodsTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ public async Task TestRenameReturnsMetadata()
118118
Assert.IsNull(renameResult.MetadataFields);
119119
}
120120

121+
[Test, IgnoreFeature("dynamic_folders")]
122+
public async Task TestRenameWithDynamicFolderAttributesInResult()
123+
{
124+
var publicId = GetUniquePublicId();
125+
var newPublicId = GetUniquePublicId();
126+
127+
await UploadImage(publicId);
128+
129+
var @params = new RenameParams(publicId, newPublicId);
130+
var renameResult = m_cloudinary.Rename(@params);
131+
132+
Assert.NotNull(renameResult.AssetFolder);
133+
Assert.NotNull(renameResult.DisplayName);
134+
}
135+
121136
private async Task UploadImage(string publicId, bool withMetadata = false)
122137
{
123138
if (withMetadata)

CloudinaryDotNet.IntegrationTests/UploadApi/UploadMethodsTest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,9 +893,12 @@ public void TestUploadAndGetResource()
893893
{
894894
File = new FileDescription(m_testImagePath),
895895
CustomCoordinates = coordinates,
896-
Tags = m_apiTag
896+
Tags = m_apiTag,
897+
DisplayName = GetUniquePublicId()
897898
});
898899

900+
Assert.IsNotEmpty(upResult.DisplayName);
901+
899902
var result = m_cloudinary.GetResource(new GetResourceParams(upResult.PublicId)
900903
{
901904
Prefix = m_test_prefix,
@@ -909,6 +912,7 @@ public void TestUploadAndGetResource()
909912

910913
Assert.NotNull(result.NextCursor, result.Error?.Message);
911914
Assert.NotZero(result.Tags.Length);
915+
Assert.IsNotEmpty(result.DisplayName);
912916
}
913917

914918
[Test, RetryWithDelay]

CloudinaryDotNet.Tests/Parameters/ParametersTest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ public void TestUploadDynamicFoldersParamsCheck()
184184
DisplayName = "test",
185185
UseFilenameAsDisplayName = true,
186186
AssetFolder = "asset_folder",
187+
UseAssetFolderAsPublicIdPrefix = true,
187188
Folder = "folder"
188189
};
189190

@@ -194,6 +195,7 @@ public void TestUploadDynamicFoldersParamsCheck()
194195
Assert.AreEqual("test", dictionary["display_name"]);
195196
Assert.AreEqual("true", dictionary["use_filename_as_display_name"]);
196197
Assert.AreEqual("asset_folder", dictionary["asset_folder"]);
198+
Assert.AreEqual("true", dictionary["use_asset_folder_as_public_id_prefix"]);
197199
Assert.AreEqual("folder", dictionary["folder"]);
198200
}
199201

@@ -205,7 +207,8 @@ public void TestExplicitDynamicFoldersParamsCheck()
205207
PublicIdPrefix = "fd_public_id_prefix",
206208
DisplayName = "test",
207209
UseFilenameAsDisplayName = true,
208-
AssetFolder = "asset_folder"
210+
AssetFolder = "asset_folder",
211+
UseAssetFolderAsPublicIdPrefix = true,
209212
};
210213

211214
var dictionary = parameters.ToParamsDictionary();
@@ -214,6 +217,7 @@ public void TestExplicitDynamicFoldersParamsCheck()
214217
Assert.AreEqual("test", dictionary["display_name"]);
215218
Assert.AreEqual("true", dictionary["use_filename_as_display_name"]);
216219
Assert.AreEqual("asset_folder", dictionary["asset_folder"]);
220+
Assert.AreEqual("true", dictionary["use_asset_folder_as_public_id_prefix"]);
217221
}
218222

219223
[Test]

CloudinaryDotNet/Actions/AssetsManagement/GetResourceResult.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,18 @@ public Dictionary<string, string> Metadata
260260
[DataMember(Name = "asset_id")]
261261
public string AssetId { get; set; }
262262

263+
/// <summary>
264+
/// Gets or sets asset folder.
265+
/// </summary>
266+
[DataMember(Name = "asset_folder")]
267+
public string AssetFolder { get; set; }
268+
269+
/// <summary>
270+
/// Gets or sets asset display name.
271+
/// </summary>
272+
[DataMember(Name = "display_name")]
273+
public string DisplayName { get; set; }
274+
263275
/// <summary>
264276
/// Gets or sets list of asset versions.
265277
/// </summary>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace CloudinaryDotNet.Actions
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
6+
/// <summary>
7+
/// Allows to filter resources by asset folder.
8+
/// </summary>
9+
public class ListResourcesByAssetFolderParams : ListResourcesParams
10+
{
11+
/// <summary>
12+
/// Gets or sets the asset folder to filter resources.
13+
/// </summary>
14+
public string AssetFolder { get; set; }
15+
16+
/// <summary>
17+
/// Validate object model.
18+
/// </summary>
19+
/// <exception cref="System.ArgumentException">Tag must be set to list resource by tag.</exception>
20+
public override void Check()
21+
{
22+
base.Check();
23+
24+
if (string.IsNullOrEmpty(AssetFolder))
25+
{
26+
throw new ArgumentException("AssetFolder must be set to filter resources by AssetFolder!");
27+
}
28+
}
29+
30+
/// <summary>
31+
/// Maps object model to dictionary of parameters in cloudinary notation.
32+
/// </summary>
33+
/// <returns>Sorted dictionary of parameters.</returns>
34+
public override SortedDictionary<string, object> ToParamsDictionary()
35+
{
36+
SortedDictionary<string, object> dict = base.ToParamsDictionary();
37+
38+
if (dict.ContainsKey("type"))
39+
{
40+
dict.Remove("type");
41+
}
42+
43+
AddParam(dict, "asset_folder", AssetFolder);
44+
45+
return dict;
46+
}
47+
}
48+
}

CloudinaryDotNet/Actions/AssetsManagement/UpdateParams.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ public UpdateParams(string publicId)
2727
/// </summary>
2828
public string PublicId { get; set; }
2929

30+
/// <summary>
31+
/// Gets or sets the folder where the asset is stored in the Media Library.
32+
/// This value does not impact the asset’s Public ID.
33+
/// </summary>
34+
public string AssetFolder { get; set; }
35+
36+
/// <summary>
37+
/// Gets or sets the name that is displayed for the asset in the Media Library.
38+
/// This value does not impact the asset’s Public ID.
39+
/// </summary>
40+
public string DisplayName { get; set; }
41+
42+
/// <summary>
43+
/// Gets or sets a value indicating whether to make sure that display name is unique.
44+
/// When set to false, should not add random characters at the end of the display name to guarantee its uniqueness.
45+
/// Only relevant if <see cref="DisplayName"/> or <see cref="AssetFolder"/> are provided.
46+
/// </summary>
47+
public bool UniqueDisplayName { get; set; }
48+
3049
/// <summary>
3150
/// Gets or sets the type of file. Possible values: image, raw, video. Default: image.
3251
/// </summary>
@@ -172,6 +191,9 @@ public override SortedDictionary<string, object> ToParamsDictionary()
172191
SortedDictionary<string, object> dict = base.ToParamsDictionary();
173192

174193
AddParam(dict, "public_id", PublicId);
194+
AddParam(dict, "asset_folder", AssetFolder);
195+
AddParam(dict, "display_name", DisplayName);
196+
AddParam(dict, "unique_display_name", UniqueDisplayName);
175197
AddParam(dict, "tags", Tags);
176198
AddParam(dict, "type", Type);
177199
AddParam(dict, "categorization", Categorization);

CloudinaryDotNet/Actions/AssetsUpload/ExplicitParams.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ public ExplicitParams(string publicId)
105105
/// </summary>
106106
public string AssetFolder { get; set; }
107107

108+
/// <summary>
109+
/// Gets or sets a value indicating whether gets or sets whether to automatically apply the path specified
110+
/// in the asset_folder parameter (or the folder that's in focus when an asset is uploaded directly
111+
/// to a folder in the Media Library) as a prefix to the public_id value.
112+
/// </summary>
113+
public bool? UseAssetFolderAsPublicIdPrefix { get; set; }
114+
108115
/// <summary>
109116
/// Gets or sets the coordinates of faces contained in an uploaded image and overrides the automatically detected
110117
/// faces.
@@ -276,6 +283,7 @@ public override SortedDictionary<string, object> ToParamsDictionary()
276283
AddParam(dict, "display_name", DisplayName);
277284
AddParam(dict, "use_filename_as_display_name", UseFilenameAsDisplayName);
278285
AddParam(dict, "asset_folder", AssetFolder);
286+
AddParam(dict, "use_asset_folder_as_public_id_prefix", UseAssetFolderAsPublicIdPrefix);
279287

280288
if (ResourceType == ResourceType.Image)
281289
{

CloudinaryDotNet/Actions/AssetsUpload/RawUploadParams.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ public RawUploadParams()
103103
/// </summary>
104104
public string AssetFolder { get; set; }
105105

106+
/// <summary>
107+
/// Gets or sets a value indicating whether gets or sets whether to automatically apply the path specified
108+
/// in the asset_folder parameter (or the folder that's in focus when an asset is uploaded directly
109+
/// to a folder in the Media Library) as a prefix to the public_id value.
110+
/// </summary>
111+
public bool? UseAssetFolderAsPublicIdPrefix { get; set; }
112+
106113
/// <summary>
107114
/// Gets or sets whether to overwrite existing resources with the same public ID.
108115
/// </summary>
@@ -182,6 +189,7 @@ public override SortedDictionary<string, object> ToParamsDictionary()
182189
AddParam(dict, "proxy", Proxy);
183190
AddParam(dict, "folder", Folder);
184191
AddParam(dict, "asset_folder", AssetFolder);
192+
AddParam(dict, "use_asset_folder_as_public_id_prefix", UseAssetFolderAsPublicIdPrefix);
185193
AddParam(dict, "raw_convert", RawConvert);
186194
AddParam(dict, "overwrite", Overwrite);
187195
AddParam(dict, "async", Async);

0 commit comments

Comments
 (0)