Release 0.3.5: coordinate atomic Jellyfin Web injection
This commit is contained in:
1 parent
096c6be8c7
commit
8f2923cee6
5 files changed
+196
-26
No files matched your search
@@ -3,9 +3,10 @@
|
|||||||
Multilang is a Jellyfin plugin that lets different users see different metadata
|
Multilang is a Jellyfin plugin that lets different users see different metadata
|
||||||
languages without changing Jellyfin's stored metadata.
|
languages without changing Jellyfin's stored metadata.
|
||||||
|
|
||||||
The `main` branch is built for the Jellyfin 10.11.x stable series. The
|
Multilang targets Jellyfin 12.x on Linux x64 and is tested on Jellyfin 12.2.
|
||||||
`jellyfin12` branch targets Jellyfin 12.0 stable on Linux x64. The plugin remains
|
Development continues on the `main` branch. The final Jellyfin 10.11 release
|
||||||
beta software.
|
is 0.2.6; its source is archived on `jellyfin10` and is no longer maintained.
|
||||||
|
The plugin remains beta software.
|
||||||
|
|
||||||
## Upgrading to 0.2.5 or 0.3.4
|
## Upgrading to 0.2.5 or 0.3.4
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
[assembly: InternalsVisibleTo("Jellyfin.Plugin.Multilang.Tests")]
|
||||||
@@ -7,9 +7,9 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<GenerateDocumentationFile>false</GenerateDocumentationFile>
|
<GenerateDocumentationFile>false</GenerateDocumentationFile>
|
||||||
<Version>0.3.4</Version>
|
<Version>0.3.5</Version>
|
||||||
<AssemblyVersion>0.3.4.0</AssemblyVersion>
|
<AssemblyVersion>0.3.5.0</AssemblyVersion>
|
||||||
<FileVersion>0.3.4.0</FileVersion>
|
<FileVersion>0.3.5.0</FileVersion>
|
||||||
<Authors>ajp_anton</Authors>
|
<Authors>ajp_anton</Authors>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -20,22 +20,25 @@ internal static class WebScriptInjector
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var content = File.ReadAllText(indexPath);
|
var changed = UpdateIndex(indexPath, original =>
|
||||||
var block = $"{StartComment}\n{scriptTag}\n{EndComment}\n";
|
|
||||||
if (content.Contains(block, StringComparison.Ordinal))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
content = RemoveOldBlock(content);
|
|
||||||
var insertAt = content.IndexOf("</body>", StringComparison.OrdinalIgnoreCase);
|
|
||||||
if (insertAt < 0)
|
|
||||||
{
|
{
|
||||||
logger.LogWarning("Multilang inject: </body> tag not found in index.html");
|
var block = $"{StartComment}\n{scriptTag}\n{EndComment}\n";
|
||||||
return false;
|
if (original.Contains(block, StringComparison.Ordinal))
|
||||||
}
|
return original;
|
||||||
|
|
||||||
File.WriteAllText(indexPath, content.Insert(insertAt, block + Environment.NewLine), Encoding.UTF8);
|
var content = RemoveOldBlock(original);
|
||||||
logger.LogInformation("Multilang inject: updated index.html");
|
var insertAt = content.IndexOf("</body>", StringComparison.OrdinalIgnoreCase);
|
||||||
return true;
|
if (insertAt < 0)
|
||||||
|
{
|
||||||
|
logger.LogWarning("Multilang inject: </body> tag not found in index.html");
|
||||||
|
return original;
|
||||||
|
}
|
||||||
|
|
||||||
|
return content.Insert(insertAt, block + Environment.NewLine);
|
||||||
|
});
|
||||||
|
if (changed)
|
||||||
|
logger.LogInformation("Multilang inject: updated index.html");
|
||||||
|
return changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool RemoveInjected(string webRootPath)
|
public static bool RemoveInjected(string webRootPath)
|
||||||
@@ -47,13 +50,33 @@ internal static class WebScriptInjector
|
|||||||
if (!File.Exists(indexPath))
|
if (!File.Exists(indexPath))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
var content = File.ReadAllText(indexPath);
|
return UpdateIndex(indexPath, RemoveOldBlock);
|
||||||
var cleaned = RemoveOldBlock(content);
|
}
|
||||||
if (cleaned.Equals(content, StringComparison.Ordinal))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
File.WriteAllText(indexPath, cleaned, Encoding.UTF8);
|
private static bool UpdateIndex(string indexPath, Func<string, string> update)
|
||||||
return true;
|
{
|
||||||
|
// Shared with other web-injecting plugins, including Multilang and Watch Orders.
|
||||||
|
using var mutex = new Mutex(false, "Jellyfin.Web.IndexHtml");
|
||||||
|
if (!mutex.WaitOne(TimeSpan.FromSeconds(30)))
|
||||||
|
throw new TimeoutException("Timed out waiting to update Jellyfin Web index.html.");
|
||||||
|
|
||||||
|
var temporaryPath = indexPath + "." + Guid.NewGuid().ToString("N") + ".tmp";
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var original = File.ReadAllText(indexPath);
|
||||||
|
var updated = update(original);
|
||||||
|
if (updated == original)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
File.WriteAllText(temporaryPath, updated, Encoding.UTF8);
|
||||||
|
File.Move(temporaryPath, indexPath, overwrite: true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
mutex.ReleaseMutex();
|
||||||
|
File.Delete(temporaryPath);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string RemoveOldBlock(string content)
|
private static string RemoveOldBlock(string content)
|
||||||
|
|||||||
@@ -0,0 +1,143 @@
|
|||||||
|
using Jellyfin.Plugin.Multilang.Services.Injection;
|
||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
|
||||||
|
namespace Jellyfin.Plugin.Multilang.Tests;
|
||||||
|
|
||||||
|
public sealed class WebScriptInjectorTests : IDisposable
|
||||||
|
{
|
||||||
|
private const string Shell = "<!doctype html><html><head></head><body><div id=\"root\"></div></body></html>";
|
||||||
|
private const string OtherBlock = "<!-- BEGIN Watch Orders Plugin -->\n<script src=\"other.js\"></script>\n<!-- END Watch Orders Plugin -->\n";
|
||||||
|
private readonly string _root = Path.Combine(Path.GetTempPath(), "web-inject-test-" + Guid.NewGuid().ToString("N"));
|
||||||
|
private string Index => Path.Combine(_root, "index.html");
|
||||||
|
|
||||||
|
public WebScriptInjectorTests()
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(_root);
|
||||||
|
File.WriteAllText(Index, Shell);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ReplacingShorterScriptPreservesOtherPluginAndRepeatedInjectionDoesNotWrite()
|
||||||
|
{
|
||||||
|
File.WriteAllText(Index, Shell.Replace("</body>", OtherBlock + "</body>"));
|
||||||
|
Inject("a-long-old-script-version.js");
|
||||||
|
Inject("v2.js");
|
||||||
|
var content = File.ReadAllText(Index);
|
||||||
|
Assert.DoesNotContain("a-long-old", content);
|
||||||
|
Assert.Contains(OtherBlock, content);
|
||||||
|
Assert.Contains("v2.js", content);
|
||||||
|
Assert.EndsWith("</body></html>", content.TrimEnd());
|
||||||
|
Assert.Equal(1, content.Split("<!-- BEGIN Multilang Plugin -->").Length - 1);
|
||||||
|
var timestamp = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||||
|
File.SetLastWriteTimeUtc(Index, timestamp);
|
||||||
|
Inject("v2.js");
|
||||||
|
Assert.Equal(timestamp, File.GetLastWriteTimeUtc(Index));
|
||||||
|
Assert.Single(Directory.GetFiles(_root));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RemovalPreservesOtherPlugin()
|
||||||
|
{
|
||||||
|
var original = Shell.Replace("</body>", OtherBlock + "</body>");
|
||||||
|
File.WriteAllText(Index, original);
|
||||||
|
Inject("plugin.js");
|
||||||
|
WebScriptInjector.RemoveInjected(_root);
|
||||||
|
var content = File.ReadAllText(Index);
|
||||||
|
Assert.DoesNotContain("<!-- BEGIN Multilang Plugin -->", content);
|
||||||
|
Assert.Contains(OtherBlock, content);
|
||||||
|
Assert.EndsWith("</body></html>", content.TrimEnd());
|
||||||
|
WebScriptInjector.RemoveInjected(_root);
|
||||||
|
Assert.Equal(content, File.ReadAllText(Index));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MissingBodyEndLeavesFileUntouched()
|
||||||
|
{
|
||||||
|
File.WriteAllText(Index, "<html><body>incomplete");
|
||||||
|
Inject("plugin.js");
|
||||||
|
Assert.Equal("<html><body>incomplete", File.ReadAllText(Index));
|
||||||
|
Assert.Single(Directory.GetFiles(_root));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(false)]
|
||||||
|
[InlineData(true)]
|
||||||
|
public async Task SharedLockProtectsWholeReadModifyWriteIncludingRemoval(bool remove)
|
||||||
|
{
|
||||||
|
Inject("plugin.js");
|
||||||
|
using var entered = new ManualResetEventSlim();
|
||||||
|
using var release = new ManualResetEventSlim();
|
||||||
|
var other = Task.Run(() =>
|
||||||
|
{
|
||||||
|
using var mutex = new Mutex(false, "Jellyfin.Web.IndexHtml");
|
||||||
|
Assert.True(mutex.WaitOne(TimeSpan.FromSeconds(10)));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var content = File.ReadAllText(Index);
|
||||||
|
entered.Set();
|
||||||
|
Assert.True(release.Wait(TimeSpan.FromSeconds(10)));
|
||||||
|
File.WriteAllText(Index, content.Replace("</body>", OtherBlock + "</body>"));
|
||||||
|
}
|
||||||
|
finally { mutex.ReleaseMutex(); }
|
||||||
|
});
|
||||||
|
Task ours = Task.CompletedTask;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Assert.True(entered.Wait(TimeSpan.FromSeconds(10)));
|
||||||
|
using var calling = new ManualResetEventSlim();
|
||||||
|
ours = Task.Run(() =>
|
||||||
|
{
|
||||||
|
calling.Set();
|
||||||
|
if (remove) WebScriptInjector.RemoveInjected(_root);
|
||||||
|
else Inject("updated.js");
|
||||||
|
});
|
||||||
|
Assert.True(calling.Wait(TimeSpan.FromSeconds(10)));
|
||||||
|
await Task.Delay(100);
|
||||||
|
Assert.False(ours.IsCompleted);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
release.Set();
|
||||||
|
await Task.WhenAll(other, ours).WaitAsync(TimeSpan.FromSeconds(10));
|
||||||
|
}
|
||||||
|
var final = File.ReadAllText(Index);
|
||||||
|
Assert.Contains(OtherBlock, final);
|
||||||
|
Assert.Equal(!remove, final.Contains("<!-- BEGIN Multilang Plugin -->"));
|
||||||
|
if (!remove) Assert.Contains("updated.js", final);
|
||||||
|
Assert.EndsWith("</body></html>", final.TrimEnd());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ReadersNeverSeeTruncatedHtmlDuringUpdates()
|
||||||
|
{
|
||||||
|
using var stop = new CancellationTokenSource();
|
||||||
|
using var reading = new ManualResetEventSlim();
|
||||||
|
var reader = Task.Run(() =>
|
||||||
|
{
|
||||||
|
reading.Set();
|
||||||
|
while (!stop.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
var content = File.ReadAllText(Index);
|
||||||
|
Assert.StartsWith("<!doctype html>", content);
|
||||||
|
Assert.EndsWith("</body></html>", content.TrimEnd());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Assert.True(reading.Wait(TimeSpan.FromSeconds(10)));
|
||||||
|
for (var i = 0; i < 30; i++)
|
||||||
|
Inject(new string('x', i % 2 == 0 ? 131072 : 1) + i + ".js");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
stop.Cancel();
|
||||||
|
await reader.WaitAsync(TimeSpan.FromSeconds(10));
|
||||||
|
}
|
||||||
|
Assert.Single(Directory.GetFiles(_root));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Inject(string source)
|
||||||
|
=> WebScriptInjector.EnsureInjected(_root, $"<script src=\"{source}\"></script>", NullLogger.Instance);
|
||||||
|
|
||||||
|
public void Dispose() => Directory.Delete(_root, recursive: true);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user