Guest User

Untitled

a guest
Oct 11th, 2013
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Web.Mvc;
  6.  
  7. namespace Phoenix.Client.Controllers
  8. {
  9.     public class WeltmeisterController : Controller
  10.     {
  11.         #region Models
  12.         class WMBrowseRequest
  13.         {
  14.             public string dir { get; set; }
  15.             public string type { get; set; }
  16.         }
  17.         #endregion
  18.  
  19.         // GET: /Weltmeister/
  20.         public ActionResult Index()
  21.         {
  22.             return View();
  23.         }
  24.  
  25.         [AcceptVerbs("GET", "POST")]
  26.         public JsonResult Glob()
  27.         {
  28.             List<string> globs = new List<string>();
  29.             List<string> files = new List<string>();
  30.  
  31.             if (Request.Params["glob[]"] != null)
  32.             {
  33.                 string glob = Request.Params["glob[]"];
  34.                 string path = Server.MapPath(
  35.                     "~/" + glob.Substring(0, glob.IndexOf('*'))
  36.                     );
  37.                 string pattern = string.Format("*{0}", Path.GetExtension(glob));
  38.  
  39.                 files.AddRange(
  40.                     new DirectoryInfo(path).GetFiles(pattern).Select(f => f.FullName.Replace(Request.PhysicalApplicationPath, string.Empty).Replace("\\", "/")).ToList()
  41.                 );
  42.             }
  43.             else if (Request.Params["glob[0]"] != null)
  44.             {
  45.                 foreach (var key in Request.Params.AllKeys.Where(param => param.Contains("glob")))
  46.                 {
  47.                     string glob = Request.Params[key];
  48.                     string path = Path.GetFullPath(glob);
  49.                     string pattern = string.Format("*{0}", Path.GetExtension(glob));
  50.  
  51.                     // get all the files
  52.                     files.AddRange(
  53.                         new DirectoryInfo(glob).GetFiles(pattern).Select(f => f.FullName.Replace(Request.PhysicalApplicationPath, string.Empty).Replace("\\", "/")).ToList()
  54.                     );
  55.                 }
  56.             }
  57.  
  58.             return Json(files, JsonRequestBehavior.AllowGet);
  59.         }
  60.  
  61.         [AcceptVerbs("GET", "POST")]
  62.         public JsonResult Save(string path, string data)
  63.         {
  64.             string serverPath = Server.MapPath("~/" + path);
  65.  
  66.             try
  67.             {
  68.                 using (FileStream fs = new FileStream(serverPath, FileMode.Create, FileAccess.ReadWrite))
  69.                 {
  70.                     using (StreamWriter writer = new StreamWriter(fs))
  71.                     {
  72.                         writer.Write(data);
  73.                         writer.Flush();
  74.                         writer.Close();
  75.                     }
  76.                 }
  77.             }
  78.             catch (Exception ex)
  79.             {
  80.                 // failure
  81.                 return Json(new
  82.                 {
  83.                     error = 1,
  84.                     msg = ex.ToString()
  85.                 }, JsonRequestBehavior.AllowGet);
  86.             }
  87.  
  88.             // success
  89.             return Json(new { error = 0, msg = "" }, JsonRequestBehavior.AllowGet);
  90.         }
  91.  
  92.         [AcceptVerbs("GET", "POST")]
  93.         public JsonResult Browse(string dir, string type)
  94.         {
  95.             List<string> files = new List<string>();
  96.             List<string> dirs = new List<string>();
  97.  
  98.             // map requested directory to server path
  99.             string serverPath = Server.MapPath(string.Format("~/{0}", dir));
  100.  
  101.             // get directories
  102.             Directory.GetDirectories(serverPath).ToList().ForEach((d) =>
  103.             {
  104.                 dirs.Add(
  105.                     d.Replace(Request.PhysicalApplicationPath, String.Empty).Replace("\\", "/")
  106.                 );
  107.             });
  108.            
  109.             // type can be "", "images", or "scripts"
  110.             List<string> fileTypes = new List<string>();
  111.             if (string.IsNullOrWhiteSpace(type))
  112.                 fileTypes = new List<string>() {
  113.                     "*.*"
  114.                 };
  115.             else if (type.Equals("images"))
  116.             {
  117.                 fileTypes = new List<string>() {
  118.                     "*.png", "*.gif", "*.jpg", "*.jpeg"
  119.                 };
  120.             }
  121.             else if (type.Equals("scripts"))
  122.             {
  123.                 fileTypes = new List<string>() {
  124.                     "*.js"
  125.                 };
  126.             }
  127.  
  128.             fileTypes.ForEach((pattern) =>
  129.             {
  130.                 files.AddRange(
  131.                     new DirectoryInfo(serverPath).GetFiles(pattern).Select(f => f.FullName.Replace(Request.PhysicalApplicationPath, string.Empty).Replace("\\", "/")).ToList()
  132.                 );
  133.             });
  134.  
  135.             string parent = "";
  136.             if (string.IsNullOrWhiteSpace(dir) == false)
  137.             {
  138.                 parent = Directory.GetParent(Server.MapPath("~/" + dir)).FullName + "\\";
  139.                 parent = parent.Replace(Request.PhysicalApplicationPath, String.Empty);
  140.                 parent = parent.Replace("\\", "/");
  141.             }
  142.             if (parent == dir)
  143.                 parent = "";
  144.  
  145.             var data = new
  146.             {
  147.                 parent = parent,
  148.                 dirs = dirs,
  149.                 files = files
  150.             };
  151.  
  152.             return Json(data, JsonRequestBehavior.AllowGet);
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment