/// <summary> /// 生成静态文件 /// </summary> /// <param name="absoluteFilePath">生成文件绝对路径(不包含物理路径)</param> /// <param name="fileContent">要保存的文件内容</param> /// <param name="fullFilePath">生成文件文件成功之后,返回文件的物理路径,返回null表示生成文件失败</param> private static void GenerateFile(string absoluteFilePath, string fileContent, out string fullFilePath) { fullFilePath = null; //文件名转码,防止URL参数当文件名过长.wangwei-2014-06-2 absoluteFilePath = System.Web.HttpUtility.UrlDecode(absoluteFilePath); if (!string.IsNullOrEmpty(fileContent)) { fullFilePath = NVelocityBus.GenerateFilePath(absoluteFilePath); bool isGenerateFile = true; char[] invalidFileNameChars = IOHelper.InvalidFileNameChars; string fileName = IOHelper.GetFileNameWithoutExtension(fullFilePath); foreach (char invalidChar in invalidFileNameChars) { if (fileName.Contains(new string(invalidChar, 1))) { isGenerateFile = false; break; } } fileName = null; if (isGenerateFile) { char[] invalidPathChars = IOHelper.InvalidPathChars; string filePath = IOHelper.GetDirectoryName(fullFilePath); foreach (char invalidChar in invalidPathChars) { if (filePath.Contains(new string(invalidChar, 1))) { isGenerateFile = false; break; } } filePath = null; } if (isGenerateFile) { lock (@FileLockObject) { try { IOHelper.GenerateFile(fullFilePath, fileContent); } catch { fullFilePath = null; } } } else { fullFilePath = null; } } }
public static void BeginRequest(HttpRequest request, HttpResponse response) { Uri filterUri = GetUriWithoutTrace(request, response); int segmentsLength = filterUri.Segments.Length; if (string.IsNullOrWhiteSpace(IOHelper.GetFileExtension(segmentsLength > 0 ? filterUri.Segments[segmentsLength - 1] : filterUri.AbsolutePath)) && string.IsNullOrWhiteSpace(filterUri.Query)) {//如果页面URL不带"/"结尾,需要添加"/"然后执行301跳转。wangyunpeng. if (!string.IsNullOrWhiteSpace(filterUri.AbsolutePath) && !filterUri.AbsolutePath.EndsWith("/")) { Uri originalUri = request.Url; UriBuilder uriBuilder = new UriBuilder(originalUri.Scheme, originalUri.Host, originalUri.Port); uriBuilder.Path = string.Concat(originalUri.AbsolutePath, "/"); uriBuilder.Query = originalUri.Query; string url301 = uriBuilder.ToString(); response.RedirectPermanent(url301, true); return; } } string physicalPath = request.PhysicalPath; //指定的路径或文件名太长,文件名必须少于 260 个字符 string absoluteFilePath = string.Concat(UrlHasher.Hash(filterUri, physicalPath, "/"), NVelocityBus.DEFAULT_DOCUMENT_SUFFIX); //所有url全部hash存放到redis和本地内存中去。区分URL大小写。wangyunpeng。2016-7-20。 request.RequestContext.HttpContext.Items[NVelocityBus.NVELOCITY_TARGET_FILE_PATH] = absoluteFilePath; //设置放入上下文对象里面获取,2015-11-17。wangyunpeng //读取本地缓存(一级缓存) string fileContent = DataCacheBus.Get(absoluteFilePath) as string; //读取Redis缓存(二级缓存) if (NVelocityBus.CACHE_REDIS && string.IsNullOrWhiteSpace(fileContent)) { try { fileContent = "";//StackExchangeRedisBus.StringGet(absoluteFilePath); } catch (Exception) { } if (!string.IsNullOrWhiteSpace(fileContent)) {//放进缓存中(一级缓存) DataCacheBus.Insert(absoluteFilePath, fileContent = ZipHelper.GZipDeCompress(fileContent), DateTime.Now.AddMinutes(NVelocityBus.CACHE_DATETIME)); } } //如果仅采用本地缓存策略,需要从磁盘上读取静态文件。(防止清除本地缓存后访问量过大,改从静态文件读取) if (!NVelocityBus.CACHE_REDIS && string.IsNullOrWhiteSpace(fileContent)) { string physicalFilePath = null; string fileExtension = IOHelper.GetFileExtension(absoluteFilePath); if (!string.IsNullOrWhiteSpace(fileExtension) && fileExtension.Equals(NVelocityBus.DEFAULT_DOCUMENT_SUFFIX, StringComparison.CurrentCultureIgnoreCase)) { physicalFilePath = NVelocityBus.GenerateFilePath(absoluteFilePath); DateTime fileDateTime = IOHelper.GetFileMaxDateTime(physicalFilePath); if (fileDateTime != DateTime.MinValue) { //如果静态文件存在 if (fileDateTime.AddDays(NVelocityBus.CACHE_DAY) > DateTime.Now) { //只读CACHE_DAY天之内的静态文件. fileContent = IOHelper.GetFileContent(physicalFilePath); } else { //超过CACHE_DAY天之外的静态文件会自动删除. IOHelper.DeleteFile(physicalFilePath); } } } if (!string.IsNullOrWhiteSpace(fileContent) && !string.IsNullOrWhiteSpace(physicalFilePath)) { //如果读取磁盘上的文件成功 DataCacheBus.Insert(absoluteFilePath, fileContent, TimeSpan.FromMinutes(NVelocityBus.CACHE_DATETIME), physicalFilePath); //放入本地缓存(一级缓存) } } if (!string.IsNullOrWhiteSpace(fileContent)) { NVelocityBus.Output(request, response, fileContent, absoluteFilePath, true); } fileContent = null; absoluteFilePath = null; }