public async Task <IActionResult> DeleteOneTimeAccessLink(string id) { try { Guid.TryParse(User.FindFirst(ClaimTypes.NameIdentifier).Value, out Guid userId); Guid linkForRepoId = new Guid(id); OneTimeAccessLink link = await _repo.GetOneTimeAccessLink(linkForRepoId); if (userId != link.UserId) { return(Unauthorized()); } _repo.Delete(link); if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("There was a problem deleting file access link")); } catch (Exception ex) { return(StatusCode(400, ex.Message)); } }
public async Task <IActionResult> ConsumeOneTimeAccessLink(string id) { try { Guid linkForRepoId = new Guid(id); OneTimeAccessLink link = await _repo.GetOneTimeAccessLink(linkForRepoId); Models.File fileFromRepo = await _repo.GetFile(link.FileId); Folder folderFromRepo = await _repo.GetFolder(fileFromRepo.FolderId); if (link.IsUsed) { return(BadRequest("The link is expired.")); } link.IsUsed = true; link.UsedAt = DateTime.Now; string folderName; if (folderFromRepo.FolderName == folderFromRepo.UserId.ToString()) { folderName = "App_Data/" + folderFromRepo.UserId.ToString() + "/" + fileFromRepo.Id; } else { folderName = "App_Data/" + folderFromRepo.UserId.ToString() + "/" + folderFromRepo.Id + "/" + fileFromRepo.Id; } string webRootPath = _hostingEnv.WebRootPath; string newPath = Path.Combine(webRootPath, folderName); if (await _repo.SaveAll()) { string fileName = fileFromRepo.FileName + "." + fileFromRepo.FileExtension; byte[] fileBytes = System.IO.File.ReadAllBytes(newPath); return(new JsonResult(new { File = File(fileBytes, "application/force-download", fileName), Name = fileName })); } return(BadRequest("There was a problem accessing file information")); } catch (Exception ex) { return(StatusCode(400, ex.Message)); } }
public async Task <IActionResult> CreateOneTimeAccessLink(string folderId, string fileId) { try { Guid.TryParse(User.FindFirst(ClaimTypes.NameIdentifier).Value, out Guid userId); Guid folderForRepoId = new Guid(folderId); Guid fileForRepoId = new Guid(fileId); Folder folder = await _repo.GetFolder(folderForRepoId); Models.File file = await _repo.GetFile(fileForRepoId); if (userId != folder.UserId) { return(Unauthorized()); } OneTimeAccessLink otal = new OneTimeAccessLink(); otal.Id = Guid.NewGuid(); otal.UserId = userId; otal.IsUsed = false; otal.FileName = file.FileName; otal.FolderName = folder.FolderName; otal.FileId = file.Id; otal.UsedAt = null; _repo.Add(otal); if (await _repo.SaveAll()) { return(StatusCode(200, "Access Link Created.")); } return(BadRequest("There was an error creating the access link")); } catch (Exception ex) { return(StatusCode(400, ex.Message)); } }
public async Task <OneTimeAccessLink> GetOneTimeAccessLink(Guid id) { OneTimeAccessLink link = await _context.OneTimeAccessLinks.FirstOrDefaultAsync(otal => otal.Id == id); return(link); }