|
| 1 | +<%@ webhandler Language="C#" class="FileManager" %> |
| 2 | + |
| 3 | +/** |
| 4 | + * KindEditor ASP.NET |
| 5 | + * |
| 6 | + * 本ASP.NET程序是演示程序,建议不要直接在实际项目中使用。 |
| 7 | + * 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。 |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +using System; |
| 12 | +using System.Collections; |
| 13 | +using System.Web; |
| 14 | +using System.IO; |
| 15 | +using System.Text.RegularExpressions; |
| 16 | +using LitJson; |
| 17 | +using System.Collections.Generic; |
| 18 | + |
| 19 | +public class FileManager : IHttpHandler |
| 20 | +{ |
| 21 | + public void ProcessRequest(HttpContext context) |
| 22 | + { |
| 23 | + String aspxUrl = context.Request.Path.Substring(0, context.Request.Path.LastIndexOf("/") + 1); |
| 24 | + |
| 25 | + //根目录路径,相对路径 |
| 26 | + String rootPath = "../attached/"; |
| 27 | + //根目录URL,可以指定绝对路径,比如 http://www.yoursite.com/attached/ |
| 28 | + String rootUrl = aspxUrl + "../attached/"; |
| 29 | + //图片扩展名 |
| 30 | + String fileTypes = "gif,jpg,jpeg,png,bmp"; |
| 31 | + |
| 32 | + String currentPath = ""; |
| 33 | + String currentUrl = ""; |
| 34 | + String currentDirPath = ""; |
| 35 | + String moveupDirPath = ""; |
| 36 | + |
| 37 | + String dirPath = context.Server.MapPath(rootPath); |
| 38 | + String dirName = context.Request.QueryString["dir"]; |
| 39 | + if (!String.IsNullOrEmpty(dirName)) { |
| 40 | + if (Array.IndexOf("image,flash,media,file".Split(','), dirName) == -1) { |
| 41 | + context.Response.Write("Invalid Directory name."); |
| 42 | + context.Response.End(); |
| 43 | + } |
| 44 | + dirPath += dirName + "/"; |
| 45 | + rootUrl += dirName + "/"; |
| 46 | + if (!Directory.Exists(dirPath)) { |
| 47 | + Directory.CreateDirectory(dirPath); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + //根据path参数,设置各路径和URL |
| 52 | + String path = context.Request.QueryString["path"]; |
| 53 | + path = String.IsNullOrEmpty(path) ? "" : path; |
| 54 | + if (path == "") |
| 55 | + { |
| 56 | + currentPath = dirPath; |
| 57 | + currentUrl = rootUrl; |
| 58 | + currentDirPath = ""; |
| 59 | + moveupDirPath = ""; |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + currentPath = dirPath + path; |
| 64 | + currentUrl = rootUrl + path; |
| 65 | + currentDirPath = path; |
| 66 | + moveupDirPath = Regex.Replace(currentDirPath, @"(.*?)[^\/]+\/$", "$1"); |
| 67 | + } |
| 68 | + |
| 69 | + //排序形式,name or size or type |
| 70 | + String order = context.Request.QueryString["order"]; |
| 71 | + order = String.IsNullOrEmpty(order) ? "" : order.ToLower(); |
| 72 | + |
| 73 | + //不允许使用..移动到上一级目录 |
| 74 | + if (Regex.IsMatch(path, @"\.\.")) |
| 75 | + { |
| 76 | + context.Response.Write("Access is not allowed."); |
| 77 | + context.Response.End(); |
| 78 | + } |
| 79 | + //最后一个字符不是/ |
| 80 | + if (path != "" && !path.EndsWith("/")) |
| 81 | + { |
| 82 | + context.Response.Write("Parameter is not valid."); |
| 83 | + context.Response.End(); |
| 84 | + } |
| 85 | + //目录不存在或不是目录 |
| 86 | + if (!Directory.Exists(currentPath)) |
| 87 | + { |
| 88 | + context.Response.Write("Directory does not exist."); |
| 89 | + context.Response.End(); |
| 90 | + } |
| 91 | + |
| 92 | + //遍历目录取得文件信息 |
| 93 | + string[] dirList = Directory.GetDirectories(currentPath); |
| 94 | + string[] fileList = Directory.GetFiles(currentPath); |
| 95 | + |
| 96 | + switch (order) |
| 97 | + { |
| 98 | + case "size": |
| 99 | + Array.Sort(dirList, new NameSorter()); |
| 100 | + Array.Sort(fileList, new SizeSorter()); |
| 101 | + break; |
| 102 | + case "type": |
| 103 | + Array.Sort(dirList, new NameSorter()); |
| 104 | + Array.Sort(fileList, new TypeSorter()); |
| 105 | + break; |
| 106 | + case "name": |
| 107 | + default: |
| 108 | + Array.Sort(dirList, new NameSorter()); |
| 109 | + Array.Sort(fileList, new NameSorter()); |
| 110 | + break; |
| 111 | + } |
| 112 | + |
| 113 | + Hashtable result = new Hashtable(); |
| 114 | + result["moveup_dir_path"] = moveupDirPath; |
| 115 | + result["current_dir_path"] = currentDirPath; |
| 116 | + result["current_url"] = currentUrl; |
| 117 | + result["total_count"] = dirList.Length + fileList.Length; |
| 118 | + List<Hashtable> dirFileList = new List<Hashtable>(); |
| 119 | + result["file_list"] = dirFileList; |
| 120 | + for (int i = 0; i < dirList.Length; i++) |
| 121 | + { |
| 122 | + DirectoryInfo dir = new DirectoryInfo(dirList[i]); |
| 123 | + Hashtable hash = new Hashtable(); |
| 124 | + hash["is_dir"] = true; |
| 125 | + hash["has_file"] = (dir.GetFileSystemInfos().Length > 0); |
| 126 | + hash["filesize"] = 0; |
| 127 | + hash["is_photo"] = false; |
| 128 | + hash["filetype"] = ""; |
| 129 | + hash["filename"] = dir.Name; |
| 130 | + hash["datetime"] = dir.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"); |
| 131 | + dirFileList.Add(hash); |
| 132 | + } |
| 133 | + for (int i = 0; i < fileList.Length; i++) |
| 134 | + { |
| 135 | + FileInfo file = new FileInfo(fileList[i]); |
| 136 | + Hashtable hash = new Hashtable(); |
| 137 | + hash["is_dir"] = false; |
| 138 | + hash["has_file"] = false; |
| 139 | + hash["filesize"] = file.Length; |
| 140 | + hash["is_photo"] = (Array.IndexOf(fileTypes.Split(','), file.Extension.Substring(1).ToLower()) >= 0); |
| 141 | + hash["filetype"] = file.Extension.Substring(1); |
| 142 | + hash["filename"] = file.Name; |
| 143 | + hash["datetime"] = file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"); |
| 144 | + dirFileList.Add(hash); |
| 145 | + } |
| 146 | + context.Response.AddHeader("Content-Type", "application/json; charset=UTF-8"); |
| 147 | + context.Response.Write(JsonMapper.ToJson(result)); |
| 148 | + context.Response.End(); |
| 149 | + } |
| 150 | + |
| 151 | + public class NameSorter : IComparer |
| 152 | + { |
| 153 | + public int Compare(object x, object y) |
| 154 | + { |
| 155 | + if (x == null && y == null) |
| 156 | + { |
| 157 | + return 0; |
| 158 | + } |
| 159 | + if (x == null) |
| 160 | + { |
| 161 | + return -1; |
| 162 | + } |
| 163 | + if (y == null) |
| 164 | + { |
| 165 | + return 1; |
| 166 | + } |
| 167 | + FileInfo xInfo = new FileInfo(x.ToString()); |
| 168 | + FileInfo yInfo = new FileInfo(y.ToString()); |
| 169 | + |
| 170 | + return xInfo.FullName.CompareTo(yInfo.FullName); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + public class SizeSorter : IComparer |
| 175 | + { |
| 176 | + public int Compare(object x, object y) |
| 177 | + { |
| 178 | + if (x == null && y == null) |
| 179 | + { |
| 180 | + return 0; |
| 181 | + } |
| 182 | + if (x == null) |
| 183 | + { |
| 184 | + return -1; |
| 185 | + } |
| 186 | + if (y == null) |
| 187 | + { |
| 188 | + return 1; |
| 189 | + } |
| 190 | + FileInfo xInfo = new FileInfo(x.ToString()); |
| 191 | + FileInfo yInfo = new FileInfo(y.ToString()); |
| 192 | + |
| 193 | + return xInfo.Length.CompareTo(yInfo.Length); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + public class TypeSorter : IComparer |
| 198 | + { |
| 199 | + public int Compare(object x, object y) |
| 200 | + { |
| 201 | + if (x == null && y == null) |
| 202 | + { |
| 203 | + return 0; |
| 204 | + } |
| 205 | + if (x == null) |
| 206 | + { |
| 207 | + return -1; |
| 208 | + } |
| 209 | + if (y == null) |
| 210 | + { |
| 211 | + return 1; |
| 212 | + } |
| 213 | + FileInfo xInfo = new FileInfo(x.ToString()); |
| 214 | + FileInfo yInfo = new FileInfo(y.ToString()); |
| 215 | + |
| 216 | + return xInfo.Extension.CompareTo(yInfo.Extension); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + public bool IsReusable |
| 221 | + { |
| 222 | + get |
| 223 | + { |
| 224 | + return true; |
| 225 | + } |
| 226 | + } |
| 227 | +} |
0 commit comments