From ab8e5cca7ee154c4f52681fe9f639cfc69d4f10f Mon Sep 17 00:00:00 2001 From: msdeibel Date: Sat, 12 Oct 2019 10:00:26 +0200 Subject: [PATCH] Fixes issue #3195 The new string extension method ToHttpsUrl ensures that URLs starting with "https" are no longer turned into "httpss" The commit also replaces all occurances of the error prone .Replace("http", "https") in the whole solution. --- src/Ombi.Core/Engine/MusicSearchEngine.cs | 9 ++-- src/Ombi.Helpers.Tests/StringHelperTests.cs | 44 ++++++++++++++++++++ src/Ombi.Helpers/StringHelper.cs | 5 +++ src/Ombi.Mapping/Profiles/TvProfile.cs | 4 +- src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs | 4 +- 5 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 src/Ombi.Helpers.Tests/StringHelperTests.cs diff --git a/src/Ombi.Core/Engine/MusicSearchEngine.cs b/src/Ombi.Core/Engine/MusicSearchEngine.cs index da41d5bf1..a9af03ecf 100644 --- a/src/Ombi.Core/Engine/MusicSearchEngine.cs +++ b/src/Ombi.Core/Engine/MusicSearchEngine.cs @@ -17,6 +17,7 @@ using Ombi.Api.Lidarr.Models; using Ombi.Core.Authentication; using Ombi.Core.Settings; using Ombi.Helpers; +using Ombi.Core.Helpers; using Ombi.Settings.Settings.Models; using Ombi.Settings.Settings.Models.External; using Ombi.Store.Entities; @@ -166,7 +167,7 @@ namespace Ombi.Core.Engine Rating = a.ratings?.value ?? 0m, ReleaseDate = a.releaseDate, Title = a.title, - Disk = a.images?.FirstOrDefault(x => x.coverType.Equals("disc"))?.url?.Replace("http", "https"), + Disk = a.images?.FirstOrDefault(x => x.coverType.Equals("disc"))?.url?.ToHttpsUrl(), Genres = a.genres, AlbumType = a.albumType, ArtistName = a.artist.artistName, @@ -187,7 +188,7 @@ namespace Ombi.Core.Engine //vm.ArtistName = a.artist?.artistName; } - vm.Cover = a.images?.FirstOrDefault(x => x.coverType.Equals("cover"))?.url?.Replace("http", "https"); + vm.Cover = a.images?.FirstOrDefault(x => x.coverType.Equals("cover"))?.url?.ToHttpsUrl(); await Rules.StartSpecificRules(vm, SpecificRules.LidarrAlbum); @@ -205,7 +206,7 @@ namespace Ombi.Core.Engine Rating = a.ratings?.value ?? 0m, ReleaseDate = a.releaseDate, Title = a.title, - Disk = a.images?.FirstOrDefault(x => x.coverType.Equals("disc"))?.url?.Replace("http", "https"), + Disk = a.images?.FirstOrDefault(x => x.coverType.Equals("disc"))?.url?.ToHttpsUrl(), Genres = a.genres }; if (a.artistId > 0) @@ -223,7 +224,7 @@ namespace Ombi.Core.Engine vm.ArtistName = a.artist?.artistName; } - vm.Cover = a.images?.FirstOrDefault(x => x.coverType.Equals("cover"))?.url?.Replace("http", "https"); + vm.Cover = a.images?.FirstOrDefault(x => x.coverType.Equals("cover"))?.url?.ToHttpsUrl(); if (vm.Cover.IsNullOrEmpty()) { vm.Cover = a.remoteCover; diff --git a/src/Ombi.Helpers.Tests/StringHelperTests.cs b/src/Ombi.Helpers.Tests/StringHelperTests.cs new file mode 100644 index 000000000..d03f926ff --- /dev/null +++ b/src/Ombi.Helpers.Tests/StringHelperTests.cs @@ -0,0 +1,44 @@ +using NUnit.Framework; + +namespace Ombi.Helpers.Tests +{ + [TestFixture] + public class StringHelperTests + { + [Test] + public void ToHttpsUrl_ShouldReturnsHttpsUrl_HttpUrl() + { + var sourceUrl = "http://www.test.url"; + var expectedUrl = "https://www.test.url"; + + Assert.AreEqual(expectedUrl, sourceUrl.ToHttpsUrl(), "Should return the source URL as https"); + } + + [Test] + public void ToHttpsUrl_ShouldReturnsUnchangedUrl_HttpsUrl() + { + var sourceUrl = "https://www.test.url"; + var expectedUrl = "https://www.test.url"; + + Assert.AreEqual(expectedUrl, sourceUrl.ToHttpsUrl(), "Should return the unchanged https URL"); + } + + [Test] + public void ToHttpsUrl_ShouldReturnsUnchangedUrl_NonHttpUrl() + { + var sourceUrl = "ftp://www.test.url"; + var expectedUrl = "ftp://www.test.url"; + + Assert.AreEqual(expectedUrl, sourceUrl.ToHttpsUrl(), "Should return the unchanged non-http URL"); + } + + [Test] + public void ToHttpsUrl_ShouldReturnsUnchangedUrl_InvalidUrl() + { + var sourceUrl = "http:/www.test.url"; + var expectedUrl = "http:/www.test.url"; + + Assert.AreEqual(expectedUrl, sourceUrl.ToHttpsUrl(), "Should return the unchanged invalid URL"); + } + } +} \ No newline at end of file diff --git a/src/Ombi.Helpers/StringHelper.cs b/src/Ombi.Helpers/StringHelper.cs index 68a29e848..bd9ecb5bb 100644 --- a/src/Ombi.Helpers/StringHelper.cs +++ b/src/Ombi.Helpers/StringHelper.cs @@ -128,5 +128,10 @@ namespace Ombi.Helpers { return string.Concat(str.Where(c => !chars.Contains(c))); } + + public static string ToHttpsUrl(this string currentUrl) + { + 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 4808a905b..db5213b71 100644 --- a/src/Ombi.Mapping/Profiles/TvProfile.cs +++ b/src/Ombi.Mapping/Profiles/TvProfile.cs @@ -29,7 +29,7 @@ namespace Ombi.Mapping.Profiles .ForMember(dest => dest.Runtime, opts => opts.MapFrom(src => src.show.runtime.ToString())) .ForMember(dest => dest.SeriesId, opts => opts.MapFrom(src => src.show.id)) .ForMember(dest => dest.Title, opts => opts.MapFrom(src => src.show.name)) - .ForMember(dest => dest.Banner, opts => opts.MapFrom(src => !string.IsNullOrEmpty(src.show.image.medium) ? src.show.image.medium.Replace("http", "https") : string.Empty)) + .ForMember(dest => dest.Banner, opts => opts.MapFrom(src => !string.IsNullOrEmpty(src.show.image.medium) ? src.show.image.medium.ToHttpsUrl() : string.Empty)) .ForMember(dest => dest.Status, opts => opts.MapFrom(src => src.show.status)); CreateMap() @@ -46,7 +46,7 @@ namespace Ombi.Mapping.Profiles .ForMember(dest => dest.Title, opts => opts.MapFrom(src => src.name)) .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)); diff --git a/src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs b/src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs index f3756bb0f..710c39595 100644 --- a/src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs +++ b/src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs @@ -682,7 +682,7 @@ namespace Ombi.Schedule.Jobs.Ombi var banner = info.image?.original; if (!string.IsNullOrEmpty(banner)) { - banner = banner.Replace("http", "https"); // Always use the Https banners + banner = banner.ToHttpsUrl(); // Always use the Https banners } var tvInfo = await _movieApi.GetTVInfo(t.TheMovieDbId); @@ -804,7 +804,7 @@ namespace Ombi.Schedule.Jobs.Ombi var banner = info.image?.original; if (!string.IsNullOrEmpty(banner)) { - banner = banner.Replace("http", "https"); // Always use the Https banners + banner = banner.ToHttpsUrl(); // Always use the Https banners } var tvInfo = await _movieApi.GetTVInfo(t.TheMovieDbId);