Release 0.2.6: coordinate atomic Jellyfin Web injection
This commit is contained in:
1 parent
e36775f1a3
commit
6d4194c511
5 files changed
+196
-25
No files matched your search
@@ -20,22 +20,25 @@ internal static class WebScriptInjector
|
||||
return false;
|
||||
}
|
||||
|
||||
var content = File.ReadAllText(indexPath);
|
||||
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)
|
||||
var changed = UpdateIndex(indexPath, original =>
|
||||
{
|
||||
logger.LogWarning("Multilang inject: </body> tag not found in index.html");
|
||||
return false;
|
||||
}
|
||||
var block = $"{StartComment}\n{scriptTag}\n{EndComment}\n";
|
||||
if (original.Contains(block, StringComparison.Ordinal))
|
||||
return original;
|
||||
|
||||
File.WriteAllText(indexPath, content.Insert(insertAt, block + Environment.NewLine), Encoding.UTF8);
|
||||
logger.LogInformation("Multilang inject: updated index.html");
|
||||
return true;
|
||||
var content = RemoveOldBlock(original);
|
||||
var insertAt = content.IndexOf("</body>", StringComparison.OrdinalIgnoreCase);
|
||||
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)
|
||||
@@ -47,13 +50,33 @@ internal static class WebScriptInjector
|
||||
if (!File.Exists(indexPath))
|
||||
return false;
|
||||
|
||||
var content = File.ReadAllText(indexPath);
|
||||
var cleaned = RemoveOldBlock(content);
|
||||
if (cleaned.Equals(content, StringComparison.Ordinal))
|
||||
return false;
|
||||
return UpdateIndex(indexPath, RemoveOldBlock);
|
||||
}
|
||||
|
||||
File.WriteAllText(indexPath, cleaned, Encoding.UTF8);
|
||||
return true;
|
||||
private static bool UpdateIndex(string indexPath, Func<string, string> update)
|
||||
{
|
||||
// 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)
|
||||
|
||||
Reference in new issue
Block a user