Skip to content

Commit 718f550

Browse files
Allow generating archive with multiple resource types
1 parent 99afd4c commit 718f550

File tree

5 files changed

+149
-16
lines changed

5 files changed

+149
-16
lines changed

Shared.Tests/CloudinaryTest.cs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,37 @@ public void TestResourcesCursor()
14741474
Assert.AreNotEqual(result1.Resources[0].PublicId, result2.Resources[0].PublicId);
14751475
}
14761476

1477+
[Test]
1478+
public void TestResourceFullyQualifiedPublicId()
1479+
{
1480+
// should return correct FullyQualifiedPublicId
1481+
1482+
var uploadParams = new ImageUploadParams()
1483+
{
1484+
File = new FileDescription(m_testImagePath),
1485+
PublicId = GetUniquePublicId(),
1486+
Tags = m_apiTag
1487+
};
1488+
1489+
m_cloudinary.Upload(uploadParams);
1490+
1491+
var listParams = new ListResourcesParams()
1492+
{
1493+
ResourceType = ResourceType.Image,
1494+
MaxResults = 1
1495+
};
1496+
1497+
var result = m_cloudinary.ListResources(listParams);
1498+
1499+
Assert.IsNotNull(result.Resources);
1500+
Assert.AreEqual(1, result.Resources.Length);
1501+
1502+
var res = result.Resources[0];
1503+
var expectedFullQualifiedPublicId = $"{res.ResourceType}/{res.Type}/{res.PublicId}";
1504+
1505+
Assert.AreEqual(expectedFullQualifiedPublicId, res.FullyQualifiedPublicId);
1506+
}
1507+
14771508
[Test]
14781509
public void TestEager()
14791510
{
@@ -3286,6 +3317,73 @@ public void TestCreateArchiveMultiplePublicIds()
32863317
Assert.AreEqual(1, result.FileCount);
32873318
}
32883319

3320+
[Test]
3321+
public void TestCreateArchiveMultipleResourceTypes()
3322+
{
3323+
var raw = ApiShared.GetCloudinaryParam(ResourceType.Raw);
3324+
3325+
var tag = GetMethodTag();
3326+
3327+
var rawUploadParams = new RawUploadParams()
3328+
{
3329+
File = new FileDescription(m_testPdfPath),
3330+
Tags = $"{tag},{m_apiTag}"
3331+
};
3332+
3333+
var upRes1 = m_cloudinary.Upload(rawUploadParams, raw);
3334+
3335+
var imageUploadParams = new ImageUploadParams()
3336+
{
3337+
File = new FileDescription(m_testImagePath),
3338+
Tags = $"{tag},{m_apiTag}"
3339+
};
3340+
3341+
var upRes2 = m_cloudinary.Upload(imageUploadParams);
3342+
3343+
var videoUploadParams = new VideoUploadParams()
3344+
{
3345+
File = new FileDescription(m_testVideoPath),
3346+
Tags = $"{tag},{m_apiTag}"
3347+
};
3348+
3349+
var upRes3 = m_cloudinary.Upload(videoUploadParams);
3350+
3351+
var fQPublicIds = new List<string>
3352+
{
3353+
upRes1.FullyQualifiedPublicId,
3354+
upRes2.FullyQualifiedPublicId,
3355+
upRes3.FullyQualifiedPublicId
3356+
};
3357+
3358+
var parameters = new ArchiveParams()
3359+
.UseOriginalFilename(true)
3360+
.TargetTags(new List<string> { tag, m_apiTag });
3361+
3362+
var ex = Assert.Throws<ArgumentException>(() => m_cloudinary.CreateArchive(parameters));
3363+
3364+
StringAssert.StartsWith("At least one of the following", ex.Message);
3365+
3366+
parameters.ResourceType("auto").Tags(new List<string> {"tag"});
3367+
3368+
ex = Assert.Throws<ArgumentException>(() => m_cloudinary.CreateArchive(parameters));
3369+
3370+
StringAssert.StartsWith("To create an archive with multiple types of assets", ex.Message);
3371+
3372+
parameters.ResourceType("").Tags(null).FullyQualifiedPublicIds(fQPublicIds);
3373+
3374+
ex = Assert.Throws<ArgumentException>(() => m_cloudinary.CreateArchive(parameters));
3375+
3376+
StringAssert.StartsWith("To create an archive with multiple types of assets", ex.Message);
3377+
3378+
Assert.AreEqual(fQPublicIds, parameters.FullyQualifiedPublicIds());
3379+
3380+
parameters.ResourceType("auto");
3381+
3382+
var result = m_cloudinary.CreateArchive(parameters);
3383+
3384+
Assert.AreEqual(3, result.FileCount);
3385+
}
3386+
32893387
/// <summary>
32903388
/// Should create a zip archive
32913389
/// </summary>

Shared/Actions/ArchiveParams.cs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace CloudinaryDotNet.Actions
1010
public class ArchiveParams : BaseParams
1111
{
1212
private List<string> m_publicIds;
13+
private List<string> m_fullyQualifiedPublicIds;
1314
private List<string> m_tags;
1415
private List<string> m_prefixes;
1516

@@ -48,6 +49,23 @@ public ArchiveParams PublicIds(List<string> publicIds)
4849
return this;
4950
}
5051

52+
/// <summary>
53+
/// Get a list of Fully Qualified Public IDs for the specific assets to be included in the archive
54+
/// </summary>
55+
public List<string> FullyQualifiedPublicIds()
56+
{
57+
return m_fullyQualifiedPublicIds;
58+
}
59+
60+
/// <summary>
61+
/// Set a list of Fully Qualified Public IDs for the specific assets to be included in the archive
62+
/// </summary>
63+
public ArchiveParams FullyQualifiedPublicIds(List<string> fullyQualifiedPublicIds)
64+
{
65+
m_fullyQualifiedPublicIds = fullyQualifiedPublicIds;
66+
return this;
67+
}
68+
5169
/// <summary>
5270
/// Get a list of tag names. All images with this tag(s) will be included in the archive
5371
/// </summary>
@@ -83,19 +101,29 @@ public ArchiveParams Prefixes(List<string> prefixes)
83101
return this;
84102
}
85103

104+
/// <inheritdoc />
86105
/// <summary>
87106
/// Validate object model
88107
/// </summary>
89108
public override void Check()
90109
{
91-
if ((m_publicIds == null || m_publicIds.Count == 0) &&
92-
(m_prefixes == null || m_prefixes.Count == 0) &&
93-
(m_tags == null || m_tags.Count == 0))
94-
throw new ArgumentException("At least one of the following \"filtering\" parameters needs to be specified: PublicIds, Tags or Prefixes.");
110+
if (m_publicIds?.Any() != true &&
111+
m_fullyQualifiedPublicIds?.Any() != true &&
112+
m_prefixes?.Any() != true &&
113+
m_tags?.Any() != true)
114+
throw new ArgumentException("At least one of the following \"filtering\" parameters needs " +
115+
"to be specified: PublicIds, FullyQualifiedPublicIds, Tags or Prefixes.");
116+
117+
if (m_resourceType == "auto" ^ (m_fullyQualifiedPublicIds?.Any() ?? false))
118+
{
119+
throw new ArgumentException(
120+
"To create an archive with multiple types of assets, you must set ResourceType to \"auto\" " +
121+
"and provide FullyQualifiedPublicIds (For example, 'video/upload/my_video.mp4')");
122+
}
95123
}
96124

97125
/// <summary>
98-
/// Get Mode whether to return the generated archive file (download)
126+
/// Get Mode whether to return the generated archive file (download)
99127
/// or to store it as a raw resource (create)
100128
/// </summary>
101129
public virtual ArchiveCallMode Mode()
@@ -104,7 +132,7 @@ public virtual ArchiveCallMode Mode()
104132
}
105133

106134
/// <summary>
107-
/// Determines whether to return the generated archive file (download)
135+
/// Determines whether to return the generated archive file (download)
108136
/// or to store it as a raw resource in your Cloudinary account and return a JSON with the URLs for accessing the archive file (create)
109137
/// </summary>
110138
/// <param name="mode"></param>
@@ -246,7 +274,7 @@ public int ExpiresAt()
246274
}
247275

248276
/// <summary>
249-
/// Set the Unix time in seconds when the generated download URL expires (e.g., 1415060076).
277+
/// Set the Unix time in seconds when the generated download URL expires (e.g., 1415060076).
250278
/// If this parameter is omitted then the generated download URL expires after 1 hour.
251279
/// Note: Relevant only for download call.
252280
/// </summary>
@@ -382,6 +410,9 @@ public override SortedDictionary<string, object> ToParamsDictionary()
382410
if (m_publicIds != null && m_publicIds.Count > 0)
383411
AddParam(dict, "public_ids", m_publicIds);
384412

413+
if (m_fullyQualifiedPublicIds != null && m_fullyQualifiedPublicIds.Count > 0)
414+
AddParam(dict, "fully_qualified_public_ids", m_fullyQualifiedPublicIds);
415+
385416
if (m_prefixes != null && m_prefixes.Count > 0)
386417
AddParam(dict, "prefixes", m_prefixes);
387418

Shared/Actions/ExplicitResult.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ namespace CloudinaryDotNet.Actions
1111
[DataContract]
1212
public class ExplicitResult : RawUploadResult
1313
{
14-
[DataMember(Name = "type")]
15-
public string Type { get; protected set; }
16-
1714
[DataMember(Name = "eager")]
1815
public Eager[] Eager { get; protected set; }
1916

@@ -35,10 +32,10 @@ internal override void SetValues(JToken source)
3532
/// </summary>
3633
[DataMember(Name = "status")]
3734
public string Status { get; protected set; }
38-
35+
3936
[DataMember(Name = "info")]
4037
public Info Info { get; protected set; }
41-
38+
4239
[DataMember(Name = "quality_analysis")]
4340
public QualityAnalysis QualityAnalysis{ get; protected set; }
4441
}

Shared/Actions/ListResourcesResult.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class ListResourcesResult : BaseResult
1313

1414
[DataMember(Name = "next_cursor")]
1515
public string NextCursor { get; protected set; }
16-
1716
}
1817

1918
[DataContract]
@@ -45,5 +44,7 @@ public class Resource : UploadResult
4544

4645
[DataMember(Name = "context")]
4746
public JToken Context { get; protected set; }
47+
48+
public string FullyQualifiedPublicId => $"{ResourceType}/{Type}/{PublicId}";
4849
}
4950
}

Shared/Actions/RawUploadResult.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public class RawUploadResult : UploadResult
1616
[DataMember(Name = "signature")]
1717
public string Signature { get; protected set; }
1818

19+
/// <summary>
20+
/// Storage type
21+
/// </summary>
22+
[DataMember(Name = "type")]
23+
public string Type { get; protected set; }
24+
1925
/// <summary>
2026
/// Resource type
2127
/// </summary>
@@ -30,10 +36,11 @@ public class RawUploadResult : UploadResult
3036

3137
[DataMember(Name = "tags")]
3238
public string[] Tags { get; protected set; }
33-
39+
3440
[DataMember(Name = "access_control")]
3541
public List<AccessControlRule> AccessControl { get; protected set; }
36-
42+
43+
public string FullyQualifiedPublicId => $"{ResourceType}/{Type}/{PublicId}";
3744
}
3845

3946
/// <summary>
@@ -47,6 +54,5 @@ public class RawPartUploadResult : RawUploadResult
4754
/// </summary>
4855
[DataMember(Name = "upload_id")]
4956
public string UploadId { get; protected set; }
50-
5157
}
5258
}

0 commit comments

Comments
 (0)