From 8b22b22d23aff3353ee1838846f12c0c87c97774 Mon Sep 17 00:00:00 2001 From: Uncled1023 Date: Fri, 7 Oct 2016 16:49:46 -0700 Subject: [PATCH] Added cacheing of the config. Added file extensions to cdn url's to allow Azure to work properly. Configured Uploads to use new CDN. Added httpCompression to allow compression over proxy. --- Teknik/Areas/Upload/UploadAreaRegistration.cs | 11 ++- Teknik/Configuration/Config.cs | 15 ++- Teknik/Global.asax.cs | 1 - Teknik/Helpers/BundleExtensions.cs | 16 ++-- Teknik/Helpers/Crypto.cs | 48 ++++++++++ Teknik/Teknik.csproj | 20 ---- Teknik/Web.config | 95 ++++++++++--------- Teknik/packages.config | 5 - 8 files changed, 126 insertions(+), 85 deletions(-) diff --git a/Teknik/Areas/Upload/UploadAreaRegistration.cs b/Teknik/Areas/Upload/UploadAreaRegistration.cs index 4e3d0a0..f3d4af6 100644 --- a/Teknik/Areas/Upload/UploadAreaRegistration.cs +++ b/Teknik/Areas/Upload/UploadAreaRegistration.cs @@ -3,6 +3,7 @@ using System.Web.Mvc; using System.Web.Optimization; using Teknik.Configuration; using Teknik.Controllers; +using Teknik.Helpers; namespace Teknik.Areas.Upload { @@ -61,18 +62,18 @@ namespace Teknik.Areas.Upload ); // Register Script Bundles - BundleTable.Bundles.Add(new ScriptBundle("~/bundles/upload").Include( + BundleTable.Bundles.Add(new CdnScriptBundle("~/bundles/upload", config.CdnHost).Include( "~/Scripts/Dropzone/dropzone.js", "~/Areas/Upload/Scripts/Upload.js", "~/Scripts/bootstrap-switch.js", "~/Scripts/bootbox/bootbox.min.js")); - BundleTable.Bundles.Add(new ScriptBundle("~/bundles/download").Include( + BundleTable.Bundles.Add(new CdnScriptBundle("~/bundles/download", config.CdnHost).Include( "~/Scripts/Blob.js", "~/Scripts/FileSaver.js", "~/Areas/Upload/Scripts/Download.js")); - BundleTable.Bundles.Add(new ScriptBundle("~/bundles/cryptoWorker").Include( + BundleTable.Bundles.Add(new CdnScriptBundle("~/bundles/cryptoWorker", config.CdnHost).Include( "~/Areas/Upload/Scripts/EncryptionWorker.js")); - BundleTable.Bundles.Add(new ScriptBundle("~/bundles/crypto").Include( + BundleTable.Bundles.Add(new CdnScriptBundle("~/bundles/crypto", config.CdnHost).Include( "~/Scripts/Crypto-js/aes.js", "~/Scripts/Crypto-js/enc-base64.js", "~/Scripts/Crypto-js/mode-ctr.js", @@ -80,7 +81,7 @@ namespace Teknik.Areas.Upload "~/Scripts/Crypto-js/pad-nopadding.js")); // Register Style Bundles - BundleTable.Bundles.Add(new StyleBundle("~/Content/upload").Include( + BundleTable.Bundles.Add(new CdnStyleBundle("~/Content/upload", config.CdnHost).Include( "~/Content/dropzone.css", "~/Content/bootstrap-switch/bootstrap3/bootstrap-switch.css", "~/Areas/Upload/Content/Upload.css")); diff --git a/Teknik/Configuration/Config.cs b/Teknik/Configuration/Config.cs index df790b3..89d0682 100644 --- a/Teknik/Configuration/Config.cs +++ b/Teknik/Configuration/Config.cs @@ -8,6 +8,9 @@ namespace Teknik.Configuration { public class Config { + private static Config _Config { get; set; } + private static string _FileHash { get; set; } + private ReaderWriterLockSlim _ConfigRWLock; private ReaderWriterLockSlim _ConfigFileRWLock; private JsonSerializerSettings _JsonSettings; @@ -152,7 +155,17 @@ namespace Teknik.Configuration public static Config Load() { string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString(); - return Load(path); + string newHash = string.Empty; + if (File.Exists(Path.Combine(path, "Config.json"))) + { + newHash = Helpers.MD5.FileHash(Path.Combine(path, "Config.json")); + } + if (_Config == null || _FileHash == null || newHash != _FileHash) + { + _Config = Load(path); + _FileHash = newHash; + } + return _Config; } public static Config Load(string path) diff --git a/Teknik/Global.asax.cs b/Teknik/Global.asax.cs index c8f8932..d07a2d0 100644 --- a/Teknik/Global.asax.cs +++ b/Teknik/Global.asax.cs @@ -53,7 +53,6 @@ namespace Teknik protected void Application_EndRequest(object sender, EventArgs e) { - Config config = Config.Load(); HttpContext context = HttpContext.Current; // Set the generation time in the header diff --git a/Teknik/Helpers/BundleExtensions.cs b/Teknik/Helpers/BundleExtensions.cs index e6d957d..37802ec 100644 --- a/Teknik/Helpers/BundleExtensions.cs +++ b/Teknik/Helpers/BundleExtensions.cs @@ -13,7 +13,7 @@ namespace Teknik.Helpers public class CdnScriptBundle : Bundle { public CdnScriptBundle(string virtualPath, string cdnHost) - : base(virtualPath, null, new IBundleTransform[] { new JsMinify(), new CdnBundleTransform { CdnHost = cdnHost } }) + : base(virtualPath, null, new IBundleTransform[] { new JsMinify(), new CdnBundleTransform(cdnHost, ".js") }) { ConcatenationToken = ";"; } @@ -22,7 +22,7 @@ namespace Teknik.Helpers public class CdnStyleBundle : Bundle { public CdnStyleBundle(string virtualPath, string cdnHost) - : base(virtualPath, null, new IBundleTransform[] { new CssMinify(), new CdnBundleTransform { CdnHost = cdnHost } }) + : base(virtualPath, null, new IBundleTransform[] { new CssMinify(), new CdnBundleTransform(cdnHost, ".css") }) { } } @@ -31,13 +31,16 @@ namespace Teknik.Helpers { public string CdnHost { get; set; } - static CdnBundleTransform() + public string Ext { get; set; } + + public CdnBundleTransform(string cdnHost, string ext) { + CdnHost = cdnHost; + Ext = ext; } public virtual void Process(BundleContext context, BundleResponse response) - { - + { // Don't continue if we aren't using a CDN if (!context.BundleCollection.UseCdn) { @@ -47,6 +50,7 @@ namespace Teknik.Helpers // Get the directory and filename for the bundle var dir = VirtualPathUtility.GetDirectory(context.BundleVirtualPath).TrimStart('/').TrimStart('~').TrimStart('/').TrimEnd('/'); var file = VirtualPathUtility.GetFileName(context.BundleVirtualPath); + var group = string.Format("{0}{1}", file, Ext); if (string.IsNullOrEmpty(CdnHost)) { @@ -56,7 +60,7 @@ namespace Teknik.Helpers 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}?v={3}", CdnHost.TrimEnd('/'), dir, file, hash); + context.BundleCollection.GetBundleFor(context.BundleVirtualPath).CdnPath = string.Format("{0}/{1}/{2}?v={3}&group={4}", CdnHost.TrimEnd('/'), dir, file, hash, group); } } } diff --git a/Teknik/Helpers/Crypto.cs b/Teknik/Helpers/Crypto.cs index 07d2f3d..74b456d 100644 --- a/Teknik/Helpers/Crypto.cs +++ b/Teknik/Helpers/Crypto.cs @@ -39,6 +39,44 @@ namespace Teknik.Helpers return sBuilder.ToString(); } + + public static string FileHash(string filename) + { + try + { + using (var md5 = System.Security.Cryptography.MD5.Create()) + { + using (var stream = File.OpenRead(filename)) + { + return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower(); + } + } + } + catch (Exception) + { + return string.Empty; + } + } + + public static string DataHash(string data) + { + try + { + using (var md5 = System.Security.Cryptography.MD5.Create()) + { + // convert string to stream + byte[] byteArray = Encoding.UTF8.GetBytes(data); + using (MemoryStream stream = new MemoryStream(byteArray)) + { + return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower(); + } + } + } + catch (Exception) + { + return string.Empty; + } + } } public class SHA384 @@ -55,6 +93,16 @@ namespace Teknik.Helpers public class SHA256 { + public static string Hash(string value) + { + byte[] valueBytes = Encoding.Unicode.GetBytes(value); + HashAlgorithm hash = new SHA256CryptoServiceProvider(); + + byte[] hashBytes = hash.ComputeHash(valueBytes); + + return Convert.ToBase64String(hashBytes); + } + public static string Hash(string value, string salt1, string salt2) { SHA256Managed hash = new SHA256Managed(); diff --git a/Teknik/Teknik.csproj b/Teknik/Teknik.csproj index 9ace3b6..cf62ca0 100644 --- a/Teknik/Teknik.csproj +++ b/Teknik/Teknik.csproj @@ -78,22 +78,6 @@ True - - ..\packages\Microsoft.Data.Edm.5.6.4\lib\net40\Microsoft.Data.Edm.dll - True - - - ..\packages\Microsoft.Data.OData.5.6.4\lib\net40\Microsoft.Data.OData.dll - True - - - ..\packages\Microsoft.Data.Services.Client.5.6.4\lib\net40\Microsoft.Data.Services.Client.dll - True - - - ..\packages\WindowsAzure.Storage.7.2.1\lib\net40\Microsoft.WindowsAzure.Storage.dll - True - ..\packages\MySql.Data.6.9.9\lib\net45\MySql.Data.dll True @@ -126,10 +110,6 @@ - - ..\packages\System.Spatial.5.6.4\lib\net40\System.Spatial.dll - True - diff --git a/Teknik/Web.config b/Teknik/Web.config index 21323f6..9c171a2 100644 --- a/Teknik/Web.config +++ b/Teknik/Web.config @@ -1,13 +1,13 @@ - + -
+
- + - - - - + + + + - - + + - + - - - + + + - - - - - + + + + + - - - + + + - + + - + - - - - + + + + - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + - + - + - - + + \ No newline at end of file diff --git a/Teknik/packages.config b/Teknik/packages.config index 2608f8b..d2e318a 100644 --- a/Teknik/packages.config +++ b/Teknik/packages.config @@ -21,9 +21,6 @@ - - - @@ -33,8 +30,6 @@ - - \ No newline at end of file