/aliaksei-dziadzishchau/WebProject/FileServer/Controllers/FileController.cs
C# | 277 lines | 210 code | 47 blank | 20 comment | 13 complexity | 5b5d965fa43e14c62fa6fd1fa9f64588 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using FileServer.Models;
- using System.Data;
- using System.Web.UI.WebControls;
-
- namespace FileServer.Controllers
- {
- public class FileController : Controller
- {
- private DataManager _dataManager;
-
- //static string rootDir = @"..\..\App_Data\Files\";
- static string rootDir = @"c:\windows\temp\";
- public FileController(DataManager dataManager)
- {
- _dataManager = dataManager;
- }
- //
- // GET: /File/
-
- public ActionResult Index()
- {
- return View();
- }
-
- //
- // GET: /File/Details/5
-
- public ActionResult Details(int id)
- {
- return View();
- }
-
- //
- // GET: /File/Create
-
- public ActionResult Create()
- {
- return View();
- }
-
- //
- // POST: /File/Create
-
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult Create(HttpPostedFileBase newFile)
- {
-
-
- try
- {
- Guid userID = new Guid();
-
- File f = _dataManager.Files.AddFile(new File { name = newFile.FileName, size = newFile.ContentLength, dateUpload = DateTime.Now, dateDelete = DateTime.Now.AddDays(60) });
-
- if (User.Identity.IsAuthenticated)
- {
- userID = _dataManager.Members.GetUser(User.Identity.Name).UserId;
- _dataManager.Files.SetFileOfUser(f.fileId, userID);
- }
-
- string fileNameToSave = rootDir + f.fileId;
-
- newFile.SaveAs(fileNameToSave);
-
- ViewData["mess"] = fileNameToSave;
-
- return RedirectToAction("Links/" + f.fileId);
- }
- catch (Exception ex)
- {
- ViewData["mess"] = ex.Message;
- return View();
- }
- }
-
- public ActionResult Links(int id)
- {
- File f = _dataManager.Files.GetFile(id);
-
- string _dir = "File/";
- string _delete = "Delete/";
- string _download = "Download/";
-
- if (f != null)
- {
- if (_dataManager.Files.GetFileInfo(id) == null)
- {
- f.FileInfo = new FileInfo { about = "" };
- }
- ViewData["FileName"] = f.name;
- string url = Request.Url.Authority + "/";
- string download = url + _dir + _download + id;
- ViewData["download"] = download;
- ViewData["delete"] = url + _dir + _delete + id;
- ViewData["html"] = string.Format("<a href=\"{0}\">Скачать {1}</a>", download, f.name);
- ViewData["forum"] = string.Format("[url=\"{0}\"]Скачать {1}[/url]", download, f.name);
- }
- else
- {
- RedirectToAction("Show/1", "Error");
- }
-
- return View(f);
- }
-
- //
- // GET: /File/Edit/5
-
- public ActionResult Edit(int id)
- {
- return View();
- }
-
- //
- // POST: /File/Edit/5
-
- [HttpPost]
- public ActionResult Edit(int id, FormCollection collection)
- {
- try
- {
- // TODO: Add update logic here
-
- return RedirectToAction("Index");
- }
- catch
- {
- return View();
- }
- }
-
- //
- // GET: /File/Delete/5
-
-
- public ActionResult Delete(int id)
- {
- return View();
- }
-
- //
- // POST: /File/Delete/5
-
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult Delete(int id, FormCollection collection)
- {
- try
- {
- // TODO: Add delete logic here
- var userId = _dataManager.Members.GetUser(User.Identity.Name).UserId;
- //_dataManager.Files.DeleteFileByUser(id, userId);
-
-
-
- if (Request.IsAjaxRequest())
- {
- return JavaScript("");
- }
- return View();
- }
- catch(Exception ex)
- {
- return Content(ex.Message);
- }
- }
-
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult SaveAbout(File f, int id)
- {
- _dataManager.Files.UpdateFileInfo(new FileInfo { about = f.FileInfo.about, fileId = f.FileInfo.fileId });
- return Content(f.FileInfo.about);
- }
-
- [AcceptVerbs(HttpVerbs.Get)]
- public ActionResult Download(int id)
- {
- if (_dataManager.Files.GetFiles().Any(x => x.fileId == id))
- {
- var file = _dataManager.Files.GetFile(id);
- if (file.isPassword)
- {
- ViewData["mess"] = "";
- ViewData["isPassword"] = file.isPassword;
- return View();
- }
- else
- {
- return File(rootDir + id.ToString(), "file", file.name);
- }
- }
- else
- {
- return RedirectToAction("Show/2", "Error");
- }
- }
-
-
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult Download(FormCollection form, int id)
- {
- if (_dataManager.Files.GetFiles().Any(x => x.fileId == id))
- {
- var file = _dataManager.Files.GetFile(id);
- if (file.FileInfo.password.Equals(form["pass"]))
- {
- return File(rootDir + id.ToString(), "file", file.name);
- }
- else
- {
- ViewData["mess"] = "Введен неправильный пароль.";
- }
- return View(file);
- }
- else
- {
- return RedirectToAction("Show/2", "Error");
- }
-
- }
-
-
- [AcceptVerbs(HttpVerbs.Post)]
- public ContentResult Rename(FormCollection file, int id)
- {
- string newName = file["newName"];
- _dataManager.Files.UpdateFileName(id, newName);
- return Content(newName);
- }
-
- [AcceptVerbs(HttpVerbs.Post)]
- public ContentResult UpdateDeleteDate(FormCollection file, int id)
- {
- var f = _dataManager.Files.GetFile(id);
- DateTime date = DateTime.Now.AddDays(60);
-
- _dataManager.Files.UpdateFileDeleteDate(id, date);
-
- return Content(date.ToLongDateString());
- }
-
- [AcceptVerbs(HttpVerbs.Post)]
- public ContentResult NewPassword(FormCollection file, int id)
- {
- var f = _dataManager.Files.GetFile(id);
- _dataManager.Files.UpdateFilePassword(id, file["newPassword"]);
-
- if (file["newPassword"].Length > 0)
- {
- return Content("1");
- }
- else
- {
- return Content("");
- }
- }
-
- [AcceptVerbs(HttpVerbs.Post)]
- public ContentResult SetAbout(FormCollection file, int id)
- {
- var f = _dataManager.Files.GetFile(id);
- try
- {
- _dataManager.Files.UpdateFileAbout(id, file["newAbout"]);
- }
- catch (Exception ex)
- {
- return Content(ex.Message);
- }
- return Content(file["newAbout"]);
- }
- }
- }