You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.6 KiB
67 lines
2.6 KiB
6 years ago
|
using System.Text;
|
||
3 years ago
|
using Microsoft.AspNetCore.Authorization;
|
||
|
using Microsoft.AspNetCore.Mvc;
|
||
5 years ago
|
using NzbDrone.Common;
|
||
6 years ago
|
using NzbDrone.Common.EnvironmentInfo;
|
||
|
using NzbDrone.Core.Analytics;
|
||
|
using NzbDrone.Core.Configuration;
|
||
|
|
||
4 years ago
|
namespace Readarr.Http.Frontend
|
||
6 years ago
|
{
|
||
3 years ago
|
[Authorize(Policy = "UI")]
|
||
|
[ApiController]
|
||
|
public class InitializeJsController : Controller
|
||
6 years ago
|
{
|
||
|
private readonly IConfigFileProvider _configFileProvider;
|
||
|
private readonly IAnalyticsService _analyticsService;
|
||
|
|
||
6 years ago
|
private static string _apiKey;
|
||
|
private static string _urlBase;
|
||
|
private string _generatedContent;
|
||
|
|
||
3 years ago
|
public InitializeJsController(IConfigFileProvider configFileProvider,
|
||
|
IAnalyticsService analyticsService)
|
||
6 years ago
|
{
|
||
|
_configFileProvider = configFileProvider;
|
||
|
_analyticsService = analyticsService;
|
||
|
|
||
6 years ago
|
_apiKey = configFileProvider.ApiKey;
|
||
|
_urlBase = configFileProvider.UrlBase;
|
||
6 years ago
|
}
|
||
|
|
||
3 years ago
|
[HttpGet("/initialize.js")]
|
||
|
public IActionResult Index()
|
||
6 years ago
|
{
|
||
4 years ago
|
// TODO: Move away from window.Readarr and prefetch the information returned here when starting the UI
|
||
3 years ago
|
return Content(GetContent(), "application/javascript");
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
|
private string GetContent()
|
||
|
{
|
||
|
if (RuntimeInfo.IsProduction && _generatedContent != null)
|
||
|
{
|
||
|
return _generatedContent;
|
||
|
}
|
||
|
|
||
|
var builder = new StringBuilder();
|
||
4 years ago
|
builder.AppendLine("window.Readarr = {");
|
||
6 years ago
|
builder.AppendLine($" apiRoot: '{_urlBase}/api/v1',");
|
||
|
builder.AppendLine($" apiKey: '{_apiKey}',");
|
||
|
builder.AppendLine($" release: '{BuildInfo.Release}',");
|
||
|
builder.AppendLine($" version: '{BuildInfo.Version.ToString()}',");
|
||
2 years ago
|
builder.AppendLine($" instanceName: '{_configFileProvider.InstanceName.ToString()}',");
|
||
2 years ago
|
builder.AppendLine($" theme: '{_configFileProvider.Theme.ToString()}',");
|
||
6 years ago
|
builder.AppendLine($" branch: '{_configFileProvider.Branch.ToLower()}',");
|
||
|
builder.AppendLine($" analytics: {_analyticsService.IsEnabled.ToString().ToLowerInvariant()},");
|
||
5 years ago
|
builder.AppendLine($" userHash: '{HashUtil.AnonymousToken()}',");
|
||
6 years ago
|
builder.AppendLine($" urlBase: '{_urlBase}',");
|
||
|
builder.AppendLine($" isProduction: {RuntimeInfo.IsProduction.ToString().ToLowerInvariant()}");
|
||
|
builder.AppendLine("};");
|
||
|
|
||
|
_generatedContent = builder.ToString();
|
||
|
|
||
|
return _generatedContent;
|
||
|
}
|
||
6 years ago
|
}
|
||
|
}
|