Skip to content

Commit a5fdde1

Browse files
Add support for CreateSlideshow Upload API
1 parent aaa172e commit a5fdde1

File tree

9 files changed

+391
-2
lines changed

9 files changed

+391
-2
lines changed

CloudinaryDotNet.IntegrationTests/IntegrationTestBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ private void SaveEmbeddedToDisk(Assembly assembly, string sourcePath, string des
200200
stream.CopyTo(fileStream);
201201
}
202202
}
203-
catch (IOException e)
203+
catch (IOException)
204204
{
205-
205+
206206
}
207207
}
208208

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System.Collections.Generic;
2+
using CloudinaryDotNet.Actions;
3+
using NUnit.Framework;
4+
using SystemHttp = System.Net.Http;
5+
6+
namespace CloudinaryDotNet.Tests.UploadApi
7+
{
8+
public class CreateSlideshowMethodsTest
9+
{
10+
[Test]
11+
public void TestCreateSlideshowFromManifestTransformation()
12+
{
13+
var cloudinary = new MockedCloudinary();
14+
15+
const string slideshowManifest = "w_352;h_240;du_5;fps_30;vars_(slides_((media_s64:aHR0cHM6Ly9y" +
16+
"ZXMuY2xvdWRpbmFyeS5jb20vZGVtby9pbWFnZS91cGxvYWQvY291cGxl);(media_s64:aH" +
17+
"R0cHM6Ly9yZXMuY2xvdWRpbmFyeS5jb20vZGVtby9pbWFnZS91cGxvYWQvc2FtcGxl)))";
18+
19+
var csParams = new CreateSlideshowParams
20+
{
21+
ManifestTransformation = new Transformation().CustomFunction(CustomFunction.Render(slideshowManifest)),
22+
Tags = new List<string> {"tag1", "tag2", "tag3"},
23+
Transformation = new Transformation().FetchFormat("auto").Quality("auto")
24+
};
25+
26+
cloudinary.CreateSlideshow(csParams);
27+
28+
cloudinary.AssertHttpCall(SystemHttp.HttpMethod.Post, "video/create_slideshow");
29+
30+
foreach (var expected in new List<string>
31+
{
32+
$"fn_render:{slideshowManifest}",
33+
"tag1",
34+
"tag2",
35+
"tag3",
36+
"f_auto,q_auto"
37+
})
38+
{
39+
StringAssert.Contains(expected, cloudinary.HttpRequestContent);
40+
}
41+
}
42+
[Test]
43+
public void TestCreateSlideshowFromManifestJson()
44+
{
45+
var cloudinary = new MockedCloudinary();
46+
47+
const string expectedManifestJson =
48+
@"{""w"":848,""h"":480,""du"":6,""fps"":30,""vars"":{""sdur"":500,""tdur"":500,""slides"":"+
49+
@"[{""media"":""i:protests9""},{""media"":""i:protests8""},{""media"":""i:protests7""},"+
50+
@"{""media"":""i:protests6""},{""media"":""i:protests2""},{""media"":""i:protests1""}]}}";
51+
52+
const string notificationUrl = "https://example.com";
53+
const string uploadPreset = "test_preset";
54+
const string testId = "test_id";
55+
56+
57+
var csParams = new CreateSlideshowParams
58+
{
59+
ManifestJson = new SlideshowManifest
60+
{
61+
Width = 848,
62+
Height = 480,
63+
Duration = 6,
64+
Fps = 30,
65+
Variables = new Slideshow
66+
{
67+
SlideDuration = 500,
68+
TransitionDuration = 500,
69+
Slides = new List<Slide>
70+
{
71+
new Slide("i:protests9"), new Slide("i:protests8"), new Slide("i:protests7"),
72+
new Slide("i:protests6"), new Slide("i:protests2"), new Slide("i:protests1")
73+
}
74+
}
75+
},
76+
PublicId = testId,
77+
NotificationUrl = notificationUrl,
78+
UploadPreset = uploadPreset,
79+
Overwrite = true
80+
};
81+
82+
cloudinary.CreateSlideshow(csParams);
83+
84+
foreach (var expected in new List<string>
85+
{
86+
expectedManifestJson,
87+
testId,
88+
notificationUrl,
89+
uploadPreset,
90+
"1" // Overwrite
91+
})
92+
{
93+
StringAssert.Contains(expected, cloudinary.HttpRequestContent);
94+
}
95+
}
96+
}
97+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
namespace CloudinaryDotNet.Actions
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using Newtonsoft.Json;
6+
7+
/// <summary>
8+
/// Parameters of generating a slideshow.
9+
/// </summary>
10+
public class CreateSlideshowParams : BaseParams
11+
{
12+
/// <summary>
13+
/// Gets or sets the manifest transformation for slideshow creation.
14+
/// </summary>
15+
public Transformation ManifestTransformation { get; set; }
16+
17+
/// <summary>
18+
/// Gets or sets the manifest json for slideshow creation.
19+
/// </summary>
20+
public SlideshowManifest ManifestJson { get; set; }
21+
22+
/// <summary>
23+
/// Gets or sets the identifier that is used for accessing the generated video.
24+
/// </summary>
25+
public string PublicId { get; set; }
26+
27+
/// <summary>
28+
/// Gets or sets an additional transformation to run on the created slideshow before saving it in the cloud.
29+
/// For example: limit the dimensions of the uploaded image to 512x512 pixels.
30+
/// </summary>
31+
public Transformation Transformation { get; set; }
32+
33+
/// <summary>
34+
/// Gets or sets a list of tag names to assign to the generated slideshow.
35+
/// </summary>
36+
/// <returns>A list of strings where each element represents a tag name.</returns>
37+
public List<string> Tags { get; set; }
38+
39+
/// <summary>
40+
/// Gets or sets whether to overwrite existing resources with the same public ID.
41+
/// </summary>
42+
public bool? Overwrite { get; set; }
43+
44+
/// <summary>
45+
/// Gets or sets an HTTP URL to send notification to (a webhook) when the operation or any additional
46+
/// requested asynchronous action is completed. If not specified,
47+
/// the response is sent to the global Notification URL (if defined)
48+
/// in the Upload settings of your account console.
49+
/// </summary>
50+
public string NotificationUrl { get; set; }
51+
52+
/// <summary>
53+
/// Gets or sets whether it is allowed to use an upload preset for setting parameters of this upload (optional).
54+
/// </summary>
55+
public string UploadPreset { get; set; }
56+
57+
/// <summary>
58+
/// Validate object model.
59+
/// </summary>
60+
public override void Check()
61+
{
62+
if (ManifestTransformation == null && ManifestJson == null)
63+
{
64+
throw new ArgumentException("Please specify ManifestTransformation or ManifestJson");
65+
}
66+
}
67+
68+
/// <summary>
69+
/// Maps object model to dictionary of parameters in cloudinary notation.
70+
/// </summary>
71+
/// <returns>Sorted dictionary of parameters.</returns>
72+
public override SortedDictionary<string, object> ToParamsDictionary()
73+
{
74+
SortedDictionary<string, object> dict = base.ToParamsDictionary();
75+
76+
AddParam(dict, "manifest_json", JsonConvert.SerializeObject(ManifestJson, new JsonSerializerSettings
77+
{
78+
NullValueHandling = NullValueHandling.Ignore,
79+
DefaultValueHandling = DefaultValueHandling.Ignore,
80+
}));
81+
AddParam(dict, "manifest_transformation", ManifestTransformation?.Generate());
82+
AddParam(dict, "public_id", PublicId);
83+
AddParam(dict, "transformation", Transformation?.Generate());
84+
AddParam(dict, "tags", Tags);
85+
AddParam(dict, "overwrite", Overwrite);
86+
AddParam(dict, "notification_url", NotificationUrl);
87+
AddParam(dict, "upload_preset", UploadPreset);
88+
89+
return dict;
90+
}
91+
}
92+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace CloudinaryDotNet.Actions
2+
{
3+
using System.Runtime.Serialization;
4+
5+
/// <summary>
6+
/// Result of the create slideshow operation.
7+
/// </summary>
8+
[DataContract]
9+
public class CreateSlideshowResult : BaseResult
10+
{
11+
/// <summary>
12+
/// Gets or sets the status of the create slideshow operation.
13+
/// </summary>
14+
[DataMember(Name = "status")]
15+
public string Status { get; set; }
16+
17+
/// <summary>
18+
/// Gets or sets the public ID assigned to the asset.
19+
/// </summary>
20+
[DataMember(Name = "public_id")]
21+
public string PublicId { get; set; }
22+
23+
/// <summary>
24+
/// Gets or sets the ID of the batch.
25+
/// </summary>
26+
[DataMember(Name = "batch_id")]
27+
public string BatchId { get; set; }
28+
}
29+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
namespace CloudinaryDotNet.Actions
2+
{
3+
using Newtonsoft.Json;
4+
5+
/// <summary>
6+
/// Represents settings of a single slide.
7+
/// Is a part of Slideshow.
8+
/// </summary>
9+
public class Slide
10+
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="Slide"/> class.
13+
/// </summary>
14+
/// <param name="media">The media.</param>
15+
public Slide(string media)
16+
{
17+
Media = media;
18+
}
19+
20+
/// <summary>
21+
/// Gets or sets the media.
22+
/// Specify images as i:[public_id]. Specify videos as v:[public_id].
23+
/// </summary>
24+
[JsonProperty(PropertyName = "media")]
25+
public string Media { get; set; }
26+
27+
/// <summary>
28+
/// Gets or sets the slide type. Set to video when using a video for a slide.
29+
/// For example: media_v:my-public-id;type_s:video. Server Default: image.
30+
/// </summary>
31+
[JsonProperty(PropertyName = "type")]
32+
public int Type { get; set; }
33+
34+
/// <summary>
35+
/// Gets or sets the transition to use from the individual slide to the next. Server Default: CrossZoom.
36+
/// </summary>
37+
[JsonProperty(PropertyName = "transition_s")]
38+
public string Transition { get; set; }
39+
40+
/// <summary>
41+
/// Gets or sets the slide duration in milliseconds. Server Default: 3000.
42+
/// </summary>
43+
[JsonProperty(PropertyName = "sdur")]
44+
public int SlideDuration { get; set; }
45+
46+
/// <summary>
47+
/// Gets or sets the transition duration in milliseconds. Server Default: 1000.
48+
/// </summary>
49+
[JsonProperty(PropertyName = "tdur")]
50+
public int TransitionDuration { get; set; }
51+
}
52+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace CloudinaryDotNet.Actions
2+
{
3+
using System.Collections.Generic;
4+
using Newtonsoft.Json;
5+
6+
/// <summary>
7+
/// Represents settings of the slideshow.
8+
/// Is a part of SlideshowManifest.
9+
/// </summary>
10+
public class Slideshow
11+
{
12+
/// <summary>
13+
/// Gets or sets the transition to use for all slides. Server Default: CrossZoom.
14+
/// </summary>
15+
[JsonProperty(PropertyName = "transition_s")]
16+
public string Transition { get; set; }
17+
18+
/// <summary>
19+
/// Gets or sets a single transformation to apply to all slides. Server Default: null.
20+
/// </summary>
21+
[JsonProperty(PropertyName = "transformation_s")]
22+
public string Transformation { get; set; }
23+
24+
/// <summary>
25+
/// Gets or sets the duration for all slides in milliseconds. Server Default: 3000.
26+
/// </summary>
27+
[JsonProperty(PropertyName = "sdur")]
28+
public int SlideDuration { get; set; }
29+
30+
/// <summary>
31+
/// Gets or sets the duration for all transitions in milliseconds. Server Default: 1000.
32+
/// </summary>
33+
[JsonProperty(PropertyName = "tdur")]
34+
public int TransitionDuration { get; set; }
35+
36+
/// <summary>
37+
/// Gets or sets the duration for all transitions in milliseconds. Server Default: 1000.
38+
/// </summary>
39+
[JsonProperty(PropertyName = "slides")]
40+
public List<Slide> Slides { get; set; }
41+
}
42+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace CloudinaryDotNet.Actions
2+
{
3+
using Newtonsoft.Json;
4+
5+
/// <summary>
6+
/// Represents a manifest for slideshow creation.
7+
/// </summary>
8+
public class SlideshowManifest
9+
{
10+
/// <summary>
11+
/// Gets or sets the width of the slideshow in pixels.
12+
/// </summary>
13+
[JsonProperty(PropertyName = "w")]
14+
public int Width { get; set; }
15+
16+
/// <summary>
17+
/// Gets or sets the height of the slideshow in pixels.
18+
/// </summary>
19+
[JsonProperty(PropertyName = "h")]
20+
public int Height { get; set; }
21+
22+
/// <summary>
23+
/// Gets or sets the duration of the slideshow in seconds. Server Default: 10.
24+
/// </summary>
25+
[JsonProperty(PropertyName = "du", NullValueHandling=NullValueHandling.Ignore)]
26+
public int Duration { get; set; }
27+
28+
/// <summary>
29+
/// Gets or sets the frames per second of the slideshow. Server Default: 20.
30+
/// </summary>
31+
[JsonProperty(PropertyName = "fps", NullValueHandling=NullValueHandling.Ignore)]
32+
public int Fps { get; set; }
33+
34+
/// <summary>
35+
/// Gets or sets the slideshow settings.
36+
/// </summary>
37+
[JsonProperty(PropertyName = "vars")]
38+
public Slideshow Variables { get; set; }
39+
}
40+
}

0 commit comments

Comments
 (0)