using System;
using System.IO;
using System.Security.Cryptography;
namespace Jellyfin.Plugin.JFVR.Services;
///
/// Loads the JFVR web bundle that is embedded in this assembly and exposes it
/// together with a short content hash.
///
/// The hash is the cache-busting token in the injected
/// <script src="../JFVR/jfvr.js?v=HASH"> tag, so a new plugin build
/// always forces browsers to refetch the bundle. It is also the same
/// md5[:6] version string the in-page HUD reports, which makes "which JS
/// is the Quest actually running?" answerable from both sides.
///
public sealed class BundleProvider
{
///
/// Logical name of the embedded resource, set explicitly in the .csproj so it
/// does not depend on the project's folder layout.
///
internal const string ResourceName = "Jellyfin.Plugin.JFVR.Web.jfvr.js";
///
/// Initializes a new instance of the class.
///
///
/// Thrown when the bundle was not embedded, which means the build is broken.
///
public BundleProvider()
{
using var stream = typeof(BundleProvider).Assembly.GetManifestResourceStream(ResourceName);
if (stream is null)
{
throw new InvalidOperationException(
$"Embedded resource '{ResourceName}' is missing from {typeof(BundleProvider).Assembly.FullName}.");
}
using var buffer = new MemoryStream();
stream.CopyTo(buffer);
Bytes = buffer.ToArray();
Hash = Convert.ToHexString(MD5.HashData(Bytes)).ToLowerInvariant()[..8];
}
///
/// Gets the raw bundle bytes.
///
public byte[] Bytes { get; }
///
/// Gets the short (8 hex chars) MD5 of .
///
public string Hash { get; }
}