public ActionResult Edit(int id) { File file = _fileService.GetFileById(id); if (file == null) { return(EntityNotFoundView()); } ViewBag.Parent = file.Parent; var model = new DbRenameModel { Id = file.Id, OldName = file.Name, ParentId = file.ParentId, AccessMode = file.AccessModeShort }; ViewBag.AccessModes = new SelectList(new Dictionary <short, string> { { (short)AccessMode.Any, AccessMode.Any.ToLocalizedString() }, { (short)AccessMode.None, AccessMode.None.ToLocalizedString() }, { (short)AccessMode.OnlyAuthenticated, AccessMode.OnlyAuthenticated.ToLocalizedString() } }, "Key", "Value", model.AccessMode); return(View(model)); }
private ActionResult DeleteRecursive(File file) { if (file == null) { return(null); } foreach (File child in file.Childs.ToList()) { ActionResult result = DeleteRecursive(child); if (result != null) { return(result); } } if (!UserIsAllowedToCrud(file)) { return(AccessDeniedView()); } _fileService.DeleteFile(file); bool isSaved; try { isSaved = _unitOfWork.SaveChanges() > 0; } catch { isSaved = false; } if (isSaved) { Logger.SaveLog(new DeleteFileProvider(file.Id)); if (file.IsFile) { string filePath = Server.MapPath(Path.Combine(Constants.UploadsUrl, file.Guid.ToString())); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } } } else { TempData["Error"] = ValidationResources.DeleteDirectoryOrFileFailure; } return(null); }
public ActionResult Delete(int id) { File file = _fileService.GetFileById(id); if (file == null) { return(NotFoundView()); } int?parentId = file.ParentId; return(DeleteRecursive(file) ?? RedirectToAction("List", new { parentId, page = 1 })); }
private bool UserIsAllowedToCrud(File file) { if (UserIsUnlimited()) { return(true); } if (file.Id == 0) { return(true); } return(file.UploaderId == _webHelper.GetCurrentUser(ControllerContext.HttpContext).Id); }
public ActionResult ChangePublication(int id) { File dbFile = _fileService.GetFileById(id); if (dbFile == null) { return(EntityNotFoundView()); } if (!UserIsAllowedToCrud(dbFile)) { return(AccessDeniedView()); } dbFile.IsPublished = !dbFile.IsPublished; bool isSaved; try { isSaved = _unitOfWork.SaveChanges() > 0; } catch { isSaved = false; } if (isSaved) { Logger.SaveLog(new FileChangePublishProvider(dbFile)); } else { TempData["Error"] = ValidationResources.UpdateFailure; } if (IsReferrerValid()) { return(Redirect(Request.UrlReferrer.AbsolutePath)); } return(RedirectToAction("List", new { page = 1, dbFile.ParentId })); }
public ActionResult UploadFile(int?parentId) { UploadDbFileModel model; if (parentId.HasValue) { File parent = _fileService.GetFileById(parentId.Value); if (parent == null) { return(NotFoundView()); } if (!UserIsAllowedToCrud(parent)) { return(AccessDeniedView()); } ViewBag.Parent = parent; model = new UploadDbFileModel { ParentId = parent.Id }; } else { model = new UploadDbFileModel(); } ViewBag.AccessModes = new SelectList(new Dictionary <short, string> { { (short)AccessMode.Any, AccessMode.Any.ToLocalizedString() }, { (short)AccessMode.None, AccessMode.None.ToLocalizedString() }, { (short)AccessMode.OnlyAuthenticated, AccessMode.OnlyAuthenticated.ToLocalizedString() } }, "Key", "Value"); return(View(model)); }
public ActionResult Edit(DbRenameModel model) { File file = _fileService.GetFileById(model.Id); if (file == null) { return(EntityNotFoundView()); } ViewBag.Parent = file.Parent; ViewBag.AccessModes = new SelectList(new Dictionary <short, string> { { (short)AccessMode.Any, AccessMode.Any.ToLocalizedString() }, { (short)AccessMode.None, AccessMode.None.ToLocalizedString() }, { (short)AccessMode.OnlyAuthenticated, AccessMode.OnlyAuthenticated.ToLocalizedString() } }, "Key", "Value", model.AccessMode); if (!ModelState.IsValid) { ModelState.AddModelError("", ValidationResources.InvalidState); return(View(model)); } if (!string.IsNullOrWhiteSpace(model.NewName)) { if (Path.GetExtension(model.NewName) == "") { model.NewName += Path.GetExtension(model.OldName); } file.Name = model.NewName; } file.AccessModeShort = model.AccessMode; bool isSaved; try { isSaved = _unitOfWork.SaveChanges() > 0; } catch { isSaved = false; } if (isSaved) { Logger.SaveLog(new UpdateFileProvider(file)); } else { TempData["Error"] = ValidationResources.RenameFailure; } return(RedirectToAction("List", new { file.ParentId, page = 1 })); }
public ActionResult List(int?parentId = null, int page = 1, int recordPerPage = Constants.RecordPerPage) { IPagination <File> results; if (parentId == null) { if (page < 1) { return(RedirectToActionPermanent("List", new { page = 1 })); } IQueryable <File> query = _fileService.GetAllFiles(); ExcludeNotRealatedRecords(ref query); query = query.OrderBy(c => c.Id).AsQueryable(); results = new LazyPagination <File>(query, page, recordPerPage); if (!results.Any() && page != 1) { return(RedirectToActionPermanent("List", new { page = 1 })); } } else { File parent = _fileService.GetFileById(parentId.Value); if (parent == null) { return(NotFoundView()); } if (page < 1) { return(RedirectToActionPermanent("List", new { page = 1, parentId })); } IQueryable <File> query = _fileService.GetAllFilesByParentId(parent.Id); ExcludeNotRealatedRecords(ref query); query = query.OrderBy(c => c.Id).AsQueryable(); results = new LazyPagination <File>(query, page, recordPerPage); if (!results.Any() && page != 1) { return(RedirectToActionPermanent("List", new { page = 1, parentId })); } ViewBag.Parent = parent; } ViewBag.RecordPerPage = recordPerPage; return(ViewOrPartialView(results)); }
public ActionResult UploadFile(UploadDbFileModel model) { ViewBag.AccessModes = new SelectList(new Dictionary <short, string> { { (short)AccessMode.Any, AccessMode.Any.ToLocalizedString() }, { (short)AccessMode.None, AccessMode.None.ToLocalizedString() }, { (short)AccessMode.OnlyAuthenticated, AccessMode.OnlyAuthenticated.ToLocalizedString() } }, "Key", "Value", model.AccessMode); if (!ModelState.IsValid) { ModelState.AddModelError("", ValidationResources.InvalidState); return(View(model)); } if (model.ParentId.HasValue) { File parent = _fileService.GetFileById(model.ParentId.Value); if (parent == null) { return(NotFoundView()); } if (!UserIsAllowedToCrud(parent)) { return(AccessDeniedView()); } ViewBag.Parent = parent; } if (string.IsNullOrWhiteSpace(model.FileName)) { model.FileName = model.PostedFile.FileName; } const string extensions = ".7z|.aiff|.asf|.avi|.bmp|.csv|.doc|.docx|.fla|.flv|.gif|.gz|.gzip|.jpeg|.jpg|.mid|.mov|.mp3|.mp4|.mpc|.mpeg|.mpg|.ods|.odt|.pdf|.png|.ppt|.pxd|.qt|.ram|.rar|.rm|.rmi|.rmvb|.rtf|.sdc|.sitd|.swf|.sxc|.sxw|.tar|.tgz|.tif|.tiff|.txt|.vsd|.wav|.wma|.wmv|.xls|.xml|.zip"; if (Path.GetExtension(model.FileName) == "") { model.FileName += Path.GetExtension(model.PostedFile.FileName); } if (model.ContentLength == 0 || extensions.Split('|').All(e => string.Compare(e, Path.GetExtension(model.FileName), StringComparison.OrdinalIgnoreCase) != 0)) { ModelState.AddModelError("", ValidationResources.SelectedFileIsInvalid); return(View(model)); } var file = new File { Uploader = _webHelper.GetCurrentUser(HttpContext), AccessModeShort = model.AccessMode, CreateDate = DateTime.UtcNow, Name = model.FileName, ContentType = model.ContentType, Size = model.ContentLength, IsPublished = true, ParentId = model.ParentId }; _fileService.SaveFile(file); bool isSaved; try { isSaved = _unitOfWork.SaveChanges() > 0; } catch { isSaved = false; } if (isSaved) { Logger.SaveLog(new CreateFileProvider(file)); string targetPath = Server.MapPath(Constants.UploadsUrl); UploadUtilities.Save(model.PostedFile, targetPath, file.Guid.ToString()); return(RedirectToAction("List", new { file.ParentId, page = 1 })); } ModelState.AddModelError("", ValidationResources.UploadFileFailure); return(View(model)); }
public ActionResult CreateDirectory(CreateDbDirectoryModel model) { ViewBag.AccessModes = new SelectList(new Dictionary <short, string> { { (short)AccessMode.Any, AccessMode.Any.ToLocalizedString() }, { (short)AccessMode.None, AccessMode.None.ToLocalizedString() }, { (short)AccessMode.OnlyAuthenticated, AccessMode.OnlyAuthenticated.ToLocalizedString() } }, "Key", "Value", model.AccessMode); if (!ModelState.IsValid) { ModelState.AddModelError("", ValidationResources.InvalidState); return(View(model)); } if (model.ParentId.HasValue) { File parent = _fileService.GetFileById(model.ParentId.Value); if (parent == null) { return(NotFoundView()); } if (!UserIsAllowedToCrud(parent)) { return(AccessDeniedView()); } ViewBag.Parent = parent; } var file = new File { Uploader = _webHelper.GetCurrentUser(HttpContext), AccessModeShort = model.AccessMode, CreateDate = DateTime.UtcNow, Name = model.Name, IsPublished = true, ParentId = model.ParentId }; _fileService.SaveFile(file); bool isSaved; try { isSaved = _unitOfWork.SaveChanges() > 0; } catch { isSaved = false; } if (isSaved) { Logger.SaveLog(new CreateFileProvider(file)); } else { TempData["Error"] = ValidationResources.CreateDirectoryFailure; } return(RedirectToAction("List", new { file.ParentId, page = 1 })); }
//[ValidateAntiForgeryToken] public ActionResult Upload(HttpPostedFileBase upload, string CKEditorFuncNum, string CKEditor, string langCode) { string url; string message; string output; if (upload == null) { return(null); } const string extensions = ".7z|.aiff|.asf|.avi|.bmp|.csv|.doc|.docx|.fla|.flv|.gif|.gz|.gzip|.jpeg|.jpg|.mid|.mov|.mp3|.mp4|.mpc|.mpeg|.mpg|.ods|.odt|.pdf|.png|.ppt|.pxd|.qt|.ram|.rar|.rm|.rmi|.rmvb|.rtf|.sdc|.sitd|.swf|.sxc|.sxw|.tar|.tgz|.tif|.tiff|.txt|.vsd|.wav|.wma|.wmv|.xls|.xml|.zip"; if (upload.ContentLength == 0 || upload.ContentLength > 1000000 || extensions.Split('|').All(e => e != Path.GetExtension(upload.FileName)) || !UploadUtilities.IsValidImageBinary(upload.InputStream)) { message = ValidationResources.SelectedFileIsInvalid; output = BuildOutput(CKEditorFuncNum, null, message); return(Content(output)); } var file = new File { Uploader = _webHelper.GetCurrentUser(HttpContext), AccessMode = AccessMode.Any, CreateDate = DateTime.UtcNow, Name = upload.FileName, ContentType = upload.ContentType, Size = upload.ContentLength, IsPublished = true }; _fileService.SaveFile(file); bool isSaved; try { isSaved = _unitOfWork.SaveChanges() > 0; } catch { isSaved = false; } if (isSaved) { Logger.SaveLog(new CreateFileProvider(file)); string targetPath = Server.MapPath(Constants.UploadsUrl); UploadUtilities.Save(upload, targetPath, file.Guid.ToString()); url = Url.RouteUrl("Download", new { file.Guid, fn = file.Name }); message = ValidationResources.UploadFileSuccess; output = BuildOutput(CKEditorFuncNum, url, message); return(Content(output)); } message = ValidationResources.UploadFileFailure; output = BuildOutput(CKEditorFuncNum, null, message); return(Content(output)); }
public ActionResult Download(Guid guid, string fn = null) { File file = _fileService.GetFileByGuid(guid); if (file == null || file.ContentType == null) { return(NotFoundView()); } if (!string.IsNullOrEmpty(fn) && string.Compare(file.Name, fn) != 0) { return(NotFoundView()); } File parent = file; while (parent != null) { if (!parent.IsPublished) { return(AccessDeniedView()); } switch (parent.AccessMode) { case AccessMode.OnlyAuthenticated: if (!User.Identity.IsAuthenticated) { return(AccessDeniedView()); } break; case AccessMode.None: if (!User.Identity.IsAuthenticated) { return(AccessDeniedView()); } User user = _webHelper.GetCurrentUser(HttpContext); if (file.UploaderId != user.Id) { return(AccessDeniedView()); } break; } parent = parent.Parent; } string filePath = Server.MapPath(Path.Combine(Constants.UploadsUrl, file.Guid.ToString())); if (!System.IO.File.Exists(filePath)) { return(NotFoundView()); } file.AccessCount++; bool isSaved = true; try { _unitOfWork.SaveChanges(); } catch { isSaved = false; } if (isSaved) { Logger.SaveLog(new FileDownloadProvider(file)); } else { TempData["Error"] = ValidationResources.DownloadFailure; } using (FileStream stream = System.IO.File.OpenRead(filePath)) { var buffer = new byte[stream.Length]; stream.Read(buffer, 0, (int)stream.Length); return(File(buffer, file.ContentType, file.Name)); } }