Example #1
0
        public APIResult Up(string folder, string fileName)
        {
            var exts = new List <string> {
                ".jpg", ".png", ".gif", ".mp3", ".mp4", ".txt", ".md"
            };
            var ext = fileName.GetFileExtension();

            if (!exts.Contains(ext))
            {
                return(new APIResult(false, "File deny", 13));
            }

            // 构造存储路径
            var url  = GetUploadPath(folder, fileName);
            var path = Asp.MapPath(url);
            var fi   = new FileInfo(path);

            if (!fi.Directory.Exists)
            {
                Directory.CreateDirectory(fi.Directory.FullName);
            }

            // 存储第一个文件
            var files = Asp.Request.Form.Files;

            if (files.Count == 0)
            {
                return(new APIResult(false, "File doesn't exist", 11));
            }
            using (var stream = File.Create(path))
                files[0].CopyToAsync(stream);
            return(new APIResult(true, url));
        }
Example #2
0
        public async Task Invoke(HttpContext context)
        {
            var path = context.Request.Path.Value;  // 无 host 和 querystring。如:/res/icon/add.png
            var ext  = path.GetFileExtension();

            if (!Extensions.Contains(ext))
            {
                await _next.Invoke(context);

                return;
            }

            // 原图路径校验
            var rawPath = Asp.MapPath(path);

            if (!File.Exists(rawPath))
            {
                //Asp.Error(404, "Not found");  // 因为 wwwroot 的原因,MapPath 出来的路径可能不对,就不做处理了
                await _next.Invoke(context);

                return;
            }

            // 原图输出
            var pathAndQuery = path + context.Request.QueryString;
            var mimeType     = path.GetMimeType();
            var w            = Asp.GetQueryInt("w");

            if (w == null)
            {
                Asp.WriteFile(rawPath, mimeType: mimeType);
                return;
            }

            // 缩略图参数
            var h = Asp.GetQueryInt("h");

            if (w > 1000)
            {
                w = 1000;
            }
            if (h != null && h > 1000)
            {
                h = 1000;
            }
            var key = pathAndQuery.ToLower().MD5();

            // 缩略图缓存策略
            var cachePath = Asp.MapPath(string.Format("/Caches/{0}.cache", key));

            if (!File.Exists(cachePath))
            {
                IO.PrepareDirectory(cachePath);
                var img = Painter.Thumbnail(rawPath, w.Value, h);
                img.Save(cachePath);
                img.Dispose();
            }
            Asp.WriteFile(cachePath, mimeType: mimeType);
        }
Example #3
0
        /// <summary>处理文件资源</summary>
        public void HandleFileRes(string url)
        {
            // 文件扩展名及推断属性
            this.FileExtension = url.GetFileExtension();
            this.MimeType      = IO.GetMimeType(this.FileExtension);
            this.Type          = this.MimeType.Contains("image") ? ResType.Image : ResType.File;

            // 物理文件属性
            if (url.IsSiteFile())
            {
                try
                {
                    var physicalPath = Asp.MapPath(url);
                    var fi           = new FileInfo(physicalPath);
                    this.FileSize = fi.Length;
                    this.FileMD5  = IO.GetFileMD5(physicalPath);
                }
                catch (Exception e)
                {
                    UtilConfig.Log("GetFileInfoFail", e.Message);
                };
            }
        }