1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Adding initial attempt at connecting to CDN

This commit is contained in:
Uncled1023 2016-10-06 17:02:34 -07:00
parent 430e274280
commit aef087a890
6 changed files with 116 additions and 8 deletions

View File

@ -1,5 +1,6 @@
using System.Web;
using System.Web.Optimization;
using Teknik.Helpers;
namespace Teknik
{
@ -8,17 +9,19 @@ namespace Teknik
// For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
public static void RegisterBundles(BundleCollection bundles)
{
BundleTable.EnableOptimizations = false;
bundles.UseCdn = true;
BundleTable.EnableOptimizations = true;
#if !DEBUG
BundleTable.EnableOptimizations = true;
#endif
bundles.Add(new StyleBundle("~/Content/Common").Include(
bundles.Add(new AzureStyleBundle("~/Content/Common", "https://cdn.teknik.io", "www").Include(
"~/Content/bootstrap.css",
"~/Content/font-awesome.css",
"~/Content/common.css"));
bundles.Add(new ScriptBundle("~/bundles/common").Include(
bundles.Add(new AzureScriptBundle("~/bundles/common", "https://cdn.teknik.io", "www").Include(
"~/Scripts/jquery-{version}.js",
"~/Scripts/jquery.validate*",
"~/Scripts/bootstrap.js",
@ -26,16 +29,16 @@ namespace Teknik
"~/Scripts/common.js",
"~/Scripts/respond.js"));
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
bundles.Add(new AzureScriptBundle("~/bundles/jquery", "https://cdn.teknik.io", "www").Include(
"~/Scripts/jquery-{version}.js",
"~/Scripts/jquery.validate*"));
// Use the development version of Modernizr to develop with and learn from. Then, when you're
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
bundles.Add(new AzureScriptBundle("~/bundles/modernizr", "https://cdn.teknik.io", "www").Include(
"~/Scripts/modernizr-*"));
bundles.Add(new ScriptBundle("~/bundles/markdown").Include(
bundles.Add(new AzureScriptBundle("~/bundles/markdown", "https://cdn.teknik.io", "www").Include(
"~/Scripts/PageDown/Markdown.Converter.js",
"~/Scripts/PageDown/Markdown.Sanitizer.js"));
}

View File

@ -4,6 +4,7 @@ using System.Web.Optimization;
using Teknik;
using Teknik.Configuration;
using Teknik.Controllers;
using Teknik.Helpers;
namespace Teknik.Areas.Home
{
@ -51,11 +52,11 @@ namespace Teknik.Areas.Home
);
// Register Style Bundles
BundleTable.Bundles.Add(new StyleBundle("~/Content/home").Include(
BundleTable.Bundles.Add(new AzureStyleBundle("~/Content/home", "https://cdn.teknik.io", "www").Include(
"~/Areas/Home/Content/Home.css"));
// Register Script Bundles
BundleTable.Bundles.Add(new ScriptBundle("~/bundles/home").Include(
BundleTable.Bundles.Add(new AzureScriptBundle("~/bundles/home", "https://cdn.teknik.io", "www").Include(
"~/Scripts/PageDown/Markdown.Converter.js",
"~/Scripts/PageDown/Markdown.Sanitizer.js"));
}

View File

@ -0,0 +1,63 @@
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Optimization;
namespace Teknik.Helpers
{
public class AzureScriptBundle : Bundle
{
public AzureScriptBundle(string virtualPath, string cdnHost, string subdomain)
: base(virtualPath, null, new IBundleTransform[] { new JsMinify(), new AzureBundleTransform { CdnHost = cdnHost, Subdomain = subdomain } })
{
ConcatenationToken = ";";
}
}
public class AzureStyleBundle : Bundle
{
public AzureStyleBundle(string virtualPath, string cdnHost, string subdomain)
: base(virtualPath, null, new IBundleTransform[] { new CssMinify(), new AzureBundleTransform { CdnHost = cdnHost, Subdomain = subdomain } })
{
}
}
public class AzureBundleTransform : IBundleTransform
{
public string CdnHost { get; set; }
public string Subdomain { get; set; }
static AzureBundleTransform()
{
}
public virtual void Process(BundleContext context, BundleResponse response)
{
var dir = VirtualPathUtility.GetDirectory(context.BundleVirtualPath).TrimStart('/').TrimStart('~').TrimStart('/').TrimEnd('/');
var file = VirtualPathUtility.GetFileName(context.BundleVirtualPath);
if (!context.BundleCollection.UseCdn)
{
return;
}
if (string.IsNullOrEmpty(CdnHost) || string.IsNullOrEmpty(Subdomain))
{
return;
}
using (var hashAlgorithm = SHA256.CreateHashAlgorithm())
{
var hash = HttpServerUtility.UrlTokenEncode(hashAlgorithm.ComputeHash(Encoding.Unicode.GetBytes(response.Content)));
context.BundleCollection.GetBundleFor(context.BundleVirtualPath).CdnPath = string.Format("{0}/{1}/{2}?sub={3}&v={4}", CdnHost.TrimEnd('/'), dir, file, Subdomain, hash);
}
}
}
}

View File

@ -84,6 +84,16 @@ namespace Teknik.Helpers
}
return hashString;
}
public static System.Security.Cryptography.SHA256 CreateHashAlgorithm()
{
if (CryptoConfig.AllowOnlyFipsAlgorithms)
{
return new SHA256CryptoServiceProvider();
}
return new SHA256Managed();
}
}
public class AES

View File

@ -73,7 +73,27 @@
<HintPath>..\packages\Microsoft.AspNet.Identity.EntityFramework.2.2.1\lib\net45\Microsoft.AspNet.Identity.EntityFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.KeyVault.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Azure.KeyVault.Core.1.0.0\lib\net40\Microsoft.Azure.KeyVault.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Data.Edm, Version=5.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Data.Edm.5.6.4\lib\net40\Microsoft.Data.Edm.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Data.OData, Version=5.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Data.OData.5.6.4\lib\net40\Microsoft.Data.OData.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Data.Services.Client, Version=5.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Data.Services.Client.5.6.4\lib\net40\Microsoft.Data.Services.Client.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.WindowsAzure.Storage, Version=7.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\WindowsAzure.Storage.7.2.1\lib\net40\Microsoft.WindowsAzure.Storage.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
<HintPath>..\packages\MySql.Data.6.9.9\lib\net45\MySql.Data.dll</HintPath>
<Private>True</Private>
@ -106,6 +126,10 @@
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Security" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Spatial, Version=5.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\System.Spatial.5.6.4\lib\net40\System.Spatial.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" />
<Reference Include="System.Web.ApplicationServices" />
@ -223,6 +247,7 @@
<Compile Include="Areas\User\Models\TrustedDevice.cs" />
<Compile Include="Areas\User\ViewModels\TwoFactorViewModel.cs" />
<Compile Include="Attributes\TeknikAuthorizeAttribute.cs" />
<Compile Include="Helpers\BundleExtensions.cs" />
<Compile Include="Models\TransferTypes.cs" />
<Compile Include="Areas\User\Models\UploadSettings.cs" />
<Compile Include="Areas\User\Models\UserSettings.cs" />

View File

@ -20,6 +20,10 @@
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net46" userInstalled="true" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net46" userInstalled="true" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net46" userInstalled="true" />
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Data.Edm" version="5.6.4" targetFramework="net452" />
<package id="Microsoft.Data.OData" version="5.6.4" targetFramework="net452" />
<package id="Microsoft.Data.Services.Client" version="5.6.4" targetFramework="net452" />
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net46" userInstalled="true" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net46" userInstalled="true" />
<package id="Modernizr" version="2.8.3" targetFramework="net452" userInstalled="true" />
@ -29,6 +33,8 @@
<package id="Piwik.Tracker" version="2.16.0.0" targetFramework="net452" />
<package id="QRCoder" version="1.2.2" targetFramework="net452" />
<package id="Respond" version="1.4.2" targetFramework="net452" userInstalled="true" />
<package id="System.Spatial" version="5.6.4" targetFramework="net452" />
<package id="TwoStepsAuthenticator" version="1.2.0" targetFramework="net452" />
<package id="WebGrease" version="1.6.0" targetFramework="net46" userInstalled="true" />
<package id="WindowsAzure.Storage" version="7.2.1" targetFramework="net452" />
</packages>