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