Ejemplo n.º 1
0
        public static void HandleImmediateThumbnail(string filePath, Timming timming = Timming.Immediate)
        {
            //正则从文件路径里匹配出上传的文件夹目录.....
            var m = Regex.Match(filePath, @"^(.*)\\upload\\(.+)\\(day_\d+)\\(\d+)(\.[A-Za-z]+)$", RegexOptions.IgnoreCase);

            if (!m.Success)
            {
                return;
            }

            var root      = m.Groups[1].Value;
            var folder    = m.Groups[2].Value;
            var subFolder = m.Groups[3].Value;
            var fileName  = m.Groups[4].Value;
            var ext       = m.Groups[5].Value;

            foreach (var pair in UploadConfigContext.ThumbnailConfigDic
                     .Where(t => t.Key.StartsWith(folder.ToLower() + "_") && t.Value.Timming == timming))
            {
                var size = pair.Value;

                var thumbnailFileFolder = string.Format("{0}\\upload\\{1}\\{2}\\thumb",
                                                        root, folder, subFolder);

                if (!Directory.Exists(thumbnailFileFolder))
                {
                    Directory.CreateDirectory(thumbnailFileFolder);
                }

                var thumbnailFilePath = string.Format("{0}\\upload\\{1}\\{2}\\thumb\\{3}_{4}_{5}{6}",
                                                      root, folder, subFolder, fileName, size.Width, size.Height, ext);

                ThumbnailHelper.MakeThumbnail(filePath, thumbnailFilePath, size);
            }
        }
Ejemplo n.º 2
0
        public static void HandlerLazyThumbnail()
        {
            foreach (var group in UploadConfigContext.UploadConfig.UploadFolders)
            {
                var folder = Path.Combine(UploadConfigContext.UploadPath, group.Path);

                if (!Directory.Exists(folder))
                {
                    continue;
                }

                foreach (var dayFolder in Directory.GetDirectories(folder))
                {
                    foreach (var filePath in Directory.GetFiles(dayFolder))
                    {
                        var m = Regex.Match(filePath, @"^(.+\\day_\d+)\\(\d+)(\.[A-Za-z]+)$", RegexOptions.IgnoreCase);

                        if (!m.Success)
                        {
                            continue;
                        }

                        var root     = m.Groups[1].Value;
                        var fileName = m.Groups[2].Value;
                        var ext      = m.Groups[3].Value;

                        var 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;
                            }

                            var thumbnailFilePath = string.Format("{0}\\thumb\\{1}_{2}_{3}{4}",
                                                                  root, fileName, size.Width, size.Height, ext);

                            if (File.Exists(thumbnailFilePath) && size.IsReplace)
                            {
                                File.Delete(thumbnailFilePath);
                            }

                            if (!File.Exists(thumbnailFilePath))
                            {
                                ThumbnailHelper.MakeThumbnail(filePath, thumbnailFilePath, size);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        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;
            }

            var path = context.Request.CurrentExecutionFilePath;

            if (!path.EndsWith(".axd") && !path.StartsWith("/Upload", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            //正则从Url里匹配出上传的文件夹目录.....
            var m = Regex.Match(path, @"upload/(.+)/(day_\d+)/thumb/(\d+)_(\d+)_(\d+)\.([A-Za-z]+)\.axd$", RegexOptions.IgnoreCase);

            if (!m.Success)
            {
                return;
            }

            var folder    = m.Groups[1].Value;
            var subFolder = m.Groups[2].Value;
            var fileName  = m.Groups[3].Value;
            var width     = m.Groups[4].Value;
            var height    = m.Groups[5].Value;
            var ext       = m.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;
            }

            var thumbnailFilePath = string.Format(@"{0}\{1}\Thumb\{2}_{4}_{5}.{3}", folder, subFolder, fileName, ext, width, height);

            thumbnailFilePath = Path.Combine(UploadConfigContext.UploadPath, thumbnailFilePath);

            var filePath = string.Format(@"{0}\{1}\{2}.{3}", folder, subFolder, fileName, ext);

            filePath = Path.Combine(UploadConfigContext.UploadPath, filePath);

            if (!File.Exists(filePath))
            {
                return;
            }

            //如果不存在缩略图,则生成
            if (!File.Exists(thumbnailFilePath))
            {
                var 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 = GetImageType(ext);
            byte[] responseImage = File.ReadAllBytes(thumbnailFilePath);
            context.Response.BinaryWrite(responseImage);

            Set304Cache(context);

            context.Response.Flush();
        }