コード例 #1
0
        public async Task <LanguageAndCulture> Save(JObject data)
        {
            dynamic languageAndCultureDto = data;
            int?    languageAndCultureId  = languageAndCultureDto.Id;
            var     languageAndCulture    = new LanguageAndCulture()
            {
                Id = languageAndCultureId ?? 0
            };
            bool isNew   = languageAndCultureDto.isNew;
            bool publish = languageAndCultureDto.publish;
            var  currentLanguageAndCulture = await _contentManagementContext.LanguageAndCultures.AsNoTracking().SingleOrDefaultAsync(ln => ln.Id == languageAndCulture.Id);

            if (!isNew)
            {
                if (currentLanguageAndCulture == null)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.LanguageAndCultureNotFound));
                }


                languageAndCulture            = currentLanguageAndCulture;
                languageAndCulture.RowVersion = languageAndCultureDto.RowVersion;

                _contentManagementContext.LanguageAndCultures.Attach(languageAndCulture);
            }
            else
            {
                _contentManagementContext.LanguageAndCultures.Add(languageAndCulture);
            }

            languageAndCulture.Country       = languageAndCultureDto.Country;
            languageAndCulture.Culture       = languageAndCultureDto.Culture;
            languageAndCulture.Language      = languageAndCultureDto.Language;
            languageAndCulture.FlagId        = languageAndCultureDto.FlagId;
            languageAndCulture.IsDefaults    = languageAndCultureDto.IsDefaults;
            languageAndCulture.IsRightToLeft = languageAndCultureDto.IsRightToLeft;

            //for force update and change version for force client to get new versipn of language.js
            languageAndCulture.Version = (currentLanguageAndCulture?.Version ?? 0) + 1;

            languageAndCulture.Status = languageAndCultureDto.Status;

            if (currentLanguageAndCulture != null)
            {
                languageAndCulture.ViewRoleId   = currentLanguageAndCulture.ViewRoleId;
                languageAndCulture.ModifyRoleId = currentLanguageAndCulture.ModifyRoleId;
                languageAndCulture.AccessRoleId = currentLanguageAndCulture.AccessRoleId;
            }

            AuthorizeManager.SetAndCheckModifyAndAccessRole(languageAndCulture, languageAndCultureDto);

            await _contentManagementContext.SaveChangesAsync();

            await _contentManagementContext.LanguageAndCultures.Where(lc => lc.Language == languageAndCulture.Language && lc.Id != languageAndCulture.Id)
            .UpdateAsync(t => new LanguageAndCulture()
            {
                Version = languageAndCulture.Version
            });

            string jsCode = languageAndCultureDto.JsCode;

            if (!string.IsNullOrEmpty(jsCode))
            {
                await WriteFileAsync(Config.ResourcesSourceCodePath, languageAndCulture.Language, ".js", jsCode);

                if (!publish)
                {
                    return(languageAndCulture);
                }

                await WriteFileAsync(Config.ResourcesDistPath, languageAndCulture.Language, ".js", _compressManager.CompressJavaScript(jsCode, languageAndCulture.Country));
            }
            UpdateWebConfigSetting(languageAndCulture, ActionKey.Add);



            CacheManager.Remove(CacheManager.GetBrowsersCodeInfoKey(CacheKey.BrowsersCodeInfo.ToString(),
                                                                    "~/" + languageAndCulture.Language));


            //var bundleInfo =
            //    SourceControl.BrowsersCodeInfos.FirstOrDefault(bc => bc.BundleUrl == languageAndCulture.Language);

            //if(bundleInfo != null)
            //bundleInfo.Version
            //    = Helper.UrlEncode(System.Text.Encoding.UTF8.GetString(languageAndCulture.RowVersion));

            //SourceControl.BrowsersCodeInfos.Remove(
            //    SourceControl.BrowsersCodeInfos.Find(bc => bc.BundleUrl == languageAndCulture.Language));

            return(languageAndCulture);
        }
コード例 #2
0
        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);
        }