/// <summary> /// 生成缩略图 /// </summary> /// <param name="imagefilePath">图片路径</param> /// <param name="timming">生成方式</param> public static void HandleThumbnail(string imagefilePath, Timming timming) { UploadFileInfo _fileInfo = FileHelper.GetFileInfo(imagefilePath, @"^(.*)\\upload\\(.+)\\(day_\d+)\\(\d+)(\.[A-Za-z]+)$"); if (_fileInfo == null) { return; } string _root = _fileInfo.Root, _folder = _fileInfo.Folder, _subFolder = _fileInfo.SubFolder, _fileName = _fileInfo.FileName, _fileExt = _fileInfo.FileNameExt; foreach (var pair in UploadConfigContext.ThumbnailConfigDic .Where(t => t.Key.StartsWith(_fileInfo.Folder.ToLower() + "_") && t.Value.Timming == timming)) { ThumbnailSize _size = pair.Value; string _thumbnailFileFolder = string.Format("{0}\\upload\\{1}\\{2}\\thumb", _root, _folder, _subFolder); if (!Directory.Exists(_thumbnailFileFolder)) { Directory.CreateDirectory(_thumbnailFileFolder); } string _thumbnailFilePath = string.Format("{0}\\upload\\{1}\\{2}\\thumb\\{3}_{4}_{5}{6}", _root, _folder, _subFolder, _fileName, _size.Width, _size.Height, _fileExt); ThumbnailHelper.MakeThumbnail(imagefilePath, _thumbnailFilePath, _size); } }
/// <summary> /// Processes the request. /// </summary> /// <param name="context">The context.</param> public void ProcessRequest(HttpContext context) { //如果304已缓存了,则返回 if (!string.IsNullOrEmpty(context.Request.Headers["If-Modified-Since"])) { context.Response.StatusCode = 304; context.Response.StatusDescription = "Not Modified"; return; } string _path = context.Request.CurrentExecutionFilePath; if (!_path.EndsWith(".axd") && !_path.StartsWith("/Upload", StringComparison.OrdinalIgnoreCase)) { return; } //正则从Url里匹配出上传的文件夹目录..... Match _uploadfolder = Regex.Match(_path, @"upload/(.+)/(day_\d+)/thumb/(\d+)_(\d+)_(\d+)\.([A-Za-z]+)\.axd$", RegexOptions.IgnoreCase); if (!_uploadfolder.Success) { return; } string _folder = _uploadfolder.Groups[1].Value, _subFolder = _uploadfolder.Groups[2].Value, _fileName = _uploadfolder.Groups[3].Value, _width = _uploadfolder.Groups[4].Value, _height = _uploadfolder.Groups[5].Value, _fileExt = _uploadfolder.Groups[6].Value; //如果在配置找不到需要按需生成的,则返回,这样能防止任何人随便敲个尺寸就生成 string _key = string.Format("{0}_{1}_{2}", _folder, _width, _height).ToLower(); bool isOnDemandSize = UploadConfigContext.ThumbnailConfigDic.ContainsKey(_key) && UploadConfigContext.ThumbnailConfigDic[_key].Timming == Timming.OnDemand; if (!isOnDemandSize) { return; } string _thumbnailFilePath = string.Format(@"{0}\{1}\Thumb\{2}_{4}_{5}.{3}", _folder, _subFolder, _fileName, _fileExt, _width, _height); _thumbnailFilePath = Path.Combine(UploadConfigContext.UploadPath, _thumbnailFilePath); string _filePath = string.Format(@"{0}\{1}\{2}.{3}", _folder, _subFolder, _fileName, _fileExt); _filePath = Path.Combine(UploadConfigContext.UploadPath, _filePath); if (!File.Exists(_filePath)) { return; } //如果不存在缩略图,则生成 if (!File.Exists(_thumbnailFilePath)) { string _thumbnailFileFolder = string.Format(@"{0}\{1}\Thumb", _folder, _subFolder); _thumbnailFileFolder = Path.Combine(UploadConfigContext.UploadPath, _thumbnailFileFolder); if (!Directory.Exists(_thumbnailFileFolder)) { Directory.CreateDirectory(_thumbnailFileFolder); } ThumbnailHelper.MakeThumbnail(_filePath, _thumbnailFilePath, UploadConfigContext.ThumbnailConfigDic[_key]); } //缩略图存在了,返回图片字节,并输出304标记 context.Response.Clear(); context.Response.ContentType = HttpContextHelper.GetImageContentType(_fileExt); byte[] _responseImage = File.ReadAllBytes(_thumbnailFilePath); context.Response.BinaryWrite(_responseImage); context.Set304Cache(); context.Response.Flush(); }
/// <summary> /// 延迟生成图片缩略图 /// </summary> public static void HandlerLazyThumbnail() { foreach (var group in UploadConfigContext.UploadConfig.UploadFolders) { string _folder = Path.Combine(UploadConfigContext.UploadPath, group.Path); if (!Directory.Exists(_folder)) { continue; } foreach (string dayFolder in Directory.GetDirectories(_folder)) { foreach (string filePath in Directory.GetFiles(dayFolder)) { UploadFileInfo _fileInfo = FileHelper.GetFileInfo(filePath, @"^(.+\\day_\d+)\\(\d+)(\.[A-Za-z]+)$"); if (_fileInfo == null) { continue; } string _root = _fileInfo.Root, _fileName = _fileInfo.FileName, _fileExt = _fileInfo.FileNameExt, _thumbnailFileFolder = Path.Combine(dayFolder, "Thumb"); if (!Directory.Exists(_thumbnailFileFolder)) { Directory.CreateDirectory(_thumbnailFileFolder); } //删除配置里干掉的Size对应的缩略图 //先不启用,等配置添完了再启用 //foreach (var thumbFilePath in Directory.GetFiles(thumbnailFileFolder)) //{ // if (!group.ThumbnailSizes.Exists(s => // Regex.IsMatch(thumbFilePath, string.Format(@"\\\d+_{0}_{1}+\.[A-Za-z]+$", s.Width, s.Height)))) // File.Delete(thumbFilePath); //} foreach (var size in group.ThumbnailSizes) { if (size.Timming != Timming.Lazy) { continue; } string _thumbnailFilePath = string.Format("{0}\\thumb\\{1}_{2}_{3}{4}", _root, _fileName, size.Width, size.Height, _fileExt); if (File.Exists(_thumbnailFilePath) && size.IsReplace) { File.Delete(_thumbnailFilePath); } if (!File.Exists(_thumbnailFilePath)) { ThumbnailHelper.MakeThumbnail(filePath, _thumbnailFilePath, size); } } } } } }