PageRenderTime 51ms CodeModel.GetById 4ms RepoModel.GetById 1ms app.codeStats 0ms

/aliaksei-dziadzishchau/WebProject/FileServer/Controllers/FileController.cs

http://asp-dot-net-training-project.googlecode.com/
C# | 277 lines | 210 code | 47 blank | 20 comment | 13 complexity | 5b5d965fa43e14c62fa6fd1fa9f64588 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using FileServer.Models;
  7. using System.Data;
  8. using System.Web.UI.WebControls;
  9. namespace FileServer.Controllers
  10. {
  11. public class FileController : Controller
  12. {
  13. private DataManager _dataManager;
  14. //static string rootDir = @"..\..\App_Data\Files\";
  15. static string rootDir = @"c:\windows\temp\";
  16. public FileController(DataManager dataManager)
  17. {
  18. _dataManager = dataManager;
  19. }
  20. //
  21. // GET: /File/
  22. public ActionResult Index()
  23. {
  24. return View();
  25. }
  26. //
  27. // GET: /File/Details/5
  28. public ActionResult Details(int id)
  29. {
  30. return View();
  31. }
  32. //
  33. // GET: /File/Create
  34. public ActionResult Create()
  35. {
  36. return View();
  37. }
  38. //
  39. // POST: /File/Create
  40. [AcceptVerbs(HttpVerbs.Post)]
  41. public ActionResult Create(HttpPostedFileBase newFile)
  42. {
  43. try
  44. {
  45. Guid userID = new Guid();
  46. File f = _dataManager.Files.AddFile(new File { name = newFile.FileName, size = newFile.ContentLength, dateUpload = DateTime.Now, dateDelete = DateTime.Now.AddDays(60) });
  47. if (User.Identity.IsAuthenticated)
  48. {
  49. userID = _dataManager.Members.GetUser(User.Identity.Name).UserId;
  50. _dataManager.Files.SetFileOfUser(f.fileId, userID);
  51. }
  52. string fileNameToSave = rootDir + f.fileId;
  53. newFile.SaveAs(fileNameToSave);
  54. ViewData["mess"] = fileNameToSave;
  55. return RedirectToAction("Links/" + f.fileId);
  56. }
  57. catch (Exception ex)
  58. {
  59. ViewData["mess"] = ex.Message;
  60. return View();
  61. }
  62. }
  63. public ActionResult Links(int id)
  64. {
  65. File f = _dataManager.Files.GetFile(id);
  66. string _dir = "File/";
  67. string _delete = "Delete/";
  68. string _download = "Download/";
  69. if (f != null)
  70. {
  71. if (_dataManager.Files.GetFileInfo(id) == null)
  72. {
  73. f.FileInfo = new FileInfo { about = "" };
  74. }
  75. ViewData["FileName"] = f.name;
  76. string url = Request.Url.Authority + "/";
  77. string download = url + _dir + _download + id;
  78. ViewData["download"] = download;
  79. ViewData["delete"] = url + _dir + _delete + id;
  80. ViewData["html"] = string.Format("<a href=\"{0}\">Скачать {1}</a>", download, f.name);
  81. ViewData["forum"] = string.Format("[url=\"{0}\"]Скачать {1}[/url]", download, f.name);
  82. }
  83. else
  84. {
  85. RedirectToAction("Show/1", "Error");
  86. }
  87. return View(f);
  88. }
  89. //
  90. // GET: /File/Edit/5
  91. public ActionResult Edit(int id)
  92. {
  93. return View();
  94. }
  95. //
  96. // POST: /File/Edit/5
  97. [HttpPost]
  98. public ActionResult Edit(int id, FormCollection collection)
  99. {
  100. try
  101. {
  102. // TODO: Add update logic here
  103. return RedirectToAction("Index");
  104. }
  105. catch
  106. {
  107. return View();
  108. }
  109. }
  110. //
  111. // GET: /File/Delete/5
  112. public ActionResult Delete(int id)
  113. {
  114. return View();
  115. }
  116. //
  117. // POST: /File/Delete/5
  118. [AcceptVerbs(HttpVerbs.Post)]
  119. public ActionResult Delete(int id, FormCollection collection)
  120. {
  121. try
  122. {
  123. // TODO: Add delete logic here
  124. var userId = _dataManager.Members.GetUser(User.Identity.Name).UserId;
  125. //_dataManager.Files.DeleteFileByUser(id, userId);
  126. if (Request.IsAjaxRequest())
  127. {
  128. return JavaScript("");
  129. }
  130. return View();
  131. }
  132. catch(Exception ex)
  133. {
  134. return Content(ex.Message);
  135. }
  136. }
  137. [AcceptVerbs(HttpVerbs.Post)]
  138. public ActionResult SaveAbout(File f, int id)
  139. {
  140. _dataManager.Files.UpdateFileInfo(new FileInfo { about = f.FileInfo.about, fileId = f.FileInfo.fileId });
  141. return Content(f.FileInfo.about);
  142. }
  143. [AcceptVerbs(HttpVerbs.Get)]
  144. public ActionResult Download(int id)
  145. {
  146. if (_dataManager.Files.GetFiles().Any(x => x.fileId == id))
  147. {
  148. var file = _dataManager.Files.GetFile(id);
  149. if (file.isPassword)
  150. {
  151. ViewData["mess"] = "";
  152. ViewData["isPassword"] = file.isPassword;
  153. return View();
  154. }
  155. else
  156. {
  157. return File(rootDir + id.ToString(), "file", file.name);
  158. }
  159. }
  160. else
  161. {
  162. return RedirectToAction("Show/2", "Error");
  163. }
  164. }
  165. [AcceptVerbs(HttpVerbs.Post)]
  166. public ActionResult Download(FormCollection form, int id)
  167. {
  168. if (_dataManager.Files.GetFiles().Any(x => x.fileId == id))
  169. {
  170. var file = _dataManager.Files.GetFile(id);
  171. if (file.FileInfo.password.Equals(form["pass"]))
  172. {
  173. return File(rootDir + id.ToString(), "file", file.name);
  174. }
  175. else
  176. {
  177. ViewData["mess"] = "Введен неправильный пароль.";
  178. }
  179. return View(file);
  180. }
  181. else
  182. {
  183. return RedirectToAction("Show/2", "Error");
  184. }
  185. }
  186. [AcceptVerbs(HttpVerbs.Post)]
  187. public ContentResult Rename(FormCollection file, int id)
  188. {
  189. string newName = file["newName"];
  190. _dataManager.Files.UpdateFileName(id, newName);
  191. return Content(newName);
  192. }
  193. [AcceptVerbs(HttpVerbs.Post)]
  194. public ContentResult UpdateDeleteDate(FormCollection file, int id)
  195. {
  196. var f = _dataManager.Files.GetFile(id);
  197. DateTime date = DateTime.Now.AddDays(60);
  198. _dataManager.Files.UpdateFileDeleteDate(id, date);
  199. return Content(date.ToLongDateString());
  200. }
  201. [AcceptVerbs(HttpVerbs.Post)]
  202. public ContentResult NewPassword(FormCollection file, int id)
  203. {
  204. var f = _dataManager.Files.GetFile(id);
  205. _dataManager.Files.UpdateFilePassword(id, file["newPassword"]);
  206. if (file["newPassword"].Length > 0)
  207. {
  208. return Content("1");
  209. }
  210. else
  211. {
  212. return Content("");
  213. }
  214. }
  215. [AcceptVerbs(HttpVerbs.Post)]
  216. public ContentResult SetAbout(FormCollection file, int id)
  217. {
  218. var f = _dataManager.Files.GetFile(id);
  219. try
  220. {
  221. _dataManager.Files.UpdateFileAbout(id, file["newAbout"]);
  222. }
  223. catch (Exception ex)
  224. {
  225. return Content(ex.Message);
  226. }
  227. return Content(file["newAbout"]);
  228. }
  229. }
  230. }