using System;
using System.Threading.Tasks;
using Jellyfin.Plugin.JFVR.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
namespace Jellyfin.Plugin.JFVR.Controllers;
///
/// Serves the JFVR web bundle and a small status endpoint.
///
/// The bundle is anonymous on purpose: the injected tag lives in
/// index.html, which the browser fetches before the user has
/// authenticated, and the payload is the same JavaScript that is already public
/// in the project repository.
///
[ApiController]
[Route("JFVR")]
public class JfvrController : ControllerBase
{
private readonly BundleProvider _bundle;
///
/// Initializes a new instance of the class.
///
/// Embedded web bundle.
public JfvrController(BundleProvider bundle)
{
_bundle = bundle;
}
///
/// Returns the JFVR web bundle.
///
/// The bundle as JavaScript.
[HttpGet("jfvr.js")]
[AllowAnonymous]
[Produces("application/javascript")]
public ActionResult GetBundle()
{
var etag = "\"" + _bundle.Hash + "\"";
// The URL is versioned with ?v=, so the body for a given URL never
// changes: it is safe to cache forever, and an update always produces a
// new URL.
Response.Headers[HeaderNames.CacheControl] = "public, max-age=31536000, immutable";
Response.Headers[HeaderNames.ETag] = etag;
if (Request.Headers[HeaderNames.IfNoneMatch].ToString().Contains(_bundle.Hash, StringComparison.Ordinal))
{
return StatusCode(304);
}
return File(_bundle.Bytes, "application/javascript; charset=utf-8");
}
///
/// Reports which bundle the server is serving and whether injection is on.
/// Handy for verifying a deployment without opening DevTools on the Quest.
///
/// A small JSON status object.
[HttpGet("status")]
[AllowAnonymous]
public ActionResult