2018-11-23 07:03:32 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
using Nancy;
|
|
|
|
using Nancy.Responses;
|
2019-08-07 02:20:47 +00:00
|
|
|
using NzbDrone.Common;
|
2018-11-23 07:03:32 +00:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
|
|
|
using NzbDrone.Core.Analytics;
|
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
|
|
|
|
namespace Radarr.Http.Frontend
|
|
|
|
{
|
|
|
|
public class InitializeJsModule : NancyModule
|
|
|
|
{
|
|
|
|
private readonly IConfigFileProvider _configFileProvider;
|
|
|
|
private readonly IAnalyticsService _analyticsService;
|
|
|
|
|
|
|
|
private static string _apiKey;
|
|
|
|
private static string _urlBase;
|
|
|
|
private string _generatedContent;
|
|
|
|
|
|
|
|
public InitializeJsModule(IConfigFileProvider configFileProvider,
|
|
|
|
IAnalyticsService analyticsService)
|
|
|
|
{
|
|
|
|
_configFileProvider = configFileProvider;
|
|
|
|
_analyticsService = analyticsService;
|
|
|
|
|
|
|
|
_apiKey = configFileProvider.ApiKey;
|
|
|
|
_urlBase = configFileProvider.UrlBase;
|
|
|
|
|
2019-08-28 21:43:55 +00:00
|
|
|
Get("/initialize.js", x => Index());
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private Response Index()
|
|
|
|
{
|
|
|
|
// TODO: Move away from window.Sonarr and prefetch the information returned here when starting the UI
|
|
|
|
return new StreamResponse(GetContentStream, "application/javascript");
|
|
|
|
}
|
|
|
|
|
|
|
|
private Stream GetContentStream()
|
|
|
|
{
|
|
|
|
var text = GetContent();
|
|
|
|
|
|
|
|
var stream = new MemoryStream();
|
|
|
|
var writer = new StreamWriter(stream);
|
|
|
|
writer.Write(text);
|
|
|
|
writer.Flush();
|
|
|
|
stream.Position = 0;
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetContent()
|
|
|
|
{
|
|
|
|
if (RuntimeInfo.IsProduction && _generatedContent != null)
|
|
|
|
{
|
|
|
|
return _generatedContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
var builder = new StringBuilder();
|
|
|
|
builder.AppendLine("window.Radarr = {");
|
2019-12-03 01:36:18 +00:00
|
|
|
builder.AppendLine($" apiRoot: '{_urlBase}/api/v3',");
|
2018-11-23 07:03:32 +00:00
|
|
|
builder.AppendLine($" apiKey: '{_apiKey}',");
|
|
|
|
builder.AppendLine($" release: '{BuildInfo.Release}',");
|
|
|
|
builder.AppendLine($" version: '{BuildInfo.Version.ToString()}',");
|
|
|
|
builder.AppendLine($" branch: '{_configFileProvider.Branch.ToLower()}',");
|
|
|
|
builder.AppendLine($" analytics: {_analyticsService.IsEnabled.ToString().ToLowerInvariant()},");
|
2019-08-07 02:20:47 +00:00
|
|
|
builder.AppendLine($" userHash: '{HashUtil.AnonymousToken()}',");
|
2018-11-23 07:03:32 +00:00
|
|
|
builder.AppendLine($" urlBase: '{_urlBase}',");
|
|
|
|
builder.AppendLine($" isProduction: {RuntimeInfo.IsProduction.ToString().ToLowerInvariant()}");
|
|
|
|
builder.AppendLine("};");
|
|
|
|
|
|
|
|
_generatedContent = builder.ToString();
|
|
|
|
|
|
|
|
return _generatedContent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|