From 4332e5cf511d3e8455bcb8643632ac5cdec1ef97 Mon Sep 17 00:00:00 2001 From: Victor Usoltsev Date: Wed, 7 Oct 2020 21:01:17 +1300 Subject: [PATCH 1/2] Replaces scheme to https for all urls returned to client side. --- src/Ombi.Core/Engine/TvSearchEngine.cs | 4 +- .../Engine/V2/MusicSearchEngineV2.cs | 46 +++++++++---------- src/Ombi.Core/Engine/V2/TvSearchEngineV2.cs | 14 +++--- src/Ombi.Core/Helpers/TvShowRequestBuilder.cs | 14 +++--- src/Ombi.Helpers.Tests/UriHelperTests.cs | 4 +- src/Ombi.Helpers/StringHelper.cs | 2 +- src/Ombi.Mapping/Profiles/TvProfile.cs | 6 ++- src/Ombi.Mapping/Profiles/TvProfileV2.cs | 10 ++-- 8 files changed, 50 insertions(+), 50 deletions(-) diff --git a/src/Ombi.Core/Engine/TvSearchEngine.cs b/src/Ombi.Core/Engine/TvSearchEngine.cs index 0d2cc4850..38ba8be1a 100644 --- a/src/Ombi.Core/Engine/TvSearchEngine.cs +++ b/src/Ombi.Core/Engine/TvSearchEngine.cs @@ -98,7 +98,7 @@ namespace Ombi.Core.Engine }; newSeason.Episodes.Add(new EpisodeRequests { - Url = e.url, + Url = e.url.ToHttpsUrl(), Title = e.name, AirDate = e.airstamp.HasValue() ? DateTime.Parse(e.airstamp) : DateTime.MinValue, EpisodeNumber = e.number, @@ -111,7 +111,7 @@ namespace Ombi.Core.Engine // We already have the season, so just add the episode season.Episodes.Add(new EpisodeRequests { - Url = e.url, + Url = e.url.ToHttpsUrl(), Title = e.name, AirDate = e.airstamp.HasValue() ? DateTime.Parse(e.airstamp) : DateTime.MinValue, EpisodeNumber = e.number, diff --git a/src/Ombi.Core/Engine/V2/MusicSearchEngineV2.cs b/src/Ombi.Core/Engine/V2/MusicSearchEngineV2.cs index fd885c914..13256be0a 100644 --- a/src/Ombi.Core/Engine/V2/MusicSearchEngineV2.cs +++ b/src/Ombi.Core/Engine/V2/MusicSearchEngineV2.cs @@ -85,10 +85,10 @@ namespace Ombi.Core.Engine.V2 if (lidarrArtistTask != null) { var artistResult = await lidarrArtistTask; - info.Banner = artistResult.images?.FirstOrDefault(x => x.coverType.Equals("banner", StringComparison.InvariantCultureIgnoreCase))?.url.Replace("http", "https"); - info.Logo = artistResult.images?.FirstOrDefault(x => x.coverType.Equals("logo", StringComparison.InvariantCultureIgnoreCase))?.url.Replace("http", "https"); - info.Poster = artistResult.images?.FirstOrDefault(x => x.coverType.Equals("poster", StringComparison.InvariantCultureIgnoreCase))?.url.Replace("http", "https"); - info.FanArt = artistResult.images?.FirstOrDefault(x => x.coverType.Equals("fanart", StringComparison.InvariantCultureIgnoreCase))?.url.Replace("http", "https"); + info.Banner = artistResult.images?.FirstOrDefault(x => x.coverType.Equals("banner", StringComparison.InvariantCultureIgnoreCase))?.url.ToHttpsUrl(); + info.Logo = artistResult.images?.FirstOrDefault(x => x.coverType.Equals("logo", StringComparison.InvariantCultureIgnoreCase))?.url.ToHttpsUrl(); + info.Poster = artistResult.images?.FirstOrDefault(x => x.coverType.Equals("poster", StringComparison.InvariantCultureIgnoreCase))?.url.ToHttpsUrl(); + info.FanArt = artistResult.images?.FirstOrDefault(x => x.coverType.Equals("fanart", StringComparison.InvariantCultureIgnoreCase))?.url.ToHttpsUrl(); info.Overview = artistResult.overview; } @@ -108,11 +108,11 @@ namespace Ombi.Core.Engine.V2 { if ((cover.thumbnails?.small ?? string.Empty).HasValue()) { - return new AlbumArt(cover.thumbnails.small); + return new AlbumArt(cover.thumbnails.small.ToHttpsUrl()); } if ((cover.thumbnails?.large ?? string.Empty).HasValue()) { - return new AlbumArt(cover.thumbnails.large); + return new AlbumArt(cover.thumbnails.large.ToHttpsUrl()); } } @@ -152,69 +152,69 @@ namespace Ombi.Core.Engine.V2 switch (relation.TypeId) { case RelationLinks.AllMusic: - links.AllMusic = relation.Url?.Resource; + links.AllMusic = relation.Url?.Resource.ToHttpsUrl(); break; case RelationLinks.BbcMusic: - links.BbcMusic = relation.Url?.Resource; + links.BbcMusic = relation.Url?.Resource.ToHttpsUrl(); break; case RelationLinks.Discogs: - links.Discogs = relation.Url?.Resource; + links.Discogs = relation.Url?.Resource.ToHttpsUrl(); break; case RelationLinks.Homepage: - links.HomePage = relation.Url?.Resource; + links.HomePage = relation.Url?.Resource.ToHttpsUrl(); break; case RelationLinks.Imdb: - links.Imdb = relation.Url?.Resource; + links.Imdb = relation.Url?.Resource.ToHttpsUrl(); break; case RelationLinks.LastFm: - links.LastFm = relation.Url?.Resource; + links.LastFm = relation.Url?.Resource.ToHttpsUrl(); break; case RelationLinks.MySpace: - links.MySpace = relation.Url?.Resource; + links.MySpace = relation.Url?.Resource.ToHttpsUrl(); break; case RelationLinks.OnlineCommunity: - links.OnlineCommunity = relation.Url?.Resource; + links.OnlineCommunity = relation.Url?.Resource.ToHttpsUrl(); break; case RelationLinks.SocialNetwork: if ((relation.Url?.Resource ?? string.Empty).Contains("twitter", CompareOptions.IgnoreCase)) { - links.Twitter = relation.Url?.Resource; + links.Twitter = relation.Url?.Resource.ToHttpsUrl(); } if ((relation.Url?.Resource ?? string.Empty).Contains("facebook", CompareOptions.IgnoreCase)) { - links.Facebook = relation.Url?.Resource; + links.Facebook = relation.Url?.Resource.ToHttpsUrl(); } if ((relation.Url?.Resource ?? string.Empty).Contains("instagram", CompareOptions.IgnoreCase)) { - links.Instagram = relation.Url?.Resource; + links.Instagram = relation.Url?.Resource.ToHttpsUrl(); } if ((relation.Url?.Resource ?? string.Empty).Contains("vk", CompareOptions.IgnoreCase)) { - links.Vk = relation.Url?.Resource; + links.Vk = relation.Url?.Resource.ToHttpsUrl(); } break; case RelationLinks.Streams: if ((relation.Url?.Resource ?? string.Empty).Contains("spotify", CompareOptions.IgnoreCase)) { - links.Spotify = relation.Url?.Resource; + links.Spotify = relation.Url?.Resource.ToHttpsUrl(); } if ((relation.Url?.Resource ?? string.Empty).Contains("deezer", CompareOptions.IgnoreCase)) { - links.Deezer = relation.Url?.Resource; + links.Deezer = relation.Url?.Resource.ToHttpsUrl(); } break; case RelationLinks.YouTube: - links.YouTube = relation.Url?.Resource; + links.YouTube = relation.Url?.Resource.ToHttpsUrl(); break; case RelationLinks.Download: if ((relation.Url?.Resource ?? string.Empty).Contains("google", CompareOptions.IgnoreCase)) { - links.Google = relation.Url?.Resource; + links.Google = relation.Url?.Resource.ToHttpsUrl(); } if ((relation.Url?.Resource ?? string.Empty).Contains("apple", CompareOptions.IgnoreCase)) { - links.Apple = relation.Url?.Resource; + links.Apple = relation.Url?.Resource.ToHttpsUrl(); } break; diff --git a/src/Ombi.Core/Engine/V2/TvSearchEngineV2.cs b/src/Ombi.Core/Engine/V2/TvSearchEngineV2.cs index 6ad512ff4..dd2ce22aa 100644 --- a/src/Ombi.Core/Engine/V2/TvSearchEngineV2.cs +++ b/src/Ombi.Core/Engine/V2/TvSearchEngineV2.cs @@ -1,11 +1,9 @@ using System; using AutoMapper; - using System.Collections.Generic; using System.Linq; using System.Security.Principal; using System.Threading.Tasks; - using Ombi.Core.Rule.Interfaces; using Ombi.Store.Repository.Requests; using Ombi.Core.Authentication; @@ -85,7 +83,7 @@ namespace Ombi.Core.Engine.V2 }; newSeason.Episodes.Add(new EpisodeRequests { - Url = e.url, + Url = e.url.ToHttpsUrl(), Title = e.name, AirDate = e.airstamp, EpisodeNumber = e.number, @@ -98,7 +96,7 @@ namespace Ombi.Core.Engine.V2 // We already have the season, so just add the episode season.Episodes.Add(new EpisodeRequests { - Url = e.url, + Url = e.url.ToHttpsUrl(), Title = e.name, AirDate = e.airstamp, EpisodeNumber = e.number, @@ -141,7 +139,7 @@ namespace Ombi.Core.Engine.V2 if (!string.IsNullOrEmpty(item.Images?.Medium)) { - item.Images.Medium = item.Images.Medium.Replace("http:", "https:"); + item.Images.Medium = item.Images.Medium.ToHttpsUrl(); } if (item.Cast?.Any() ?? false) @@ -150,7 +148,7 @@ namespace Ombi.Core.Engine.V2 { if (!string.IsNullOrEmpty(cast.Character?.Image?.Medium)) { - cast.Character.Image.Medium = cast.Character?.Image?.Medium.Replace("http:", "https:"); + cast.Character.Image.Medium = cast.Character?.Image?.Medium.ToHttpsUrl(); } } } @@ -168,9 +166,9 @@ namespace Ombi.Core.Engine.V2 return model; } - model.Trailer = result.Trailer?.AbsoluteUri ?? string.Empty; + model.Trailer = result.Trailer?.AbsoluteUri.ToHttpsUrl() ?? string.Empty; model.Certification = result.Certification; - model.Homepage = result.Homepage?.AbsoluteUri ?? string.Empty; + model.Homepage = result.Homepage?.AbsoluteUri.ToHttpsUrl() ?? string.Empty; } return model; } diff --git a/src/Ombi.Core/Helpers/TvShowRequestBuilder.cs b/src/Ombi.Core/Helpers/TvShowRequestBuilder.cs index 56d227ad0..8ccbed8fb 100644 --- a/src/Ombi.Core/Helpers/TvShowRequestBuilder.cs +++ b/src/Ombi.Core/Helpers/TvShowRequestBuilder.cs @@ -55,7 +55,7 @@ namespace Ombi.Core.Helpers FirstAir = dt; // For some reason the poster path is always http - PosterPath = ShowInfo.image?.medium.Replace("http:", "https:"); + PosterPath = ShowInfo.image?.medium.ToHttpsUrl(); return this; } @@ -113,7 +113,7 @@ namespace Ombi.Core.Helpers EpisodeNumber = ep.number, AirDate = FormatDate(ep.airdate), Title = ep.name, - Url = ep.url + Url = ep.url.ToHttpsUrl() } }, SeasonNumber = ep.season, @@ -126,7 +126,7 @@ namespace Ombi.Core.Helpers EpisodeNumber = ep.number, AirDate = FormatDate(ep.airdate), Title = ep.name, - Url = ep.url + Url = ep.url.ToHttpsUrl() }); } } @@ -146,7 +146,7 @@ namespace Ombi.Core.Helpers EpisodeNumber = ep.number, AirDate = FormatDate(ep.airdate), Title = ep.name, - Url = ep.url + Url = ep.url.ToHttpsUrl() }); } } @@ -170,7 +170,7 @@ namespace Ombi.Core.Helpers EpisodeNumber = ep.number, AirDate = FormatDate(ep.airdate), Title = ep.name, - Url = ep.url + Url = ep.url.ToHttpsUrl() }); } } @@ -200,7 +200,7 @@ namespace Ombi.Core.Helpers EpisodeNumber = ep.number, AirDate = FormatDate(ep.airdate), Title = ep.name, - Url = ep.url, + Url = ep.url.ToHttpsUrl() }); } } @@ -216,7 +216,7 @@ namespace Ombi.Core.Helpers EpisodeNumber = ep.number, AirDate = FormatDate(ep.airdate), Title = ep.name, - Url = ep.url, + Url = ep.url.ToHttpsUrl() }); seasonRequests.Add(newRequest); } diff --git a/src/Ombi.Helpers.Tests/UriHelperTests.cs b/src/Ombi.Helpers.Tests/UriHelperTests.cs index fd21e66f4..3e1b4aec2 100644 --- a/src/Ombi.Helpers.Tests/UriHelperTests.cs +++ b/src/Ombi.Helpers.Tests/UriHelperTests.cs @@ -62,7 +62,7 @@ namespace Ombi.Helpers.Tests var expected = (string)d.ExpectedResult; var args = d.Arguments.ToList(); args.Add(true); - var newExpected = expected.Replace("http://", "https://"); + var newExpected = expected.ToHttpsUrl(); if (args.Contains(80)) { newExpected = expected; @@ -99,7 +99,7 @@ namespace Ombi.Helpers.Tests } } args.Add(true); - var newExpected = expected.Replace("http://", "https://"); + var newExpected = expected.ToHttpsUrl(); if (args.Contains(80)) { newExpected = expected; diff --git a/src/Ombi.Helpers/StringHelper.cs b/src/Ombi.Helpers/StringHelper.cs index bd9ecb5bb..e918fd35a 100644 --- a/src/Ombi.Helpers/StringHelper.cs +++ b/src/Ombi.Helpers/StringHelper.cs @@ -131,7 +131,7 @@ namespace Ombi.Helpers public static string ToHttpsUrl(this string currentUrl) { - return currentUrl.Replace("http://", "https://"); + return currentUrl?.Replace("http://", "https://"); } } } \ No newline at end of file diff --git a/src/Ombi.Mapping/Profiles/TvProfile.cs b/src/Ombi.Mapping/Profiles/TvProfile.cs index 0e8264a71..8cb4e38b6 100644 --- a/src/Ombi.Mapping/Profiles/TvProfile.cs +++ b/src/Ombi.Mapping/Profiles/TvProfile.cs @@ -56,8 +56,10 @@ namespace Ombi.Mapping.Profiles .ForMember(dest => dest.Runtime, opts => opts.MapFrom(src => src.Runtime.ToString())) .ForMember(dest => dest.Title, opts => opts.MapFrom(src => src.Title)) .ForMember(dest => dest.Status, opts => opts.MapFrom(src => TraktEnumHelper.GetDescription(src.Status))) - .ForMember(dest => dest.Trailer, opts => opts.MapFrom(src => src.Trailer)) - .ForMember(dest => dest.Homepage, opts => opts.MapFrom(src => src.Homepage)); + .ForMember(dest => dest.Trailer, + opts => opts.MapFrom(src => src.Trailer.ToString().ToHttpsUrl())) + .ForMember(dest => dest.Homepage, + opts => opts.MapFrom(src => src.Homepage.ToString().ToHttpsUrl())); } } } \ No newline at end of file diff --git a/src/Ombi.Mapping/Profiles/TvProfileV2.cs b/src/Ombi.Mapping/Profiles/TvProfileV2.cs index 3a8aeeb62..5ee0645fd 100644 --- a/src/Ombi.Mapping/Profiles/TvProfileV2.cs +++ b/src/Ombi.Mapping/Profiles/TvProfileV2.cs @@ -30,7 +30,7 @@ namespace Ombi.Mapping.Profiles .ForMember(dest => dest.Banner, opts => opts.MapFrom(src => !string.IsNullOrEmpty(src.image.medium) - ? src.image.medium.Replace("http", "https") + ? src.image.medium.ToHttpsUrl() : string.Empty)) .ForMember(dest => dest.Status, opts => opts.MapFrom(src => src.status)); @@ -45,8 +45,8 @@ namespace Ombi.Mapping.Profiles .ForMember(dest => dest.Timezone, opts => opts.MapFrom(src => src.timezone)); CreateMap() - .ForMember(dest => dest.Medium, opts => opts.MapFrom(src => src.medium)) - .ForMember(dest => dest.Original, opts => opts.MapFrom(src => src.original)); + .ForMember(dest => dest.Medium, opts => opts.MapFrom(src => src.medium.ToHttpsUrl())) + .ForMember(dest => dest.Original, opts => opts.MapFrom(src => src.original.ToHttpsUrl())); CreateMap() .ForMember(dest => dest.Character, opts => opts.MapFrom(src => src.character)) @@ -58,7 +58,7 @@ namespace Ombi.Mapping.Profiles .ForMember(dest => dest.Id, opts => opts.MapFrom(src => src.id)) .ForMember(dest => dest.Name, opts => opts.MapFrom(src => src.name)) .ForMember(dest => dest.Image, opts => opts.MapFrom(src => src.image)) - .ForMember(dest => dest.Url, opts => opts.MapFrom(src => src.url)); + .ForMember(dest => dest.Url, opts => opts.MapFrom(src => src.url.ToHttpsUrl())); CreateMap() .ForMember(dest => dest.Person, opts => opts.MapFrom(src => src.person)) @@ -73,7 +73,7 @@ namespace Ombi.Mapping.Profiles CreateMap() .ForMember(dest => dest.Name, opts => opts.MapFrom(src => src.name)) .ForMember(dest => dest.Id, opts => opts.MapFrom(src => src.id)) - .ForMember(dest => dest.Url, opts => opts.MapFrom(src => src.url)) + .ForMember(dest => dest.Url, opts => opts.MapFrom(src => src.url.ToHttpsUrl())) .ForMember(dest => dest.Image, opts => opts.MapFrom(src => src.image)); CreateMap().ReverseMap(); From 0c34fbd0cdd6510f11fb1b78f71e1aff9d2dfa84 Mon Sep 17 00:00:00 2001 From: Victor Usoltsev Date: Thu, 8 Oct 2020 12:13:42 +1300 Subject: [PATCH 2/2] Adds a test for ToHttpsUrl extension. --- src/Ombi.Helpers.Tests/StringHelperTests.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Ombi.Helpers.Tests/StringHelperTests.cs b/src/Ombi.Helpers.Tests/StringHelperTests.cs index d03f926ff..80ca32c64 100644 --- a/src/Ombi.Helpers.Tests/StringHelperTests.cs +++ b/src/Ombi.Helpers.Tests/StringHelperTests.cs @@ -40,5 +40,14 @@ namespace Ombi.Helpers.Tests Assert.AreEqual(expectedUrl, sourceUrl.ToHttpsUrl(), "Should return the unchanged invalid URL"); } + + [Test] + public void ToHttpsUrl_ShouldReturnNull_NullUrl() + { + const string sourceUrl = null; + const string expectedUrl = null; + + Assert.AreEqual(expectedUrl, sourceUrl.ToHttpsUrl(), "Should return null for null URL"); + } } } \ No newline at end of file