Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: avoid expired thumbnails #1

Merged
merged 2 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/Skybrud.VideoPicker.Skyfish/Controllers/SkyfishController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Limbo.Integrations.Skyfish;
using Skybrud.VideoPicker.Exceptions;
using Skybrud.VideoPicker.Services;
using Skybrud.WebApi.Json;
using Umbraco.Web.WebApi;

namespace Skybrud.VideoPicker.Skyfish.Controllers {
[JsonOnlyConfiguration]
public class SkyfishController : UmbracoApiController {

private readonly VideoPickerService _videoPickerService;

public SkyfishController(VideoPickerService videoPickerService) {
_videoPickerService = videoPickerService;
}

[Route("umbraco/api/skyfish/GetThumbnail")]
public object GetThumbnail(string uniqueMediaId) {

if (!_videoPickerService.Providers.TryGet(out SkyfishVideoProvider provider)) return Request.CreateResponse(HttpStatusCode.NotFound);
if (!_videoPickerService.Config.TryGetConfig(provider, out SkyfishConfig config)) return Request.CreateResponse(HttpStatusCode.NotFound);

// Get the first credentials (or trigger an error if none)
SkyfishCredentials credentials = config.Credentials.FirstOrDefault();
if (credentials == null) throw new VideosException("Skyfish provider is not configured (1).");
if (credentials.IsConfigured == false) throw new VideosException("Skyfish provider is not configured (2).");

// Initialize a new service for the Skyfish API
SkyfishHttpService api = SkyfishHttpService.CreateFromKeys(credentials.PublicKey, credentials.SecretKey, credentials.Username, credentials.Password);
SkyfishHttpHelper skyHelper = new SkyfishHttpHelper(api);

var thumbnailUrl = skyHelper.GetVideoByUniqueMediaId(int.Parse(uniqueMediaId)).ThumbnailUrl;

if (!string.IsNullOrWhiteSpace(thumbnailUrl)) return Redirect(thumbnailUrl);
return Request.CreateResponse(HttpStatusCode.NotFound);

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>1.0.0-beta001</Version>
<Version>1.0.0-beta002</Version>
<Company>Limbo</Company>
<Product>Skybrud.VideoPicker</Product>
<Authors>Jesper Mayntzhusen</Authors>
Expand All @@ -29,7 +29,7 @@

<!-- Include NuGet dependencies -->
<ItemGroup>
<PackageReference Include="Limbo.Integrations.Skyfish" Version="1.0.0-beta001" />
<PackageReference Include="Limbo.Integrations.Skyfish" Version="1.0.0-beta002" />
<PackageReference Include="Skybrud.VideoPicker" Version="2.0.0-alpha006">
<ExcludeAssets>contentFiles</ExcludeAssets>
</PackageReference>
Expand Down
12 changes: 6 additions & 6 deletions src/Skybrud.VideoPicker.Skyfish/SkyfishVideoDetails.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Limbo.Integrations.Skyfish.Models;
using Limbo.Integrations.Skyfish.Models.Media;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Skybrud.Essentials.Json.Extensions;
Expand Down Expand Up @@ -29,11 +29,11 @@ public class SkyfishVideoDetails : IVideoDetails {

#region Constructors

public SkyfishVideoDetails(SkyfishVideo video, VideoThumbnail[] thumbnails) {
Id = video.VideoId.ToString();
Title = string.IsNullOrWhiteSpace(video.VideoTitle) ? video.FileName : video.VideoTitle;
Description = video.VideoDescription;
EmbedUrl = video.EmbedUrl;
public SkyfishVideoDetails(SkyfishMediaItem video, VideoThumbnail[] thumbnails, string embedUrl) {
Id = video.UniqueMediaId.ToString();
Title = string.IsNullOrWhiteSpace(video.Title) ? video.FileName : video.Title;
Description = video.Description;
EmbedUrl = embedUrl;
Thumbnails = thumbnails;
}

Expand Down
22 changes: 14 additions & 8 deletions src/Skybrud.VideoPicker.Skyfish/SkyfishVideoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Limbo.Integrations.Skyfish;
using Limbo.Integrations.Skyfish.Models;
using Limbo.Integrations.Skyfish.Models.Media;
using Newtonsoft.Json.Linq;
using Skybrud.Essentials.Json.Extensions;
using Skybrud.Essentials.Strings.Extensions;
using Skybrud.VideoPicker.Exceptions;
using Skybrud.VideoPicker.Models;
using Skybrud.VideoPicker.Models.Config;
Expand Down Expand Up @@ -57,15 +56,17 @@ public VideoPickerValue GetVideo(VideoPickerService service, IVideoOptions optio

// Initialize a new service for the Skyfish API
SkyfishHttpService api = SkyfishHttpService.CreateFromKeys(credentials.PublicKey, credentials.SecretKey, credentials.Username, credentials.Password);
SkyfishHttpHelper skyHelper = new SkyfishHttpHelper(api);

SkyfishVideo video = api.GetVideo(int.Parse(o.VideoId));
SkyfishMediaItem video = skyHelper.GetVideoByMediaId(int.Parse(o.VideoId));
string embedUrl = skyHelper.GetEmbedUrlByUniqueMediaId(video.UniqueMediaId);

// As thumbnail URLs received from the Skyfish API expire over time, we need to create our own solution to handle thumbnails URLs
VideoThumbnail[] thumbnails = GetThumbnails(video, config, credentials);
VideoThumbnail[] thumbnails = GetThumbnails(video);

VideoProviderDetails provider = new VideoProviderDetails(Alias, Name);

SkyfishVideoDetails details = new SkyfishVideoDetails(video, thumbnails);
SkyfishVideoDetails details = new SkyfishVideoDetails(video, thumbnails, embedUrl);

SkyfishEmbedOptions embed = new SkyfishEmbedOptions(details);

Expand Down Expand Up @@ -98,17 +99,22 @@ internal bool IsValidName(string videoName) {
return videoName != null && Regex.IsMatch(videoName, "^([0-9_]+)$");
}

internal VideoThumbnail[] GetThumbnails(SkyfishVideo video, SkyfishConfig config, SkyfishCredentials credentials) {
internal VideoThumbnail[] GetThumbnails(SkyfishMediaItem video) {

List<VideoThumbnail> thumbnails = new List<VideoThumbnail>();

if (video.ThumbnailUrl.HasValue()) thumbnails.Add(new VideoThumbnail(0, 0, video.ThumbnailUrl));
if (video.ThumbnailUrlSsl.HasValue()) thumbnails.Add(new VideoThumbnail(0, 0, video.ThumbnailUrlSsl));
thumbnails.Add(GetThumbnail(video));

return thumbnails.ToArray();

}

private VideoThumbnail GetThumbnail(SkyfishMediaItem video) {
string url = $"/umbraco/api/Skyfish/GetThumbnail?uniqueMediaId={video.UniqueMediaId}";

return new VideoThumbnail(0, 0, url);
}

}

}