public async Task <ActionResult <FileItem> > AddShareFile([FromBody] AddFileShareBody body) { InternalFile file; try { file = await ValidateAddFileShare(body); } catch (HttpResultException exc) { return(exc.Result); } if (await dbContext.ShareFiles.AnyAsync(f => f.Name == body.Name && f.UserId == body.UserId)) { return(BadRequest("File with this name is already shared")); } ShareFile shareFile = new ShareFile() { Name = body.Name, Path = file.PhysicalPath, IsListed = body.IsListed, UserId = string.IsNullOrWhiteSpace(body.UserId) ? null : body.UserId, Permission = Models.FileItemPermission.New(body.Permission), }; await dbContext.ShareFiles.AddAsync(shareFile); await dbContext.SaveChangesAsync(); return(shareFile.ToFileItem()); }
private async Task <InternalFile> ValidateAddFileShare(AddFileShareBody body) { await ValidateFileSystemItemShare(body); string userId = HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); InternalFile file = await ShareFileHelper.GetFileItem(body.Path, dbContext, userId, this); if (!HasPermission(file.Permission, body.Permission)) { throw (HttpResultException)Forbid(); } if (!System.IO.File.Exists(file.PhysicalPath)) { throw (HttpResultException)NotFound("File not found"); } return(file); }