Fix and Cache Shopify JS

This commit is contained in:
Kukks 2020-09-19 12:13:55 +02:00
parent 0cf9b20328
commit 4516bbdadd
2 changed files with 21 additions and 24 deletions

View File

@ -10,37 +10,42 @@ using BTCPayServer.Services.Shopify;
using BTCPayServer.Services.Shopify.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
namespace BTCPayServer.Controllers
{
public partial class StoresController
{
[AllowAnonymous]
[HttpGet("{storeId}/integrations/shopify/shopify.js")]
public async Task<IActionResult> ShopifyJavascript(string storeId)
{
string[] fileList = new[]
private static string _cachedBasejavascript;
private async Task<string> GetJavascript()
{
if (!string.IsNullOrEmpty(_cachedBasejavascript))
{
"modal/btcpay.js",
"shopify/btcpay-browser-client.js",
"shopify/btcpay-shopify-checkout.js"
};
if (_BtcpayServerOptions.BundleJsCss)
{
fileList = new[] {_bundleProvider.GetBundle("shopify-bundle.min.js").OutputFileUrl};
return _cachedBasejavascript;
}
var jsFile = $"var BTCPAYSERVER_URL = \"{Request.GetAbsoluteRoot()}\"; var STORE_ID = \"{storeId}\";";
string[] fileList = _BtcpayServerOptions.BundleJsCss
? new[] { "bundles/shopify-bundle.min.js"}
: new[] {"modal/btcpay.js", "shopify/btcpay-browser-client.js", "shopify/btcpay-shopify-checkout.js"};
foreach (var file in fileList)
{
await using var stream = _webHostEnvironment.WebRootFileProvider
.GetFileInfo(file).CreateReadStream();
using var reader = new StreamReader(stream);
jsFile += Environment.NewLine + await reader.ReadToEndAsync();
_cachedBasejavascript += Environment.NewLine + await reader.ReadToEndAsync();
}
return _cachedBasejavascript;
}
[AllowAnonymous]
[HttpGet("{storeId}/integrations/shopify/shopify.js")]
public async Task<IActionResult> ShopifyJavascript(string storeId)
{
var jsFile = $"var BTCPAYSERVER_URL = \"{Request.GetAbsoluteRoot()}\"; var STORE_ID = \"{storeId}\"; { await GetJavascript()}";
return Content(jsFile, "text/javascript");
}

View File

@ -58,15 +58,13 @@ namespace BTCPayServer.Controllers
ExplorerClientProvider explorerProvider,
IFeeProviderFactory feeRateProvider,
LanguageService langService,
IWebHostEnvironment env, IHttpClientFactory httpClientFactory,
PaymentMethodHandlerDictionary paymentMethodHandlerDictionary,
SettingsRepository settingsRepository,
IAuthorizationService authorizationService,
EventAggregator eventAggregator,
CssThemeManager cssThemeManager,
AppService appService,
IWebHostEnvironment webHostEnvironment,
IBundleProvider bundleProvider)
IWebHostEnvironment webHostEnvironment)
{
_RateFactory = rateFactory;
_Repo = repo;
@ -75,15 +73,12 @@ namespace BTCPayServer.Controllers
_LangService = langService;
_TokenController = tokenController;
_WalletProvider = walletProvider;
_Env = env;
_httpClientFactory = httpClientFactory;
_paymentMethodHandlerDictionary = paymentMethodHandlerDictionary;
_settingsRepository = settingsRepository;
_authorizationService = authorizationService;
_CssThemeManager = cssThemeManager;
_appService = appService;
_webHostEnvironment = webHostEnvironment;
_bundleProvider = bundleProvider;
_EventAggregator = eventAggregator;
_NetworkProvider = networkProvider;
_ExplorerProvider = explorerProvider;
@ -105,15 +100,12 @@ namespace BTCPayServer.Controllers
readonly TokenRepository _TokenRepository;
readonly UserManager<ApplicationUser> _UserManager;
private readonly LanguageService _LangService;
readonly IWebHostEnvironment _Env;
private readonly IHttpClientFactory _httpClientFactory;
private readonly PaymentMethodHandlerDictionary _paymentMethodHandlerDictionary;
private readonly SettingsRepository _settingsRepository;
private readonly IAuthorizationService _authorizationService;
private readonly CssThemeManager _CssThemeManager;
private readonly AppService _appService;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly IBundleProvider _bundleProvider;
private readonly EventAggregator _EventAggregator;
[TempData]