예제 #1
0
        private string _GetModifiedTime(string filepath)
        {
            FileInfo fi;
            DateTime dt = default(DateTime);

            try
            {
                fi = new FileInfo(filepath);
                dt = fi.LastWriteTime;
                return(GMTHelper.ToGMTString(dt));
            }
            catch (Exception ex)
            {
                ElectronicObserver.Utility.ErrorReporter.SendErrorReport(ex, "在读取文件修改时间时发生异常:" + dt);
                return("");
            }
        }
예제 #2
0
        private void _SaveModifiedTime(string filepath, string gmTime)
        {
            FileInfo fi;

            try
            {
                fi = new FileInfo(filepath);
                DateTime dt = GMTHelper.GMT2Local(gmTime);
                if (dt.Year > 1900)
                {
                    fi.LastWriteTime = dt;
                }
            }
            catch (Exception ex)
            {
                ElectronicObserver.Utility.ErrorReporter.SendErrorReport(ex, string.Format("在保存文件修改时间时发生异常。filepath: {0}, gmTime: {1}", filepath, gmTime));
            }
        }
예제 #3
0
        public override bool OnBeforeRequest(Session oSession)
        {
            if (oSession.fullUrl.Contains("/kcsapi/api_start2"))
            {
                oSession.bBufferResponse = true;
            }

            if (settings.CacheEnabled && oSession.fullUrl.Contains("/kcs/"))
            {
                // = KanColleCacher =
                string filepath;
                var    direction = Cache.GotNewRequest(oSession.fullUrl, out filepath);

                if (direction == Direction.Return_LocalFile ||
                    direction == Direction.NoCache_LocalFile)
                {
                    //返回本地文件
                    oSession.utilCreateResponseAndBypassServer();
                    oSession.oResponse.headers["Server"] = "nginx";
                    oSession.oResponse.headers["Date"]   = GMTHelper.ToGMTString(DateTime.Now);

                    filepath = filepath.ToLower();
                    if (filepath.EndsWith(".swf"))
                    {
                        oSession.oResponse.headers["Content-Type"] = "application/x-shockwave-flash";
                    }
                    else if (filepath.EndsWith(".mp3"))
                    {
                        oSession.oResponse.headers["Content-Type"] = "audio/mpeg";
                    }
                    else if (filepath.EndsWith(".png"))
                    {
                        oSession.oResponse.headers["Content-Type"] = "image/png";
                    }

                    oSession.oResponse.headers["Last-Modified"] = _GetModifiedTime(filepath);
                    oSession.oResponse.headers["Connection"]    = "close";
                    if (direction == Direction.NoCache_LocalFile)
                    {
                        oSession.oResponse.headers["Pragma"]        = "no-cache";
                        oSession.oResponse.headers["Cache-Control"] = "no-cache";
                    }
                    else
                    {
                        oSession.oResponse.headers["Pragma"]        = "public";
                        oSession.oResponse.headers["Cache-Control"] = "max-age=18000, public";
                    }
                    oSession.oResponse.headers["Accept-Ranges"] = "bytes";

                    byte[] file;
                    using (var fs = File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        file = new byte[fs.Length];
                        fs.Read(file, 0, (int)fs.Length);
                    }
                    oSession.ResponseBody = file;

                    //Debug.WriteLine("CACHR> 【返回本地】" + result);
                }
                else if (direction == Direction.Verify_LocalFile)
                {
                    //请求服务器验证文件
                    oSession.oRequest.headers["If-Modified-Since"] = _GetModifiedTime(filepath);
                    oSession.bBufferResponse = true;

                    //Debug.WriteLine("CACHR> 【验证文件】" + oSession.PathAndQuery);
                }
                else if (settings.ShowCacheLog && (settings.ShowMainD2Link || !oSession.fullUrl.Contains("mainD2.swf")))
                {
                    //下载文件
                    ElectronicObserver.Utility.Logger.Add(2, string.Format("重新下载缓存文件: {0}", oSession.fullUrl));
                }


                return(true);
            }
            return(false);
        }