PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/admin/FileManager/AjaxFileManager.aspx.cs

#
C# | 318 lines | 289 code | 22 blank | 7 comment | 12 complexity | 68b1ede4782f4c2903633cd73e182d48 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Hosting;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.Services;
  9. using System.Collections;
  10. using App_Code;
  11. using BlogEngine.Core.Json;
  12. using BlogEngine.Core;
  13. using System.IO;
  14. using System.Text;
  15. using System.Drawing;
  16. using System.Drawing.Drawing2D;
  17. using BlogEngine.Core.Providers;
  18. public partial class admin_AjaxFileManager : System.Web.UI.Page
  19. {
  20. [WebMethod]
  21. public static JsonResponse ChangeImage(string path, int change)
  22. {
  23. JsonResponse response = new JsonResponse()
  24. {
  25. Data = string.Empty,
  26. Success = false,
  27. Message = ""
  28. };
  29. //check rights
  30. if (!Security.IsAuthorizedTo(Rights.CreateNewPosts))
  31. {
  32. response.Success = false;
  33. response.Message = "Access denied";
  34. return response;
  35. }
  36. var itemChange = BlogEngine.Core.FileSystem.Image.ModificationType.FlipHorizontal;
  37. switch (change)
  38. {
  39. case 0:
  40. itemChange = BlogEngine.Core.FileSystem.Image.ModificationType.FlipHorizontal;
  41. break;
  42. case 1:
  43. itemChange = BlogEngine.Core.FileSystem.Image.ModificationType.FlipVertical;
  44. break;
  45. case 2:
  46. itemChange = BlogEngine.Core.FileSystem.Image.ModificationType.RotateLeft;
  47. break;
  48. case 3:
  49. itemChange = BlogEngine.Core.FileSystem.Image.ModificationType.RotateRight;
  50. break;
  51. }
  52. BlogService.GetFile(path).AsImage.ModifyImage(itemChange);
  53. return new JsonResponse()
  54. {
  55. Success = true,
  56. Message = "",
  57. Data = ""
  58. };
  59. }
  60. [WebMethod]
  61. public static JsonResponse ResizeImage(string path, int percent)
  62. {
  63. JsonResponse response = new JsonResponse()
  64. {
  65. Data = string.Empty,
  66. Success = false,
  67. Message = ""
  68. };
  69. //check rights
  70. if (!Security.IsAuthorizedTo(Rights.CreateNewPosts))
  71. {
  72. response.Success = false;
  73. response.Message = "Access denied";
  74. return response;
  75. }
  76. BlogService.GetFile(path).AsImage.ResizeImage((decimal)percent);
  77. return new JsonResponse()
  78. {
  79. Success = true,
  80. Data = "",
  81. Message = ""
  82. };
  83. }
  84. [WebMethod]
  85. public static JsonResponse CropImage(string path, int x, int y, int width, int height)
  86. {
  87. JsonResponse response = new JsonResponse()
  88. {
  89. Data = string.Empty,
  90. Success = false,
  91. Message = ""
  92. };
  93. //check rights
  94. if (!Security.IsAuthorizedTo(Rights.CreateNewPosts))
  95. {
  96. response.Success = false;
  97. response.Message = "Access denied";
  98. return response;
  99. }
  100. BlogService.GetFile(path).AsImage.CropImage(x, y, width, height);
  101. return new JsonResponse()
  102. {
  103. Success = true,
  104. Data = path
  105. };
  106. }
  107. [WebMethod]
  108. public static JsonResponse RenameFile(string path, string newname)
  109. {
  110. JsonResponse response = new JsonResponse()
  111. {
  112. Data = string.Empty,
  113. Success = false,
  114. Message = ""
  115. };
  116. //check rights
  117. if (!Security.IsAuthorizedTo(Rights.CreateNewPosts))
  118. {
  119. response.Success = false;
  120. response.Message = "Access denied";
  121. return response;
  122. }
  123. try
  124. {
  125. var file = BlogService.GetFile(path).Rename(newname);
  126. return (response = new JsonResponse()
  127. {
  128. Data = file.ParentDirectory.FullPath,
  129. Success = true,
  130. Message = "File \"" + path + "\" has been renamed to \"" + newname + "\"."
  131. });
  132. }
  133. catch
  134. {
  135. return new JsonResponse()
  136. {
  137. Success = false,
  138. Message = "There was an error while renaming your file, please try again or specify a new name."
  139. };
  140. }
  141. }
  142. [WebMethod]
  143. public static JsonResponse DeleteFile(string path)
  144. {
  145. JsonResponse response = new JsonResponse()
  146. {
  147. Data = string.Empty,
  148. Success = false,
  149. Message = ""
  150. };
  151. //check rights
  152. if (!Security.IsAuthorizedTo(Rights.CreateNewPosts))
  153. {
  154. response.Success = false;
  155. response.Message = "Access denied";
  156. return response;
  157. }
  158. var file = BlogService.GetFile(path);
  159. var directory = file.ParentDirectory;
  160. file.Delete();
  161. return (response = new JsonResponse()
  162. {
  163. Data = directory.FullPath,
  164. Success = true,
  165. Message = "File \"" + new FileInfo(path).Name + "\" has been deleted."
  166. });
  167. }
  168. [WebMethod]
  169. public static JsonResponse AppendFile(string path)
  170. {
  171. JsonResponse response = new JsonResponse()
  172. {
  173. Data = string.Empty,
  174. Success = false,
  175. Message = ""
  176. };
  177. //check rights
  178. if (!Security.IsAuthorizedTo(Rights.CreateNewPosts))
  179. {
  180. response.Success = false;
  181. response.Message = "Access denied";
  182. return response;
  183. }
  184. var file = BlogService.GetFile(path);
  185. if (file != null)
  186. {
  187. if (file.IsImage)
  188. {
  189. var imagePath = string.Format("<img src=\"{0}\" />", file.AsImage.ImageUrl);
  190. response.Success = true;
  191. response.Data = imagePath;
  192. }
  193. else
  194. {
  195. var filePath = string.Format("<p><a href=\"{1}\" >{0}</a></p>", file.FileDescription, file.FileDownloadPath);
  196. response.Success = true;
  197. response.Data = filePath;
  198. }
  199. }
  200. return response;
  201. }
  202. [WebMethod]
  203. public static JsonResponse<FileResponse> GetFiles(string path)
  204. {
  205. JsonResponse<FileResponse> response = new JsonResponse<FileResponse>()
  206. {
  207. Data = new FileResponse(),
  208. Success = false,
  209. Message = ""
  210. };
  211. //check rights
  212. if (!Security.IsAuthorizedTo(Rights.CreateNewPosts)) {
  213. response.Success = false;
  214. response.Message = "Access denied";
  215. return response;
  216. }
  217. var rwr = Utils.RelativeWebRoot;
  218. List<FileInstance> list = new List<FileInstance>();
  219. var directory = BlogService.GetDirectory(path);
  220. if (!directory.IsRoot)
  221. {
  222. list.Add(new FileInstance()
  223. {
  224. FileSize = "",
  225. FileType = FileType.Directory,
  226. Created = "",
  227. FullPath = "",
  228. Image = rwr + "admin/filemanager/images/up-directory.png",
  229. Name = "..."
  230. });
  231. response.Data.Path = "root" + directory.FullPath;
  232. }
  233. else
  234. response.Data.Path = "root";
  235. foreach (var dir in directory.Directories)
  236. list.Add(new FileInstance()
  237. {
  238. FileSize = "",
  239. FileType = FileType.Directory,
  240. Created = dir.DateCreated.ToString(),
  241. FullPath = dir.FullPath,
  242. Image = rwr + "admin/filemanager/images/folder.png",
  243. Name = dir.Name.Replace("/","")
  244. });
  245. foreach (var file in directory.Files)
  246. list.Add(new FileInstance()
  247. {
  248. FileSize = file.FileSizeFormat,
  249. Created = file.DateCreated.ToString(),
  250. FileType = FileType.File,
  251. Image = !file.IsImage ? rwr + "admin/filemanager/images/page_white_world.png" : rwr + "admin/filemanager/images/page_white_picture.png",
  252. FullPath = file.FilePath,
  253. Name = file.Name
  254. });
  255. var pathPieces = response.Data.Path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
  256. StringBuilder pathString = new StringBuilder();
  257. pathString.Append("<a href=\"javascript:;\" class=\"fmdPathPiece\" data-path=\"\">root</a>");
  258. var hPath = "";
  259. foreach (var p in pathPieces.Skip(1))
  260. {
  261. hPath = string.Format("{0}/{1}", hPath, p);
  262. pathString.AppendFormat("/<a href=\"javascript:;\" class=\"fmdPathPiece\" data-path=\"{0}\">{1}</a>", hPath, p);
  263. }
  264. response.Data.Path = pathString.ToString();
  265. response.Success = true;
  266. response.Message = string.Empty;
  267. response.Data.Files = list;
  268. return response;
  269. }
  270. public class FileResponse
  271. {
  272. public FileResponse()
  273. {
  274. Files = new List<FileInstance>();
  275. Path = string.Empty;
  276. }
  277. public IEnumerable<FileInstance> Files { get; set; }
  278. public string Path { get; set; }
  279. }
  280. public class FileInstance
  281. {
  282. public string Image { get; set; }
  283. public string Created { get; set; }
  284. public string Name { get; set; }
  285. public string FileSize { get; set; }
  286. public FileType FileType { get; set; }
  287. public string FullPath { get; set; }
  288. }
  289. public enum FileType
  290. {
  291. Directory,
  292. File,
  293. Image,
  294. None
  295. }
  296. }