file_manager_json1.ashx 5.8 KB

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