public static async Task <bool> Validate(this FolderUpload folderUpload, AppDbContext db) { if (folderUpload.FolderId < 1) { throw new Exception("An upload must be associated with an folder"); } if (folderUpload.UploadId < 1) { throw new Exception("An folder must be associated with an upload"); } var check = await db.FolderUploads .FirstOrDefaultAsync(x => x.Id != folderUpload.Id && x.FolderId == folderUpload.FolderId && x.UploadId == folderUpload.UploadId ); if (check != null) { throw new Exception("This folder already contains the specified upload"); } return(true); }
/// <summary> /// Creates a directory if the following conditions are met: /// <list type="bullet"> /// <item> /// <description>The full path does not exists</description> /// </item> /// <item> /// <description>The max allowed sub folders for the full path is not reached</description> /// </item> /// </list> /// </summary> /// <param name="folder">The form the user send</param> /// <param name="requestingUserUuid">The uuid of the requesting user</param> public async Task CreateFolder(FolderUpload folder, Guid requestingUserUuid) { if (!Directory.Exists($"{Environment.CurrentDirectory}/Media{folder.ParentPath}") || !DirectoryHelper.CanCreateFolderInDirectory(folder.ParentPath)) { throw new UnprocessableException(); } string fullPath = $"{Environment.CurrentDirectory}/Media{folder.ParentPath}{folder.Name}"; if (Directory.Exists(fullPath)) { throw new DuplicateNameException(); } FilePath filepathInfo = FilePathInfo.Find(folder.ParentPath); string rootPath = $"{Environment.CurrentDirectory}/Media{filepathInfo?.Path}"; var rootDirectoryInfoFile = await DirectoryHelper.GetInfoFileFromDirectory(rootPath); DirectoryContentInfo directoryContentInfo = rootDirectoryInfoFile.DirectoryContentInfo .Find(dci => dci.OwnerUuid == requestingUserUuid); if (directoryContentInfo == null || directoryContentInfo == new DirectoryContentInfo()) { rootDirectoryInfoFile.DirectoryContentInfo.Add(new DirectoryContentInfo { DirectoriesOwnedByUser = new List <string> { folder.Name }, OwnerUuid = requestingUserUuid }); } else { directoryContentInfo.DirectoriesOwnedByUser.Add(folder.Name); } Directory.CreateDirectory(fullPath); var directoryInfoFile = new DirectoryInfoFile { DirectoryOwnerUuid = requestingUserUuid }; await DirectoryHelper.UpdateInfoFile(rootPath, rootDirectoryInfoFile); await DirectoryHelper.UpdateInfoFile(fullPath, directoryInfoFile); }
public static string SaveFile(HttpPostedFileBase fileImport, FolderUpload folder) { var filename = Guid.NewGuid() + Path.GetExtension(fileImport.FileName);; var uploadPath = Path.Combine(HttpContext.Current.Server.MapPath("/App_Data/Uploads/" + folder), filename); try { fileImport.SaveAs(uploadPath); } catch { Directory.CreateDirectory(HttpContext.Current.Server.MapPath("/App_Data/Uploads/" + folder)); fileImport.SaveAs(uploadPath); } return(uploadPath); }
public async Task <ActionResult> CreateFolder([FromForm] FolderUpload folder) { try { UserHelper requestingUser = _controllerHelper.GetRequestingUser(this); await _directoryLogic.CreateFolder(folder, requestingUser.Uuid); return(Ok()); } catch (DuplicateNameException) { return(Conflict()); } catch (UnprocessableException) { return(StatusCode(StatusCodes.Status422UnprocessableEntity)); } catch (Exception e) { _logLogic.Log(e); return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public static string GetFilePath(string fileName, FolderUpload folder) { return(Path.Combine(HttpContext.Current.Server.MapPath("/App_Data/Uploads/" + folder), fileName)); }