public virtual void BackUp(string backUpName) { var fileSystemManager = new FileSystemManager(); lock (Lock) { GetErrorById("1"); if (!ToggleLogInMemoryOnly()) { ToggleLogInMemoryOnly(); } fileSystemManager.CopyFile(_dbPath, fileSystemManager.CreatDirectoryIfNotExist(Config.LogPath) + "ErrorLog_" + backUpName + ".db"); fileSystemManager.DeleteFile(_dbPath); CreateDb(); if (ToggleLogInMemoryOnly()) { ToggleLogInMemoryOnly(); } } }
private string Transform(MasterDataKeyValue bundle, string localHost, string source, string dist) { FileSystemManager.CreatDirectoryIfNotExist(AuthorizeManager.AuthorizeActionOnPath (dist.Substring(0, dist.LastIndexOf("/", StringComparison.Ordinal)), ActionKey.WriteToDisk)); var bundlePath = source.ToLower().Replace("~/", ""); var bundleOption = new List <BundleOption> { new BundleOption() { Url = "~/BrowsersCodeOutPut/" + bundle.Guid + "/" + bundle.Version + "/" + bundlePath, Sources = new List <string>() { source } } }; foreach (var option in bundleOption) { foreach (var sourceUrl in option.Sources) { AuthorizeManager.AuthorizeActionOnPath(sourceUrl, ActionKey.ReadFromDisk); } } _bundleManager.AddBundle(bundleOption); var bundleNmae = "~/BrowsersCodeOutPut/" + bundle.Guid + "/" + bundle.Version + "/" + bundlePath .Replace(".", "-"); var url = bundleNmae.Replace("~", localHost); string contents; using (var wc = new System.Net.WebClient()) { wc.Encoding = Encoding.UTF8; contents = wc.DownloadString(url); } _bundleManager.RemoveBundle(bundleNmae); return(contents); }
public async Task <bool> Compile(JObject data, string localHost) { dynamic bundleDto = data; int id = bundleDto.Id; bool isPublish = bundleDto.IsPublish; //string buildJs = ""; var bundleBySources = await ContentManagementContext.MasterDataKeyValues.Where(cd => cd.Id == id || (cd.ParentId == id && cd.TypeId == (int)EntityIdentity.BundleSource)).ToListAsync(); var bundle = bundleBySources.FirstOrDefault(bn => bn.Id == id); var sources = bundleBySources.Where(sr => sr.ParentId == id).ToList(); if (bundle == null) { throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.BundleNotFound)); } if (sources.Count == 0) { throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.BundleHasNoSource)); } CheckAccess(bundle); var code = await ContentManagementContext.MasterDataKeyValues.FirstOrDefaultAsync(cd => cd.Id == bundle.ParentId); if (code.EditMode) { SourceControl.CheckCodeCheckOute(code); } bundle.Version++; await ContentManagementContext.SaveChangesAsync(); if (bundle.Value == 1) { foreach (var source in sources) { var debugpath = ""; if (source.PathOrUrl.IndexOf(".less", StringComparison.OrdinalIgnoreCase) > -1 || bundle.PathOrUrl.IndexOf(".sass", StringComparison.OrdinalIgnoreCase) > -1 || bundle.PathOrUrl.IndexOf(".scss", StringComparison.OrdinalIgnoreCase) > -1) { debugpath = source.PathOrUrl.ToLower().Replace(code.PathOrUrl.ToLower(), Config.StyleDebugPath).Replace("//", "/"); debugpath = debugpath.Replace(".less", ".css").Replace(".sass", ".css").Replace(".scss", ".css"); FileSystemManager.CreatDirectoryIfNotExist( AuthorizeManager.AuthorizeActionOnPath( debugpath.Substring(0, debugpath.LastIndexOf("/", StringComparison.Ordinal)), ActionKey.WriteToDisk)); await WriteFileAsync(debugpath, "", "", Transform(bundle, localHost, source.PathOrUrl, debugpath)); if (isPublish) { var minContent = ""; var distpath = source.PathOrUrl.ToLower().Replace(code.PathOrUrl.ToLower(), Config.StyleDistPath).Replace("//", "/"); distpath = distpath.Replace(".less", ".css").Replace(".sass", ".css").Replace(".scss", ".css"); minContent = _compressManager.CompressCss(Transform(bundle, localHost, source.PathOrUrl, distpath)); FileSystemManager.CreatDirectoryIfNotExist( AuthorizeManager.AuthorizeActionOnPath( distpath.Substring(0, distpath.LastIndexOf("/", StringComparison.Ordinal)), ActionKey.WriteToDisk)); await WriteFileAsync(distpath, "", "", minContent); } } else if (source.PathOrUrl.IndexOf(".js", StringComparison.OrdinalIgnoreCase) == -1 && source.PathOrUrl.IndexOf(".css", StringComparison.OrdinalIgnoreCase) == -1) { debugpath = source.PathOrUrl.ToLower().Replace(code.PathOrUrl.ToLower(), Config.StyleDebugPath).Replace("//", "/"); debugpath = debugpath.Remove(debugpath.LastIndexOf(".", StringComparison.Ordinal)) + ".js"; FileSystemManager.CreatDirectoryIfNotExist( AuthorizeManager.AuthorizeActionOnPath( debugpath.Substring(0, debugpath.LastIndexOf("/", StringComparison.Ordinal)), ActionKey.WriteToDisk)); await WriteFileAsync(debugpath, "", "", Transform(bundle, localHost, source.PathOrUrl, debugpath)); if (isPublish) { var minContent = ""; var distpath = source.PathOrUrl.ToLower().Replace(code.PathOrUrl.ToLower(), Config.ScriptDistPath).Replace("//", "/"); distpath = debugpath.Remove(distpath.LastIndexOf(".", StringComparison.Ordinal)) + ".js"; minContent = _compressManager.CompressJavaScript( await FileSystemManager.ReadAsync(AuthorizeManager.AuthorizeActionOnPath(source.PathOrUrl, ActionKey.ReadFromDisk)), source.PathOrUrl); FileSystemManager.CreatDirectoryIfNotExist( AuthorizeManager.AuthorizeActionOnPath( distpath.Substring(0, distpath.LastIndexOf("/", StringComparison.Ordinal)), ActionKey.WriteToDisk)); await WriteFileAsync(distpath, "", "", minContent); } } else { debugpath = source.PathOrUrl.IndexOf(".css", StringComparison.OrdinalIgnoreCase) > -1 ? source.PathOrUrl.ToLower().Replace(code.PathOrUrl.ToLower(), Config.StyleDebugPath).Replace("//", "/") : source.PathOrUrl.ToLower().Replace(code.PathOrUrl.ToLower(), Config.ScriptDebugPath).Replace("//", "/"); FileSystemManager.CreatDirectoryIfNotExist(AuthorizeManager.AuthorizeActionOnPath( debugpath.Substring(0, debugpath.LastIndexOf("/", StringComparison.Ordinal)), ActionKey.WriteToDisk)); FileSystemManager.CopyFile(AuthorizeManager.AuthorizeActionOnPath( source.PathOrUrl, ActionKey.ReadFromDisk), AuthorizeManager.AuthorizeActionOnPath(debugpath, ActionKey.WriteToDisk)); if (isPublish) { var distpath = ""; var minContent = ""; if (source.PathOrUrl.IndexOf(".css", StringComparison.OrdinalIgnoreCase) > -1) { distpath = source.PathOrUrl.ToLower().Replace(code.PathOrUrl.ToLower(), Config.StyleDistPath).Replace("//", "/"); minContent = _compressManager.CompressCss(await FileSystemManager.ReadAsync(AuthorizeManager.AuthorizeActionOnPath(source.PathOrUrl, ActionKey.ReadFromDisk))); } else { distpath = source.PathOrUrl.ToLower().Replace(code.PathOrUrl.ToLower(), Config.ScriptDistPath).Replace("//", "/"); minContent = _compressManager.CompressJavaScript( await FileSystemManager.ReadAsync(AuthorizeManager.AuthorizeActionOnPath(source.PathOrUrl, ActionKey.ReadFromDisk)), source.PathOrUrl); } FileSystemManager.CreatDirectoryIfNotExist( AuthorizeManager.AuthorizeActionOnPath( distpath.Substring(0, distpath.LastIndexOf("/", StringComparison.Ordinal)), ActionKey.WriteToDisk)); await WriteFileAsync(distpath, "", "", minContent); } } } return(true); } var bundlePath = bundle.PathOrUrl.ToLower().Replace("~/", ""); var bundleOption = new List <BundleOption> { new BundleOption() { Url = "~/BrowsersCodeOutPut/" + bundle.Guid + "/" + bundle.Version + "/" + bundlePath, Sources = sources.Select(sr => sr.PathOrUrl).ToList() } }; foreach (var option in bundleOption) { foreach (var source in option.Sources) { AuthorizeManager.AuthorizeActionOnPath(source, ActionKey.ReadFromDisk); } } _bundleManager.AddBundle(bundleOption); var path = ""; var bundleNmae = "~/BrowsersCodeOutPut/" + bundle.Guid + "/" + bundle.Version + "/" + bundlePath .Replace(".", "-"); var url = bundleNmae.Replace("~", localHost); string contents; using (var wc = new System.Net.WebClient()) { wc.Encoding = Encoding.UTF8; contents = wc.DownloadString(url); } if (bundlePath.IndexOf(".css", StringComparison.Ordinal) > -1) { path = Config.StyleDebugPath + (bundlePath[0] == '/' ? bundlePath.Substring(1) : bundlePath); } else { path = Config.ScriptDebugPath + (bundlePath[0] == '/' ? bundlePath.Substring(1) : bundlePath); } FileSystemManager.CreatDirectoryIfNotExist(AuthorizeManager.AuthorizeActionOnPath(path.Substring(0, path.LastIndexOf("/", StringComparison.Ordinal)), ActionKey.WriteToDisk)); await WriteFileAsync(path, "", "", contents); if (isPublish) { var minContent = ""; if (bundlePath.IndexOf(".css", StringComparison.Ordinal) > -1) { path = Config.StyleDistPath + (bundlePath[0] == '/' ? bundlePath.Substring(1) : bundlePath); minContent = _compressManager.CompressCss(contents); } else { path = Config.ScriptDistPath + (bundlePath[0] == '/' ? bundlePath.Substring(1) : bundlePath); minContent = _compressManager.CompressJavaScript(contents, path); } FileSystemManager.CreatDirectoryIfNotExist(AuthorizeManager.AuthorizeActionOnPath(path.Substring(0, path.LastIndexOf("/", StringComparison.Ordinal)), ActionKey.WriteToDisk)); await WriteFileAsync(path, "", "", minContent); } _bundleManager.RemoveBundle(bundleNmae); BrowsersCodeInfo bundleInfo = null; var bundleInfoCache = CacheManager.Get <BrowsersCodeInfo>(CacheManager.GetBrowsersCodeInfoKey(CacheKey.BrowsersCodeInfo.ToString(), bundle.PathOrUrl)); if (bundleInfoCache.IsCached) { bundleInfo = bundleInfoCache.Value; } //var bundleInfo = KS.Core.CodeManager.SourceControl.BrowsersCodeInfos.FirstOrDefault(bc => bc.BundleUrl == bundle.PathOrUrl); if (bundleInfo != null) { bundleInfo.Version = bundle.Version.ToString(); CacheManager.StoreForEver(CacheManager.GetBrowsersCodeInfoKey(CacheKey.BrowsersCodeInfo.ToString(), bundle.PathOrUrl), bundleInfo); } if (bundle.Code != BundleCode + bundle.Id) { await SourceControl.AddOrUpdateDependencyEngineAsync(new BundleDependency() { DependencyKey = bundle.Code, Path = bundle.PathOrUrl, Dependency = await GetBundleDependencyForDependencyEngieen(bundle.Id), Version = bundle.Version, IsPublish = isPublish, IsDelete = false }); } return(true); }