Split VAR.WebForms.Common to a class library.
This commit is contained in:
65
VAR.WebForms.Common/Code/Bundler.cs
Normal file
65
VAR.WebForms.Common/Code/Bundler.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
|
||||
namespace VAR.WebForms.Common.Code
|
||||
{
|
||||
public class Bundler
|
||||
{
|
||||
#region Declarations
|
||||
|
||||
private string _path = null;
|
||||
private List<string> _files = null;
|
||||
|
||||
#endregion Declarations
|
||||
|
||||
#region Properties
|
||||
|
||||
private List<string> Files
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_files != null) { return _files; }
|
||||
|
||||
DirectoryInfo dir = new DirectoryInfo(_path);
|
||||
FileInfo[] files = dir.GetFiles();
|
||||
_files = files.OrderBy(file => file.FullName).Select(file2 => file2.FullName).ToList();
|
||||
return _files;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Properties
|
||||
|
||||
#region Creator
|
||||
|
||||
public Bundler(string path)
|
||||
{
|
||||
_path = path;
|
||||
}
|
||||
|
||||
#endregion Creator
|
||||
|
||||
#region Public methods
|
||||
|
||||
public void WriteResponse(HttpResponse response, string contentType)
|
||||
{
|
||||
response.ContentType = contentType;
|
||||
foreach (string fileName in Files)
|
||||
{
|
||||
string fileContent = File.ReadAllText(fileName);
|
||||
byte[] byteArray = Encoding.UTF8.GetBytes(fileContent);
|
||||
if (byteArray.Length > 0)
|
||||
{
|
||||
response.OutputStream.Write(byteArray, 0, byteArray.Length);
|
||||
|
||||
byteArray = Encoding.UTF8.GetBytes("\n\n");
|
||||
response.OutputStream.Write(byteArray, 0, byteArray.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Public methods
|
||||
}
|
||||
}
|
||||
50
VAR.WebForms.Common/Code/ExtensionMethods.cs
Normal file
50
VAR.WebForms.Common/Code/ExtensionMethods.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Web;
|
||||
using VAR.Json;
|
||||
|
||||
namespace VAR.WebForms.Common.Code
|
||||
{
|
||||
public static class ExtensionMethods
|
||||
{
|
||||
#region HttpContext
|
||||
|
||||
public static string GetRequestParm(this HttpContext context, string parm)
|
||||
{
|
||||
foreach (string key in context.Request.Params.AllKeys)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key) == false && key.EndsWith(parm))
|
||||
{
|
||||
return context.Request.Params[key];
|
||||
}
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public static void ResponseObject(this HttpContext context, object obj)
|
||||
{
|
||||
var jsonWritter = new JsonWriter();
|
||||
context.Response.ContentType = "text/json";
|
||||
string strObject = jsonWritter.Write(obj);
|
||||
context.Response.Write(strObject);
|
||||
}
|
||||
|
||||
public static void PrepareCacheableResponse(this HttpResponse response)
|
||||
{
|
||||
const int secondsInDay = 86400;
|
||||
response.ExpiresAbsolute = DateTime.Now.AddSeconds(secondsInDay);
|
||||
response.Expires = secondsInDay;
|
||||
response.Cache.SetCacheability(HttpCacheability.Public);
|
||||
response.Cache.SetMaxAge(new TimeSpan(0, 0, secondsInDay));
|
||||
}
|
||||
|
||||
public static void PrepareUncacheableResponse(this HttpResponse response)
|
||||
{
|
||||
response.ExpiresAbsolute = DateTime.Now.AddDays(-2d);
|
||||
response.Expires = -1500;
|
||||
response.AddHeader("Cache-Control", "max-age=0, no-cache, no-store");
|
||||
response.BufferOutput = true;
|
||||
}
|
||||
|
||||
#endregion HttpContext
|
||||
}
|
||||
}
|
||||
29
VAR.WebForms.Common/Code/GlobalConfig.cs
Normal file
29
VAR.WebForms.Common/Code/GlobalConfig.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace VAR.WebForms.Common.Code
|
||||
{
|
||||
public static class GlobalConfig
|
||||
{
|
||||
private static IGlobalConfig _globalConfig = null;
|
||||
|
||||
public static IGlobalConfig Get()
|
||||
{
|
||||
if (_globalConfig == null)
|
||||
{
|
||||
Type iGlobalConfig = typeof(IGlobalConfig);
|
||||
Type foundGlobalConfig = AppDomain.CurrentDomain
|
||||
.GetAssemblies()
|
||||
.SelectMany(x => x.GetTypes())
|
||||
.Where(x =>
|
||||
x.IsAbstract == false &&
|
||||
x.IsInterface == false &&
|
||||
iGlobalConfig.IsAssignableFrom(x) &&
|
||||
true)
|
||||
.FirstOrDefault();
|
||||
_globalConfig = ObjectActivator.CreateInstance(foundGlobalConfig) as IGlobalConfig;
|
||||
}
|
||||
return _globalConfig;
|
||||
}
|
||||
}
|
||||
}
|
||||
64
VAR.WebForms.Common/Code/GlobalErrorHandler.cs
Normal file
64
VAR.WebForms.Common/Code/GlobalErrorHandler.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using VAR.WebForms.Common.Pages;
|
||||
|
||||
namespace VAR.WebForms.Common.Code
|
||||
{
|
||||
public static class GlobalErrorHandler
|
||||
{
|
||||
#region Private methods
|
||||
|
||||
private static void ShowInternalError(HttpContext context, Exception ex)
|
||||
{
|
||||
context.Response.StatusCode = 500;
|
||||
context.Response.Clear();
|
||||
|
||||
StringBuilder sbOutput = new StringBuilder();
|
||||
sbOutput.Append("<h2>Internal error</h2>");
|
||||
Exception exAux = ex;
|
||||
if (exAux is HttpUnhandledException && exAux.InnerException != null) { exAux = exAux.InnerException; }
|
||||
while (exAux != null)
|
||||
{
|
||||
sbOutput.AppendFormat("<p><b>Message:</b> {0}</p>", exAux.Message);
|
||||
sbOutput.AppendFormat("<p><b>StackTrace:</b></p> <pre><code>{0}</code></pre>", exAux.StackTrace);
|
||||
exAux = exAux.InnerException;
|
||||
}
|
||||
|
||||
// Fill response to 512 bytes to avoid browser "beauty" response of errors.
|
||||
long fillResponse = 512 - sbOutput.Length;
|
||||
if (fillResponse > 0)
|
||||
{
|
||||
sbOutput.Append("<!--");
|
||||
for (int i = 0; i < fillResponse; i++)
|
||||
{
|
||||
sbOutput.Append("A");
|
||||
}
|
||||
sbOutput.Append("-->");
|
||||
}
|
||||
|
||||
context.Response.Write(sbOutput.ToString());
|
||||
}
|
||||
|
||||
#endregion Private methods
|
||||
|
||||
#region Public methods
|
||||
|
||||
public static void HandleError(HttpContext context, Exception ex)
|
||||
{
|
||||
try
|
||||
{
|
||||
IHttpHandler frmError = new FrmError(ex);
|
||||
context.Response.Clear();
|
||||
context.Handler = frmError;
|
||||
frmError.ProcessRequest(context);
|
||||
}
|
||||
catch
|
||||
{
|
||||
ShowInternalError(context, ex);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Public methods
|
||||
}
|
||||
}
|
||||
19
VAR.WebForms.Common/Code/IGlobalConfig.cs
Normal file
19
VAR.WebForms.Common/Code/IGlobalConfig.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Web;
|
||||
|
||||
namespace VAR.WebForms.Common.Code
|
||||
{
|
||||
public interface IGlobalConfig
|
||||
{
|
||||
string Title { get; }
|
||||
string TitleSeparator { get; }
|
||||
string Author { get; }
|
||||
string Copyright { get; }
|
||||
string DefaultHandler { get; }
|
||||
string LoginHandler { get; }
|
||||
List<string> AllowedExtensions { get; }
|
||||
|
||||
bool IsUserAuthenticated(HttpContext context);
|
||||
void UserUnauthenticate(HttpContext context);
|
||||
}
|
||||
}
|
||||
82
VAR.WebForms.Common/Code/MultiLang.cs
Normal file
82
VAR.WebForms.Common/Code/MultiLang.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Web;
|
||||
using VAR.Json;
|
||||
|
||||
namespace VAR.WebForms.Common.Code
|
||||
{
|
||||
public class MultiLang
|
||||
{
|
||||
private static string GetLocalPath(string path)
|
||||
{
|
||||
string currentDir = Path.GetDirectoryName((new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath);
|
||||
return string.Format("{0}/{1}", Directory.GetParent(currentDir), path);
|
||||
}
|
||||
|
||||
private static Dictionary<string, Dictionary<string, object>> _literals = null;
|
||||
|
||||
private static void InitializeLiterals()
|
||||
{
|
||||
_literals = new Dictionary<string, Dictionary<string, object>>();
|
||||
|
||||
JsonParser jsonParser = new JsonParser();
|
||||
foreach (string lang in new string[] { "en", "es" })
|
||||
{
|
||||
string filePath = GetLocalPath(string.Format("Resources/Literals.{0}.json", lang));
|
||||
if (File.Exists(filePath) == false) { continue; }
|
||||
|
||||
string strJsonLiteralsLanguage = File.ReadAllText(filePath);
|
||||
object result = jsonParser.Parse(strJsonLiteralsLanguage);
|
||||
_literals.Add(lang, result as Dictionary<string, object>);
|
||||
}
|
||||
}
|
||||
|
||||
private const string _defaultLanguage = "en";
|
||||
|
||||
private static string GetUserLanguage()
|
||||
{
|
||||
HttpContext ctx = HttpContext.Current;
|
||||
if (ctx != null)
|
||||
{
|
||||
if (ctx.Items["UserLang"] != null)
|
||||
{
|
||||
return (string)ctx.Items["UserLang"];
|
||||
}
|
||||
|
||||
IEnumerable<string> userLanguages = ctx.Request.UserLanguages
|
||||
.Select(lang =>
|
||||
{
|
||||
if (lang.Contains(";"))
|
||||
{
|
||||
lang = lang.Split(';')[0];
|
||||
}
|
||||
if (lang.Contains("-"))
|
||||
{
|
||||
lang = lang.Split('-')[0];
|
||||
}
|
||||
return lang.ToLower();
|
||||
})
|
||||
.Where(lang => _literals.ContainsKey(lang));
|
||||
string userLang = userLanguages.FirstOrDefault() ?? _defaultLanguage;
|
||||
|
||||
ctx.Items["UserLang"] = userLang;
|
||||
return userLang;
|
||||
}
|
||||
return _defaultLanguage;
|
||||
}
|
||||
|
||||
public static string GetLiteral(string resource, string culture = null)
|
||||
{
|
||||
if (_literals == null) { InitializeLiterals(); }
|
||||
if (culture == null) { culture = GetUserLanguage(); }
|
||||
|
||||
if (_literals == null || _literals.ContainsKey(culture) == false) { return resource; }
|
||||
Dictionary<string, object> _literalCurrentCulture = _literals[culture];
|
||||
|
||||
if (_literalCurrentCulture == null || _literalCurrentCulture.ContainsKey(resource) == false) { return resource; }
|
||||
return (_literalCurrentCulture[resource] as string) ?? resource;
|
||||
}
|
||||
}
|
||||
}
|
||||
35
VAR.WebForms.Common/Code/ObjectActivator.cs
Normal file
35
VAR.WebForms.Common/Code/ObjectActivator.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace VAR.WebForms.Common.Code
|
||||
{
|
||||
public class ObjectActivator
|
||||
{
|
||||
private static Dictionary<Type, Func<object>> _creators = new Dictionary<Type, Func<object>>();
|
||||
|
||||
public static Func<object> GetLambdaNew(Type type)
|
||||
{
|
||||
if (_creators.ContainsKey(type))
|
||||
{
|
||||
return _creators[type];
|
||||
}
|
||||
|
||||
lock (_creators)
|
||||
{
|
||||
NewExpression newExp = Expression.New(type);
|
||||
LambdaExpression lambda = Expression.Lambda(typeof(Func<object>), newExp);
|
||||
Func<object> compiledLambdaNew = (Func<object>)lambda.Compile();
|
||||
|
||||
_creators.Add(type, compiledLambdaNew);
|
||||
}
|
||||
return _creators[type];
|
||||
}
|
||||
|
||||
public static object CreateInstance(Type type)
|
||||
{
|
||||
Func<object> creator = GetLambdaNew(type);
|
||||
return creator();
|
||||
}
|
||||
}
|
||||
}
|
||||
20
VAR.WebForms.Common/Code/ScriptsBundler.cs
Normal file
20
VAR.WebForms.Common/Code/ScriptsBundler.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Web;
|
||||
|
||||
namespace VAR.WebForms.Common.Code
|
||||
{
|
||||
public class ScriptsBundler : IHttpHandler
|
||||
{
|
||||
#region IHttpHandler
|
||||
|
||||
public bool IsReusable { get { return false; } }
|
||||
|
||||
public void ProcessRequest(HttpContext context)
|
||||
{
|
||||
Bundler bundler = new Bundler(context.Server.MapPath("~/Scripts/"));
|
||||
context.Response.PrepareCacheableResponse();
|
||||
bundler.WriteResponse(context.Response, "text/javascript");
|
||||
}
|
||||
|
||||
#endregion IHttpHandler
|
||||
}
|
||||
}
|
||||
94
VAR.WebForms.Common/Code/StaticFileHelper.cs
Normal file
94
VAR.WebForms.Common/Code/StaticFileHelper.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
|
||||
namespace VAR.WebForms.Common.Code
|
||||
{
|
||||
public class StaticFileHelper
|
||||
{
|
||||
private static Dictionary<string, string> _mimeTypeByExtension = new Dictionary<string, string> { {".aac", "audio/aac"},
|
||||
{".abw", "application/x-abiword"},
|
||||
{".arc", "application/octet-stream"},
|
||||
{".avi", "video/x-msvideo"},
|
||||
{".azw", "application/vnd.amazon.ebook"},
|
||||
{".bin", "application/octet-stream"},
|
||||
{".bz", "application/x-bzip"},
|
||||
{".bz2", "application/x-bzip2"},
|
||||
{".csh", "application/x-csh"},
|
||||
{".css", "text/css"},
|
||||
{".csv", "text/csv"},
|
||||
{".doc", "application/msword"},
|
||||
{".epub", "application/epub+zip"},
|
||||
{".gif", "image/gif"},
|
||||
{".htm", "text/html"},
|
||||
{".html", "text/html"},
|
||||
{".ico", "image/x-icon"},
|
||||
{".ics", "text/calendar"},
|
||||
{".jar", "application/java-archive"},
|
||||
{".jpg", "image/jpeg"},
|
||||
{".jpeg", "image/jpeg"},
|
||||
{".js", "application/javascript"},
|
||||
{".json", "application/json"},
|
||||
{".mid", "audio/midi"},
|
||||
{".midi", "audio/midi"},
|
||||
{".mpeg", "video/mpeg"},
|
||||
{".mpkg", "application/vnd.apple.installer+xml"},
|
||||
{".odp", "application/vnd.oasis.opendocument.presentation"},
|
||||
{".ods", "application/vnd.oasis.opendocument.spreadsheet"},
|
||||
{".odt", "application/vnd.oasis.opendocument.text"},
|
||||
{".oga", "audio/ogg"},
|
||||
{".ogv", "video/ogg"},
|
||||
{".ogx", "application/ogg"},
|
||||
{".png", "image/png"},
|
||||
{".pdf", "application/pdf"},
|
||||
{".ppt", "application/vnd.ms-powerpoint"},
|
||||
{".rar", "application/x-rar-compressed"},
|
||||
{".rtf", "application/rtf"},
|
||||
{".sh", "application/x-sh"},
|
||||
{".svg", "image/svg+xml"},
|
||||
{".swf", "application/x-shockwave-flash"},
|
||||
{".tar", "application/x-tar"},
|
||||
{".tiff", "image/tiff"},
|
||||
{".tif", "image/tiff"},
|
||||
{".ttf", "font/ttf"},
|
||||
{".vsd", "application/vnd.visio"},
|
||||
{".wav", "audio/x-wav"},
|
||||
{".weba", "audio/webm"},
|
||||
{".webm", "video/webm"},
|
||||
{".webp", "image/webp"},
|
||||
{".woff", "font/woff"},
|
||||
{".woff2", "font/woff2"},
|
||||
{".xhtml", "application/xhtml+xml"},
|
||||
{".xls", "application/vnd.ms-excel"},
|
||||
{".xml", "application/xml"},
|
||||
{".xul", "application/vnd.mozilla.xul+xml"},
|
||||
{".zip", "application/zip"},
|
||||
{".7z", "application/x-7z-compressed"},
|
||||
};
|
||||
|
||||
public static void ResponseStaticFile(HttpContext context, string filePath)
|
||||
{
|
||||
string extension = Path.GetExtension(filePath).ToLower();
|
||||
string contentType = null;
|
||||
if (_mimeTypeByExtension.ContainsKey(extension))
|
||||
{
|
||||
contentType = _mimeTypeByExtension[extension];
|
||||
}
|
||||
|
||||
context.Response.Clear();
|
||||
|
||||
if (string.IsNullOrEmpty(contentType) == false)
|
||||
{
|
||||
context.Response.ContentType = contentType;
|
||||
}
|
||||
context.Response.PrepareCacheableResponse();
|
||||
|
||||
context.Response.Buffer = true;
|
||||
context.Response.WriteFile(filePath);
|
||||
context.Response.Flush();
|
||||
context.Response.Close();
|
||||
context.Response.End();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
20
VAR.WebForms.Common/Code/StylesBundler.cs
Normal file
20
VAR.WebForms.Common/Code/StylesBundler.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Web;
|
||||
|
||||
namespace VAR.WebForms.Common.Code
|
||||
{
|
||||
public class StylesBundler : IHttpHandler
|
||||
{
|
||||
#region IHttpHandler
|
||||
|
||||
public bool IsReusable { get { return false; } }
|
||||
|
||||
public void ProcessRequest(HttpContext context)
|
||||
{
|
||||
Bundler bundler = new Bundler(context.Server.MapPath("~/Styles/"));
|
||||
context.Response.PrepareCacheableResponse();
|
||||
bundler.WriteResponse(context.Response, "text/css");
|
||||
}
|
||||
|
||||
#endregion IHttpHandler
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user