Ejemplo n.º 1
0
        public static List<CombinerModel> AddXMLConfig(string name, string description, string expires, string iNames,
                                                       ContentType contentType, bool isCache, bool isMinify,
                                                        List<CombinerModel> combinerList, bool isDebug, string version)
        {
            //if (string.IsNullOrEmpty(version))
            //{
            //    version = DateTime.Now.ToString("yyyyMMddHHmmss");
            //}
            //string version = DateTime.Now.ToString("yyyyMMddHHmmss");

            string url = HttpContext.Current.Request.Url.Host;

            if (HttpContext.Current.Request.Url.Port != 80)
            {
                url = HttpContext.Current.Request.Url.DnsSafeHost;
            }
            string linkUrl = string.Format(Constnatmanager.Link.CSS, url, name, version);

            if (contentType == Enum.ContentType.javascript)
            {
                linkUrl = string.Format(Constnatmanager.Link.Javascript, url, name, version);
            }

            CombinerModel newCombiner = new CombinerModel()
            {
                GUID = Guid.NewGuid().ToString(),
                Name = name,
                Description = description,
                Version = version,
                IsCache = isCache,
                IsMinify = isMinify,
                CreateDate = DateTime.Now,
                ContentType = contentType,
                LinkUrl = linkUrl,
                Expires = expires,
                IsDebug = isDebug
            };

            List<string> itemsNames = string.IsNullOrWhiteSpace(iNames) ? null : iNames.Split('|').ToList();

            if (itemsNames != null)
            {
                List<Item> combinerItemList = new List<Item>();

                foreach (var item in itemsNames)
                {
                    Item combinerItem = new Item();
                    combinerItem.Name = item;
                    combinerItemList.Add(combinerItem);
                }

                newCombiner.Items = combinerItemList;
            }

            combinerList.Add(newCombiner);

            return combinerList;
        }
Ejemplo n.º 2
0
        private void LoadData()
        {
            SystemConfigOperation sysConfigOperation = new SystemConfigOperation();
            string strFilePath = AppConfigHelper.GetConfigValue(Constnatmanager.Config.XmlConfigFilePath);
            IList<CombinerModel> combinerList = sysConfigOperation.ReadData(strFilePath);

            if (string.IsNullOrEmpty(guid))
            {
                combiner = new CombinerModel();
            }
            else
            {
                combiner = combinerList.Where(x => x.GUID == guid).FirstOrDefault();
            }
        }
Ejemplo n.º 3
0
        public static List<CombinerModel> CreateTestData()
        {
            List<CombinerModel> Resources = new List<CombinerModel>();

            CombinerModel model = new CombinerModel();
            model.GUID = Guid.NewGuid().ToString();
            model.Name = "IndexCss";
            model.Description = "首页css样式";
            model.IsCache = true;
            model.IsMinify = true;
            model.Version = "1.0";
            model.CreateDate = DateTime.Now;
            model.Expires = "3h";

            List<Item> items = new List<Item>();
            items.Add(new Item() { Name = "~/style/a.css" });
            items.Add(new Item() { Name = "http://www.cn100.com/b.css" });
            model.Items = items;
            Resources.Add(model);

            model = new CombinerModel();
            model.GUID = Guid.NewGuid().ToString();
            model.Name = "IndexJs";
            model.Description = "首页Js";
            model.IsCache = true;
            model.IsMinify = true;
            model.Version = "1.0";
            model.CreateDate = DateTime.Now;
            model.Expires = "4h";

            items = new List<Item>();
            items.Add(new Item() { Name = "~/Scripts/a.js" });
            items.Add(new Item() { Name = "http://www.cn100.com/b.Js" });
            model.Items = items;
            Resources.Add(model);

            return Resources;
        }
Ejemplo n.º 4
0
 public static void RemoveCache(CombinerModel item)
 {
     string key = SystemConfigOperation.GetCacheKey(item.Name, item.Version, item.IsCache);
     CacheHelper.Remove(key);
 }
Ejemplo n.º 5
0
 public static bool RemoveXMLItem(CombinerModel item, List<CombinerModel> combinerList)
 {
     return combinerList.Remove(item);
 }
Ejemplo n.º 6
0
 private void WriteCacheStatus(HttpResponse response,CombinerModel model,string eTag)
 {
     SetResponse(response, model, eTag);
     response.StatusCode = 304;
     response.Flush();
     response.End();
 }
Ejemplo n.º 7
0
        private void SetResponse(HttpResponse response, CombinerModel model,string eTag)
        {
            DateTime expireTime = ConvertToDatetime(model.Expires);
            response.ContentType = string.Format(Constnatmanager.Link.Type, model.ContentType.ToString());
            response.Cache.SetExpires(expireTime);
            response.Cache.SetMaxAge(new TimeSpan(expireTime.Ticks));
            response.Cache.SetCacheability(HttpCacheability.Public);
            response.Cache.SetLastModified(model.CreateDate);
            response.Cache.SetETag(eTag);

            if (IsAppendCacheExtension)
            {
                response.Cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
            }
        }
Ejemplo n.º 8
0
        private void ResponseOperation(HttpResponse response, byte[] bytes, CombinerModel model, bool isCompressed)
        {
            response.AppendHeader("Content-Length", bytes.Length.ToString());

            if (isCompressed)
            {
                response.AppendHeader("Content-Encoding", "gzip");
            }

            string eTag = GetETag(bytes, model.GUID);
            SetResponse(response,model, eTag);
            response.OutputStream.Write(bytes, 0, bytes.Length);
            response.Flush();
        }
Ejemplo n.º 9
0
        /// <summary>
        /// file process, include gzip and minify function
        /// </summary>
        /// <param name="rootPath"></param>
        /// <param name="model"></param>
        /// <param name="isCompressed"></param>
        /// <param name="isDebug"></param>
        /// <returns></returns>
        private byte[] FilesProcess(string rootPath, CombinerModel model, bool isCompressed, bool isDebug)
        {
            string[] fileNames = model.Items.Select(i => i.Name).ToArray();
            string[] arrContent =new  string[fileNames.Length];
            ContentType contentType = model.ContentType;
            int index=0;

            foreach (string fileName in fileNames)
            {
                string name = fileName.Trim();
                string strContent = this.ReadFileContent(rootPath, name);

                if (contentType == ContentType.javascript && strContent.LastIndexOf(";")+1!=strContent.Length)
                {
                    strContent += ";";
                }

                arrContent[index]=strContent;
                index++;
            }

            string content = string.Join("", arrContent);
            byte[] bytes = Encoding.UTF8.GetBytes(content);

            if (!isDebug && model.IsMinify)
            {
                StringBuilder contentBuilder = new StringBuilder();

                for (int i = 0; i < arrContent.Length; i++)
                {
                    contentBuilder.Append(HttpHelper.MinifiContentToString(arrContent[i], model.ContentType));
                }

                bytes = Encoding.UTF8.GetBytes(contentBuilder.ToString());
            }

            if (isCompressed)
            {
                bytes = CompressBytes(bytes);
            }

            return bytes;
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 1, file process
        /// 2. write cache
        /// </summary>
        /// <param name="context"></param>
        /// <param name="rootPath"></param>
        /// <param name="model"></param>
        /// <param name="isCompressed"></param>
        /// <param name="cacheID"></param>
        /// <returns></returns>
        private byte[] DataProcess(HttpContext context, string rootPath, CombinerModel model,bool isCompressed, string cacheID)
        {
            byte[] bytes = readCache(cacheID);

            if (bytes == null || bytes.Length == 0)
            {
                CacheHelper.Remove(model.GUID);
                bytes = FilesProcess(rootPath, model, isCompressed, model.IsDebug);

                if (model.IsCache)
                {
                    CacheHelper.WriteData(cacheID, bytes, CACHE_DURATION);
                }
            }
            else
            {
                string eTag = GetETag(bytes, model.GUID);

                if (IsCached(context.Request, eTag))
                {
                    WriteCacheStatus(context.Response, model, eTag);
                }
                return bytes;
            }

            return bytes;
        }