Пример #1
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);
        }
Пример #2
0
        public async Task <MasterDataKeyValue> SaveBundleOrBundleSource(JObject data, bool isSource = false)
        {
            dynamic bundleOrSource = data;

            int    id            = bundleOrSource.Id;
            var    isNew         = id == 0;
            var    dependency    = new List <string>();
            string dependencyKey = "";

            id = id == 0 ? (await GetMaxId()) + 1 : id;
            var codeName = isSource ? SourceCode : BundleCode;

            if (isSource)
            {
                int bundleId = bundleOrSource.ParentId;
                var bundle   = await
                               ContentManagementContext.MasterDataKeyValues.AsNoTracking().FirstOrDefaultAsync(md => md.Id == bundleId);

                if (bundle == null)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.ParentRecordNotFound));
                }


                var code = await
                           ContentManagementContext.MasterDataKeyValues.AsNoTracking().FirstOrDefaultAsync(
                    md => md.Id == bundle.ParentId);

                if (code == null)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.ParentRecordNotFound));
                }

                if (code.EditMode)
                {
                    SourceControl.CheckCodeCheckOute(code);
                }

                string sourcePathOrUrl = bundleOrSource.PathOrUrl;

                if (bundle.Value == 1)
                {
                    if (
                        sourcePathOrUrl.ToLower()
                        .IndexOf(bundle.PathOrUrl.ToLower().Replace("~", code.PathOrUrl.ToLower()),
                                 StringComparison.Ordinal) != 0)
                    {
                        throw new KhodkarInvalidException(
                                  LanguageManager.ToAsErrorMessage(ExceptionKey.SourceOfOneByOneBundleNotValid));
                    }
                }
            }
            else
            {
                dependencyKey = bundleOrSource.DependencyKey;
                if (dependencyKey != null)
                {
                    JArray dependencyArray = bundleOrSource.Dependency;
                    if (dependencyArray != null)
                    {
                        dependency = dependencyArray.ToObject <List <string> >();
                    }
                }
            }
            int key = 0;

            try
            {
                key = bundleOrSource.Key;
            }
            catch (Exception)
            {
                // ignored
            }

            if (!isNew && !isSource)
            {
                var bundleEntity = await
                                   ContentManagementContext.MasterDataKeyValues.AsNoTracking().Where(md => md.Id == id)
                                   .FirstOrDefaultAsync();

                if (bundleEntity == null)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.BundleNotFound));
                }

                var code = await
                           ContentManagementContext.MasterDataKeyValues.AsNoTracking().FirstOrDefaultAsync(
                    md => md.Id == bundleEntity.ParentId);

                if (code == null)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.ParentRecordNotFound));
                }


                if (code.EditMode)
                {
                    SourceControl.CheckCodeCheckOute(code);
                }

                int value = 0;

                try
                {
                    value = bundleOrSource.Value;
                }
                catch (Exception)
                {
                    // ignored
                }
                //oneByOne Bundle
                if (value == 1 && ((bundleEntity.Value ?? 0) == 0))
                {
                    var sources = await
                                  ContentManagementContext.MasterDataKeyValues.AsNoTracking().Where(
                        md => md.ParentId == id && md.TypeId == (int)EntityIdentity.BundleSource)
                                  .ToListAsync();

                    if (sources.Any(source => source.PathOrUrl.ToLower()
                                    .IndexOf(bundleEntity.PathOrUrl.ToLower().Replace("~", code.PathOrUrl.ToLower()),
                                             StringComparison.Ordinal) != 0))
                    {
                        throw new KhodkarInvalidException(
                                  LanguageManager.ToAsErrorMessage(ExceptionKey.SourceOfOneByOneBundleNotValid));
                    }
                }
                if (bundleEntity.Code != codeName + id && dependencyKey != bundleEntity.Code)
                {
                    await SourceControl.AddOrUpdateDependencyEngineAsync(new BundleDependency()
                    {
                        DependencyKey = bundleEntity.Code,
                        Path          = bundleEntity.PathOrUrl,
                        Dependency    = await GetBundleDependencyForDependencyEngieen(bundleEntity.Id),
                        Version       = bundleEntity.Version,
                        IsPublish     = true,
                        IsDelete      = true
                    });
                }
            }
            return(await base.Save(JObject.Parse(JsonConvert.SerializeObject
                                                     (new
            {
                Id = id,
                Code = string.IsNullOrEmpty(dependencyKey) ? codeName + id : dependencyKey,
                SecondCode = string.Join(",", dependency.ToArray()),
                bundleOrSource.Description,
                bundleOrSource.Name,
                EditMode = false,
                EnableCache = false,
                Guid = SecureGuid.NewGuid().ToString("N"),
                IsLeaf = false,
                IsType = false,
                Language = Config.DefaultsLanguage,
                bundleOrSource.ModifyRoleId,
                bundleOrSource.ParentId,
                bundleOrSource.RowVersion,
                Key = key,
                bundleOrSource.Value,
                Status = 1,
                TypeId = isSource ? (int)EntityIdentity.BundleSource : (int)EntityIdentity.Bundle,
                IsPath = true,
                IsPathSecond = false,
                PathOrUrlProtocolId = (int)Protocol.PathProtocol,
                bundleOrSource.PathOrUrl,
                bundleOrSource.SecondPathOrUrl,
                bundleOrSource.ViewRoleId,
                bundleOrSource.AccessRoleId,
                bundleOrSource.IsNew
            }, Formatting.None,
                                                     new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            })), false));
        }
Пример #3
0
        public async Task <bool> DeleteBundleOrBundleSource(JObject data, bool isSource = false)
        {
            dynamic dataDto = data;
            int     id      = dataDto.Id;

            if (isSource)
            {
                var sourceEntity = await
                                   ContentManagementContext.MasterDataKeyValues.FirstOrDefaultAsync(md => md.Id == id);

                var bundleEntity = await
                                   ContentManagementContext.MasterDataKeyValues.FirstOrDefaultAsync(md => md.Id == sourceEntity.ParentId);

                var code = await
                           ContentManagementContext.MasterDataKeyValues.FirstOrDefaultAsync(
                    md => md.Id == bundleEntity.ParentId);

                if (code.EditMode)
                {
                    SourceControl.CheckCodeCheckOute(code);
                }
            }
            else
            {
                var bundleEntity = await
                                   ContentManagementContext.MasterDataKeyValues.FirstOrDefaultAsync(md => md.Id == id);

                var code = await
                           ContentManagementContext.MasterDataKeyValues.FirstOrDefaultAsync(
                    md => md.Id == bundleEntity.ParentId);

                if (code.EditMode)
                {
                    SourceControl.CheckCodeCheckOute(code);
                }

                var sourcesCount = await ContentManagementContext.MasterDataKeyValues.Where(md => md.ParentId == bundleEntity.Id)
                                   .CountAsync();

                if (sourcesCount > 0)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.InUseItem, bundleEntity.Name));
                }
            }

            var dependency = !isSource ? await GetBundleDependencyForDependencyEngieen(id) : new List <KeyValue>();

            var bundle = await base.Delete(data);

            if (isSource)
            {
                return(true);
            }

            if (bundle.Code != BundleCode + bundle.Id)
            {
                await SourceControl.AddOrUpdateDependencyEngineAsync(new BundleDependency()
                {
                    DependencyKey = bundle.Code,
                    Path          = bundle.PathOrUrl,
                    Dependency    = dependency,
                    Version       = bundle.Version,
                    IsPublish     = true,
                    IsDelete      = true
                });
            }

            var path = "";

            if (bundle.PathOrUrl.IndexOf(".css", StringComparison.Ordinal) > -1)
            {
                path = Config.StyleDebugPath + (bundle.PathOrUrl[0] == '/' ? bundle.PathOrUrl.Substring(1) : bundle.PathOrUrl).Replace("~/", "");
            }
            else
            {
                path = Config.ScriptDebugPath + (bundle.PathOrUrl[0] == '/' ? bundle.PathOrUrl.Substring(1) : bundle.PathOrUrl).Replace("~/", "");
            }

            DeleteFile(path.IndexOf(Config.ScriptDebugPath, StringComparison.OrdinalIgnoreCase) > -1
                ? path.Replace(Config.ScriptDebugPath, Config.ScriptDistPath)
                : path.Replace(Config.StyleDebugPath, Config.StyleDistPath));
            return(DeleteFile(path));
        }