PageRenderTime 14ms CodeModel.GetById 6ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/admin/FileManager/FileUpload.ashx

#
Unknown | 105 lines | 90 code | 15 blank | 0 comment | 0 complexity | 30c6fae8f89977b7179de07a76af29a2 MD5 | raw file
  1<%@ WebHandler Language="C#" Class="FileUpload" %>
  2
  3using System;
  4using System.Web;
  5using System.Web.Hosting;
  6using System.Linq;
  7using System.IO;
  8using System.Web.SessionState;
  9using BlogEngine.Core;
 10using BlogEngine.Core.Providers;
 11public class FileUpload : IHttpHandler, IRequiresSessionState {
 12
 13    public enum FileType
 14    {
 15        Image,
 16        File
 17    }
 18    
 19    public void ProcessRequest(HttpContext context)
 20    {
 21        var files = context.Request.Files;
 22        if (files.Count == 0)
 23        {
 24            context.Response.Write("0:Unknown file upload");
 25            context.Response.End();
 26        }
 27        
 28        var file = files[0];
 29        var dirName = string.Format("/{0}/{1}", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"));
 30        var dir = BlogService.GetDirectory(dirName);  
 31        
 32        var uFile = BlogService.UploadFile(file.InputStream, file.FileName, dir, true);
 33        if (uFile.IsImage)
 34        {
 35            var imgString = string.Format("<img src=\"{0}\" />", uFile.AsImage.ImageUrl);
 36            context.Response.Write(string.Format("1:{0}:{1}:{2}", imgString, uFile.Name, uFile.ParentDirectory.FullPath));
 37            context.Response.End();
 38        }
 39        else
 40        {
 41            var fileString = string.Format("<p><a href=\"{0}\" >{1}</a></p>", uFile.FileDownloadPath, uFile.FileDescription);
 42            context.Response.Write(string.Format("1:{0}:{1}:{2}", fileString, uFile.Name, uFile.ParentDirectory.FullPath));
 43            context.Response.End();
 44        }
 45        
 46        context.Response.Write("0:Upload failed..");
 47    }
 48 
 49    public bool IsReusable {
 50        get {
 51            return false;
 52        }
 53    }
 54
 55
 56    private FileType ExtractType(string Extension)
 57    {
 58        if (Extension == ".png" ||
 59            Extension == ".jpg" ||
 60            Extension == ".jpeg" ||
 61            Extension == ".bmp" ||
 62            Extension == ".gif")
 63            return FileType.Image;
 64        else
 65            return FileType.File;
 66    }
 67    
 68    /// <summary>
 69    /// Sizes the format.
 70    /// </summary>
 71    /// <param name="size">
 72    /// The string size.
 73    /// </param>
 74    /// <param name="formatString">
 75    /// The format string.
 76    /// </param>
 77    /// <returns>
 78    /// The string.
 79    /// </returns>
 80    private static string SizeFormat(float size, string formatString)
 81    {
 82        if (size < 1024)
 83        {
 84            return string.Format("{0} bytes", size.ToString(formatString));
 85        }
 86
 87        if (size < Math.Pow(1024, 2))
 88        {
 89            return string.Format("{0} kb", (size / 1024).ToString(formatString));
 90        }
 91
 92        if (size < Math.Pow(1024, 3))
 93        {
 94            return string.Format("{0} mb", (size / Math.Pow(1024, 2)).ToString(formatString));
 95        }
 96
 97        if (size < Math.Pow(1024, 4))
 98        {
 99            return string.Format("{0} gb", (size / Math.Pow(1024, 3)).ToString(formatString));
100        }
101
102        return size.ToString(formatString);
103    }
104
105}