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 BTCPayServer.Services.Shopify.Models;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
namespace BTCPayServer.Controllers namespace BTCPayServer.Controllers
{ {
public partial class StoresController 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", return _cachedBasejavascript;
"shopify/btcpay-browser-client.js",
"shopify/btcpay-shopify-checkout.js"
};
if (_BtcpayServerOptions.BundleJsCss)
{
fileList = new[] {_bundleProvider.GetBundle("shopify-bundle.min.js").OutputFileUrl};
} }
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) foreach (var file in fileList)
{ {
await using var stream = _webHostEnvironment.WebRootFileProvider await using var stream = _webHostEnvironment.WebRootFileProvider
.GetFileInfo(file).CreateReadStream(); .GetFileInfo(file).CreateReadStream();
using var reader = new StreamReader(stream); 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"); return Content(jsFile, "text/javascript");
} }

View File

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