-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Getting the saved data and models right
- Loading branch information
Showing
11 changed files
with
512 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Collections.Generic; | ||
using Skybrud.Essentials.Http.Collections; | ||
using Skybrud.Essentials.Strings; | ||
|
||
namespace Limbo.Umbraco.YouTube.Extensions { | ||
|
||
internal static class YouTubeExtensions { | ||
|
||
public static void Deconstruct<T>(this IList<T> list, out T first, out T second) { | ||
// TODO: Move to Skybrud.Essentials | ||
first = list.Count > 0 ? list[0] : default; | ||
second = list.Count > 1 ? list[1] : default; | ||
} | ||
|
||
public static bool TryGetBoolean(this IHttpQueryString query, string key, out bool result) { | ||
// TODO: Move to Skybrud.Essentials.Http | ||
return StringUtils.TryParseBoolean(query[key], out result); | ||
} | ||
|
||
public static bool TryGetInt32(this IHttpQueryString query, string key, out int result) { | ||
// TODO: Move to Skybrud.Essentials.Http | ||
return int.TryParse(query[key], out result); | ||
} | ||
|
||
public static bool TryGetDouble(this IHttpQueryString query, string key, out double result) { | ||
// TODO: Move to Skybrud.Essentials.Http | ||
return double.TryParse(query[key], out result); | ||
} | ||
|
||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
src/Limbo.Umbraco.YouTube/Json/Converters/TimeSpanSecondsConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Globalization; | ||
using Newtonsoft.Json; | ||
|
||
namespace Limbo.Umbraco.YouTube.Json.Converters { | ||
|
||
/// <summary> | ||
/// JSON converter for seriqalizing and deserializating between instances of <see cref="TimeSpan"/> and the total | ||
/// amount of seconds represented by the time span. | ||
/// </summary> | ||
internal class TimeSpanSecondsConverter : JsonConverter { | ||
|
||
// TODO: Move to Skybrud.Essentials | ||
|
||
/// <summary> | ||
/// Writes the JSON representation of the object. | ||
/// </summary> | ||
/// <param name="writer">The <see cref="JsonWriter"/> to write to.</param> | ||
/// <param name="value">The value.</param> | ||
/// <param name="serializer">The calling serializer.</param> | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { | ||
|
||
switch (value) { | ||
|
||
case null: | ||
writer.WriteNull(); | ||
break; | ||
|
||
case TimeSpan ts: | ||
writer.WriteRawValue(ts.TotalSeconds.ToString(CultureInfo.InvariantCulture)); | ||
break; | ||
|
||
default: | ||
throw new JsonSerializationException($"Unsupported type: {value.GetType()}"); | ||
|
||
} | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Reads the JSON representation of the object. | ||
/// </summary> | ||
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param> | ||
/// <param name="objectType">Type of the object.</param> | ||
/// <param name="existingValue">The existing value of object being read.</param> | ||
/// <param name="serializer">The calling serializer.</param> | ||
/// <returns>The object value.</returns> | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { | ||
|
||
if (objectType != typeof(TimeSpan) && objectType != typeof(TimeSpan?)) throw new JsonSerializationException($"Object type {objectType} is not supported"); | ||
|
||
switch (reader.TokenType) { | ||
|
||
case JsonToken.Null: | ||
return objectType != typeof(TimeSpan?) ? null : default(TimeSpan); | ||
|
||
case JsonToken.Integer: | ||
case JsonToken.Float: | ||
return TimeSpan.FromSeconds((double) Convert.ChangeType(reader.Value, typeof(double), CultureInfo.InvariantCulture)); | ||
|
||
default: | ||
throw new JsonSerializationException("Unexpected token type: " + reader.TokenType); | ||
|
||
} | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Determines whether this instance can convert the specified object type. | ||
/// </summary> | ||
/// <param name="objectType">Type of the object.</param> | ||
/// <returns><c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.</returns> | ||
public override bool CanConvert(Type objectType) { | ||
return objectType == typeof(TimeSpan); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/Limbo.Umbraco.YouTube/Options/YouTubeEmbedOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using System; | ||
using System.Web; | ||
using Limbo.Umbraco.YouTube.Models.Videos; | ||
using Microsoft.AspNetCore.Html; | ||
using Newtonsoft.Json; | ||
using Skybrud.Essentials.Common; | ||
|
||
namespace Limbo.Umbraco.YouTube.Options { | ||
|
||
/// <summary> | ||
/// Class representing the embed options for a Vimeo video | ||
/// </summary> | ||
public class YouTubeEmbedOptions { | ||
|
||
#region Properties | ||
|
||
/// <summary> | ||
/// Gets or sets the ID of the video. | ||
/// </summary> | ||
[JsonProperty("id")] | ||
public string Id { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the title of the embed iframe. | ||
/// </summary> | ||
[JsonProperty("title", NullValueHandling = NullValueHandling.Ignore)] | ||
public string Title { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the options for the player. | ||
/// </summary> | ||
[JsonProperty("player", NullValueHandling = NullValueHandling.Ignore)] | ||
public YouTubeEmbedPlayerOptions Player { get; set; } | ||
|
||
#endregion | ||
|
||
#region Constructors | ||
|
||
/// <summary> | ||
/// Initializes a new instance based on the specified <paramref name="id"/>. | ||
/// </summary> | ||
/// <param name="id">The ID of the video.</param> | ||
public YouTubeEmbedOptions(string id) { | ||
Id = id; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance based on the specified <paramref name="video"/>. | ||
/// </summary> | ||
/// <param name="video">The Vimeo video.</param> | ||
public YouTubeEmbedOptions(YouTubeVideoDetails video) { | ||
Id = video.Id; | ||
Title = video.Snippet?.Title; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance based on the specified <paramref name="video"/>. | ||
/// </summary> | ||
/// <param name="video">The Vimeo video.</param> | ||
/// <param name="player">The options for the YouTube video player.</param> | ||
public YouTubeEmbedOptions(YouTubeVideoDetails video, YouTubeEmbedPlayerOptions player) { | ||
Id = video.Id; | ||
Title = video.Snippet?.Title; | ||
Player = player; | ||
} | ||
|
||
#endregion | ||
|
||
#region Member methods | ||
|
||
/// <summary> | ||
/// Returns the embed URL for the video. | ||
/// </summary> | ||
/// <returns>The embed URL.</returns> | ||
public string GetEmbedUrl() { | ||
return (Player ?? new YouTubeEmbedPlayerOptions()).GetEmbedUrl(Id); | ||
} | ||
|
||
/// <summary> | ||
/// Returns the HTML embed code for the video described by this options instance. | ||
/// </summary> | ||
/// <returns>An instance of <see cref="HtmlString"/> representing the HTML embed code.</returns> | ||
public virtual HtmlString GetEmbedCode() { | ||
return GetEmbedCode(null, null); | ||
} | ||
|
||
/// <summary> | ||
/// Returns the HTML embed code for the video described by this options instance. | ||
/// </summary> | ||
/// <param name="width">The width of the video.</param> | ||
/// <param name="height">The height of the video.</param> | ||
/// <returns>An instance of <see cref="HtmlString"/> representing the HTML embed code.</returns> | ||
public virtual HtmlString GetEmbedCode(int? width, int? height) { | ||
|
||
width ??= 560; | ||
height ??= 315; | ||
|
||
if (string.IsNullOrWhiteSpace(Id)) throw new PropertyNotSetException(nameof(Id)); | ||
|
||
string url = (Player ?? new YouTubeEmbedPlayerOptions()).GetEmbedUrl(Id); | ||
|
||
string title = string.IsNullOrWhiteSpace(Title) ? "Vimeo video player" : Title; | ||
|
||
string html = $"<iframe width=\"{width}\" height=\"{height}\" src=\"{HttpUtility.HtmlAttributeEncode(url)}\" title=\"{HttpUtility.HtmlAttributeEncode(title)}\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>"; | ||
|
||
return new HtmlString(html); | ||
|
||
} | ||
|
||
#endregion | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.