public IActionResult ChangeFileInfo(FileInfoModel fileInfo) { if (ModelState.IsValid) { HostingFile file = _hostingCore.GetFileById(fileInfo.Id); User user = _hostingCore.GetUserById(file.AuthorId); if (User.Identity.Name == user.Email || User.HasClaim(ClaimsIdentity.DefaultRoleClaimType, Roles.Admin.ToString()) || User.HasClaim(ClaimsIdentity.DefaultRoleClaimType, Roles.Editor.ToString())) { IEnumerable <HostingFile> userFiles = _hostingCore.GetUserFiles(file.AuthorId); var selectedFiles = from userFile in userFiles where userFile.Name == fileInfo.Name && userFile.Id != fileInfo.Id select userFile; if (selectedFiles?.Count() > 0) { ModelState.AddModelError("", "You already have a file with this name"); return(View(fileInfo)); } file.Name = fileInfo.Name; file.Category = fileInfo.Category; file.Description = fileInfo.Description; _hostingCore.UpdateFile(file.Id, file); } return(RedirectToAction("FilePage", new { fileId = file.Id })); } return(View(fileInfo)); }
public async Task <IActionResult> Upload(FileLoadModel loadModel) { if (ModelState.IsValid) { IFormFile uploadedFile = loadModel.UploadedFile; User user = _hostingCore.GetUserByEmail(User.Identity.Name); if (user != null && uploadedFile != null) { if (uploadedFile.FileName.Length > 100) { ModelState.AddModelError("", "Max file name length is 100 symbols"); return(View(loadModel)); } IEnumerable <HostingFile> userFiles = _hostingCore.GetUserFiles(user.Id); var selectedFiles = from userFile in userFiles where userFile.Name == uploadedFile.FileName select userFile; if (selectedFiles?.Count() > 0) { ModelState.AddModelError("", "You already uploaded a file with this name"); return(View(loadModel)); } var link = $"Files/{user.Id}/"; var file = new HostingFile { AuthorId = user.Id, Name = uploadedFile.FileName, Size = uploadedFile.Length, Category = loadModel.Category, Description = loadModel.Description, Link = link }; _hostingCore.InsertFile(file); userFiles = _hostingCore.GetUserFiles(user.Id); selectedFiles = from userFile in userFiles where userFile.Name == uploadedFile.FileName select userFile; file = selectedFiles.FirstOrDefault(); file.Link += file.Id; _hostingCore.UpdateFile(file.Id, file); var path = _appEnvironment.WebRootPath + $"/Files/{user.Id}/"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } path += file.Id; using (var fileStream = new FileStream(path, FileMode.Create)) { await uploadedFile.CopyToAsync(fileStream); } return(RedirectToAction("UserPage", "Hosting", new { userId = file.AuthorId })); } return(View(loadModel)); } ModelState.AddModelError("", "An error occurred during uploading"); return(View(loadModel)); }