diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Api/JCoverSharedController.cs | 9 | ||||
-rw-r--r-- | Api/JCoverStaticProvider.cs | 40 | ||||
-rw-r--r-- | Api/ScriptInjector.cs | 74 | ||||
-rw-r--r-- | Api/coverscript.js | 64 | ||||
-rw-r--r-- | Folder.DotSettings.user | 5 | ||||
-rw-r--r-- | Jellyfin.Plugin.JCoverXtremePro.csproj | 6 | ||||
-rw-r--r-- | Plugin.cs | 12 | ||||
-rw-r--r-- | SeriesImageProvider.cs | 64 | ||||
-rwxr-xr-x | develop.sh | 7 | ||||
-rw-r--r-- | docker-compose.yml | 16 |
11 files changed, 293 insertions, 6 deletions
@@ -2,6 +2,6 @@ bin obj .idea/ - +testenv diff --git a/Api/JCoverSharedController.cs b/Api/JCoverSharedController.cs new file mode 100644 index 0000000..a5eecc1 --- /dev/null +++ b/Api/JCoverSharedController.cs @@ -0,0 +1,9 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Jellyfin.Plugin.JCoverXtremePro.Api; + +[ApiController] +[Route("JCoverXtreme")] +public class JCoverSharedController : ControllerBase +{ +}
\ No newline at end of file diff --git a/Api/JCoverStaticProvider.cs b/Api/JCoverStaticProvider.cs new file mode 100644 index 0000000..e4587ba --- /dev/null +++ b/Api/JCoverStaticProvider.cs @@ -0,0 +1,40 @@ +using Microsoft.Extensions.Logging; + +namespace Jellyfin.Plugin.JCoverXtremePro.Api; + +using System.Reflection; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +/// <summary> +/// Static file server for the JavaScript snippet injected by <see cref="ScriptInjector"/> +/// </summary> +[ApiController] +[Route("JCoverXtremeProStatic")] +public class JCoverStaticProvider : ControllerBase +{ + private readonly Assembly assembly; + private readonly string scriptPath; + + public JCoverStaticProvider() + { + assembly = Assembly.GetExecutingAssembly(); + scriptPath = GetType().Namespace + ".coverscript.js"; + } + + [HttpGet("ClientScript")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [Produces("application/javascript")] + public ActionResult GetClientScript() + { + Plugin.Logger.LogInformation($"Requesting ClientScript {scriptPath}"); + var scriptStream = assembly.GetManifestResourceStream(scriptPath); + if (scriptStream == null) + { + return NotFound(); + } + + return File(scriptStream, "application/javascript"); + } +}
\ No newline at end of file diff --git a/Api/ScriptInjector.cs b/Api/ScriptInjector.cs new file mode 100644 index 0000000..d6b1865 --- /dev/null +++ b/Api/ScriptInjector.cs @@ -0,0 +1,74 @@ +namespace Jellyfin.Plugin.JCoverXtremePro.Api; + +using System; +using System.IO; +using System.Text.RegularExpressions; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Controller.Configuration; +using Microsoft.Extensions.Logging; + +/// <summary> +/// Utility for injecting a JavaScript script tag into the Jellyfin web frontend. +/// </summary> +public static class ScriptInjector +{ + public static void PerformInjection( + IApplicationPaths applicationPaths, + IServerConfigurationManager configurationManager + ) + { + var indexHtmlFilePath = Path.Combine(applicationPaths.WebPath, "index.html"); + if (!File.Exists(indexHtmlFilePath)) + { + Plugin.Logger.LogWarning("Could not find index html file"); + return; + } + + var html = File.ReadAllText(indexHtmlFilePath); + var snippet = GetInjectedSnippet(GetHTTPBasePath(configurationManager)); + if (html.Contains(snippet, StringComparison.InvariantCulture)) + { + Plugin.Logger.LogInformation("Not injecting existing HTML snippet."); + return; + } + + html = Regex.Replace(html, $"<script[^>]*guid=\"{Plugin.GUID}\"[^>]*></script>", string.Empty); + var bodyEnd = html.LastIndexOf("</body>", StringComparison.InvariantCulture); + if (bodyEnd < 0) + { + Plugin.Logger.LogError("Could not find end of body to inject script"); + return; + } + + html = html.Insert(bodyEnd, snippet); + try + { + File.WriteAllText(indexHtmlFilePath, html); + Plugin.Logger.LogInformation("Injected index.html"); + } + catch (Exception e) + { + Plugin.Logger.LogError(e, "Failed to write patched index.html"); + } + } + + public static string GetHTTPBasePath(IServerConfigurationManager configurationManager) + { + var networkConfig = configurationManager.GetConfiguration("network"); + var configType = networkConfig.GetType(); + var baseUrlField = configType.GetProperty("BaseUrl"); + var baseUrl = baseUrlField!.GetValue(networkConfig)!.ToString()!.Trim('/'); + return baseUrl; + } + + public static string GetScriptUrl(string basePath) + { + return basePath + "/JCoverXtremeProStatic/ClientScript"; + } + + public static string GetInjectedSnippet(string basePath) + { + return + $"<script guid=\"{Plugin.GUID}\" plugin=\"{Plugin.Instance!.Name}\" src=\"{GetScriptUrl(basePath)}\" defer></script>"; + } +}
\ No newline at end of file diff --git a/Api/coverscript.js b/Api/coverscript.js new file mode 100644 index 0000000..24f3c34 --- /dev/null +++ b/Api/coverscript.js @@ -0,0 +1,64 @@ +(function () { + /** + * + * @param {HTMLElement} element + * @param {string} selector + * @returns {HTMLElement | null} + */ + function findParent(element, selector) { + let p = element + while (p) { + if (p.matches(selector)) return p + p = p.parentNode + } + return null + } + + const injectionMarker = "JCoverXtremePro-injection-marker"; + + /** + * + * @param {HTMLElement} cloneFrom + * @return {HTMLElement} + */ + function createDownloadSeriesButton(cloneFrom) { + /*<button is="paper-icon-button-light" class="btnDownloadRemoteImage autoSize paper-icon-button-light" raised"="" title="Download"><span class="material-icons cloud_download" aria-hidden="true"></span></button>*/ + //import LayersIcon from '@mui/icons-material/Layers'; + //import CloudDownloadIcon from '@mui/icons-material/CloudDownload'; + const element = document.createElement("button") + element.classList.add(...cloneFrom.classList) + element.classList.add(injectionMarker) + element.title = "Download Series" + const icon = document.createElement("span") + icon.classList.add("material-icons", "burst_mode") + icon.setAttribute("aria-hidden", "true") + element.appendChild(icon) + element.addEventListener("click", ev => { + ev.preventDefault() + + alert("YOU HAVE JUST BEEN INTERDICTED BY THE JCOVERXTREMEPRO SERIES DOWNLOADIFICATOR") + }) + return element + } + + const observer = new MutationObserver(() => { + console.log("JCoverXtremePro observation was triggered!") + console.log("Listing all download buttons") + /** + * @type {NodeListOf<Element>} + */ + const buttons = document.querySelectorAll(".imageEditorCard .cardFooter .btnDownloadRemoteImage") + + buttons.forEach(element => { + const downloadRowContainer = findParent(element, ".cardText") + if (downloadRowContainer.querySelector(`.${injectionMarker}`)) return + // TODO: extract information about the series, and check if this is at all viable + downloadRowContainer.appendChild(createDownloadSeriesButton(element)) + }) + + }) + observer.observe(document.body, {// TODO: selectively observe the body if at all possible + subtree: true, + childList: true, + }); +})()
\ No newline at end of file diff --git a/Folder.DotSettings.user b/Folder.DotSettings.user new file mode 100644 index 0000000..87f4e76 --- /dev/null +++ b/Folder.DotSettings.user @@ -0,0 +1,5 @@ +<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> + <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ABaseItem_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Ff5158e2ef5269abc9726c67e850a8988a202b4911c07d9781389d71e7e227_003FBaseItem_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> + <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AControllerBase_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fcf607849fce840fcb02ecfccf47e7bce1dc400_003F86_003F5f98b882_003FControllerBase_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> + <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AEpisode_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F1badb499b414e42f148749e92584cbf5d72618b72d771344ad7f7a053edf693_003FEpisode_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> + <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AMovie_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F1e53cde0ae407da5e0d8b3675976ee156dbd2d3335bf707cb079be17fa471_003FMovie_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>
\ No newline at end of file diff --git a/Jellyfin.Plugin.JCoverXtremePro.csproj b/Jellyfin.Plugin.JCoverXtremePro.csproj index 8455ca0..33412ff 100644 --- a/Jellyfin.Plugin.JCoverXtremePro.csproj +++ b/Jellyfin.Plugin.JCoverXtremePro.csproj @@ -1,7 +1,7 @@ <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> - <TargetFramework>net6.0</TargetFramework> + <TargetFramework>net8.0</TargetFramework> <RootNamespace>Jellyfin.Plugin.JCoverXtremePro</RootNamespace> <GenerateDocumentationFile>true</GenerateDocumentationFile> <TreatWarningsAsErrors>false</TreatWarningsAsErrors> @@ -13,6 +13,9 @@ <ItemGroup> <PackageReference Include="Jellyfin.Controller" Version="10.8.13"/> <PackageReference Include="Jellyfin.Model" Version="10.8.13"/> + <Reference Include="Jellyfin.Api"> + <HintPath>/usr/lib/jellyfin/Jellyfin.Api.dll</HintPath> + </Reference> </ItemGroup> <ItemGroup> @@ -24,6 +27,7 @@ <ItemGroup> <None Remove="Configuration\configPage.html"/> <EmbeddedResource Include="Configuration\configPage.html"/> + <EmbeddedResource Include="Api\coverscript.js"/> </ItemGroup> </Project> @@ -1,10 +1,13 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.IO; using System.Net.Http; +using Jellyfin.Plugin.JCoverXtremePro.Api; using Jellyfin.Plugin.JellyFed.Configuration; using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Plugins; +using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Plugins; @@ -18,18 +21,19 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages public Plugin( IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer, ILibraryManager libraryManager, ILogger<Plugin> logger, - IHttpClientFactory httpClientFactory + IHttpClientFactory httpClientFactory, + IServerConfigurationManager configurationManager ) : base(applicationPaths, xmlSerializer) { - logger.LogInformation("Loaded plugin with library manager {}", libraryManager); MediuxDownloader.instance = new MediuxDownloader(httpClientFactory); Instance = this; Logger = logger; + ScriptInjector.PerformInjection(applicationPaths, configurationManager); } public override string Name => "JCoverXtremePro"; - - public override Guid Id => Guid.Parse("f3e43e23-4b28-4b2f-a29d-37267e2ea2e2"); + public static Guid GUID = Guid.Parse("f3e43e23-4b28-4b2f-a29d-37267e2ea2e2"); + public override Guid Id => GUID; public static Plugin? Instance { get; private set; } diff --git a/SeriesImageProvider.cs b/SeriesImageProvider.cs new file mode 100644 index 0000000..7f48223 --- /dev/null +++ b/SeriesImageProvider.cs @@ -0,0 +1,64 @@ +using Microsoft.Extensions.Logging; + +namespace Jellyfin.Plugin.JCoverXtremePro; + +using System.Collections.Generic; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Entities.TV; +using MediaBrowser.Controller.Providers; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Providers; + +public class SeriesImageProvider + : IRemoteImageProvider, IHasOrder +{ + public bool Supports(BaseItem item) + { + return item is Episode or Series; + } + + public string Name => "Mediux Series"; + + public IEnumerable<ImageType> GetSupportedImages(BaseItem item) + { + return + [ + ImageType.Primary + ]; + } + + public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken) + { + // TODO: hadnle episodes + if (item is Series series) + { + return await HandleSeries(series, cancellationToken); + } + + return []; + } + + public async Task<IEnumerable<RemoteImageInfo>> HandleSeries(Series series, CancellationToken token) + { + var tmdbId = series.GetProviderId(MetadataProvider.Tmdb); + if (tmdbId == null) + { + return []; // TODO: handle missing id + } + + var metadata = await MediuxDownloader.instance.GetMediuxMetadata("https://mediux.pro/shows/" + tmdbId) + .ConfigureAwait(false); + Plugin.Logger.LogInformation("JSON: " + metadata); + return []; + } + + public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) + { + return MediuxDownloader.instance.DownloadFile(url); + } + + public int Order => 0; +}
\ No newline at end of file diff --git a/develop.sh b/develop.sh new file mode 100755 index 0000000..440ad42 --- /dev/null +++ b/develop.sh @@ -0,0 +1,7 @@ +#!/bin/bash +set -euo pipefail + +dotnet build +sudo cp ./bin/Debug/net8.0/Jellyfin.Plugin.JCoverXtremePro.* testenv/config/plugins/JCoverXtremePro +docker compose up + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9a6ade5 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,16 @@ + +services: + jellyfin: + image: jellyfin/jellyfin + network_mode: host + volumes: + - ./testenv/config:/config + - ./testenv/cache:/cache + - ./testenv/media:/media + develop: + watch: + - action: sync+restart + path: ./bin/Debug/net8.0/ + target: /config/plugins/JCoverXtremePro/ + + |