Example #1
0
        private Dictionary <string, string> GridFsSave()
        {
            var database = (_fileType == FileType.Image ? "picture" : "attach");
            var manager  = GridFsManager.Instance(database);
            var list     = new Dictionary <string, string>();

            foreach (string name in _context.Request.Files.Keys)
            {
                var file = _context.Request.Files[name];
                if (file == null)
                {
                    continue;
                }
                var fileName = manager.Save(file.InputStream);
                list.Add(name, string.Format("{0}mongo/{1}/{2}{3}", Contains.BaseUrl, database, fileName,
                                             Path.GetExtension(file.FileName)));
            }
            return(list);
        }
Example #2
0
        /// <summary> 返回图片格式 </summary>
        /// <param name="context"></param>
        /// <param name="defaultFile"></param>
        internal static void ResponseImage(HttpContext context = null, string defaultFile = null)
        {
            context = context ?? HttpContext.Current;
            if (context == null)
            {
                return;
            }
            var path = (string.IsNullOrWhiteSpace(defaultFile) ? context.Request.Url.AbsolutePath : defaultFile);
            var ext  = Path.GetExtension(path);

            if (string.IsNullOrWhiteSpace(ext))
            {
                return;
            }
            var reg  = new Regex(Contains.ImageUrlRegex, RegexOptions.IgnoreCase);
            var uri  = context.Request.Url.AbsoluteUri;
            var hash = uri.Md5();

            if (IsCachedOnBrowser(context, hash))
            {
                return;
            }
            int width = -1, height = -1;

            if (reg.IsMatch(uri))
            {
                var m = reg.Match(uri);
                width  = ConvertHelper.StrToInt(m.Groups["w"].Value, -1);
                height = ConvertHelper.StrToInt(m.Groups["h"].Value, -2);
                if (width == -1 && height < -1)
                {
                    height = -1;
                }
                if (path != null)
                {
                    path = path.Replace(m.Groups[1].Value, string.Empty);
                }
            }
            var resp = context.Response;

            resp.AppendHeader("Vary", "Accept-Encoding");
            resp.AppendHeader("Cache-Control", "max-age=604800");
            resp.AppendHeader("Expires", DateTime.Now.AddYears(1).ToString("R"));
            resp.AppendHeader("ETag", hash);
            resp.ContentType = Contains.MiniType[ext];
            resp.Charset     = "utf-8";
            var fileStream = Stream.Null;

            if (path != null && path.StartsWith("/mongo/"))
            {
                var name = Path.GetFileNameWithoutExtension(path);
                var db   = path.Split('/')[2];
                fileStream = GridFsManager.Instance(db).Read(name);
                if (fileStream == Stream.Null)
                {
                    path = context.Server.MapPath(Contains.DefaultImage);
                }
            }
            else
            {
                path = context.Server.MapPath(path);
            }

            using (var img = fileStream == Stream.Null ? new ImageCls(path) : new ImageCls(fileStream))
            {
                var bit = img.ResizeImg(width, height);
                var encoderParameters = new EncoderParameters(1);
                encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 88L);
                var encoder = GetEncoderInfo(ext);
                bit.Save(resp.OutputStream, encoder, encoderParameters);
            }
        }