public async Task <IActionResult> DownloadXmlFile(long id, string token) { DownloadUploadToken dut = await _context.DownloadUploadTokens .Where(d => d.IsUsed == false) .Where(d => d.TableName == TableName.LeaveApplication) .Where(d => d.ExpirityTime >= DateTime.UtcNow) .Where(d => d.DbEntryId == id) .Where(d => d.Token.Equals(token)) .FirstOrDefaultAsync(); if (dut == null) { return(BadRequest("Token Not Found")); } XmlFile xmlFile = await _context.XmlFiles.FindAsync(dut.DbEntryId); if (xmlFile == null) { return(NoContent()); } byte[] byteArray = Encoding.UTF8.GetBytes(xmlFile.FileContent); MemoryStream fileStream = new MemoryStream(byteArray); return(File(fileStream, "application/ocet-stream", xmlFile.FileRealName)); }
public async Task <ActionResult <long> > UploadXmlFile([FromForm] IFormFile xmlFile, [FromForm] long?previousFileId, [FromForm] string token) //XmlFile xmlFile { XmlFile uploadedFile = new XmlFile(); DownloadUploadToken dut = await _context.DownloadUploadTokens .Where(d => d.IsUsed == false) .Where(d => d.TableName == TableName.LeaveApplication) .Where(d => d.ExpirityTime >= DateTime.UtcNow) .Where(d => d.Token.Equals(token)) //.Where(d => d.Signer == user) .FirstOrDefaultAsync(); if (dut == null) { return(BadRequest("Token Not Found")); } dut.MarkAsUsed(); _context.DownloadUploadTokens.Update(dut); uploadedFile.PreviousSignedFile = await _context.XmlFiles.FindAsync(previousFileId); if (uploadedFile.PreviousSignedFile == null) { return(BadRequest("Valid Token and previous file ID don't match")); } if (xmlFile.Length > 0) { uploadedFile.Signer = await _userManager.Users.FirstOrDefaultAsync(u => u.Id == dut.SignerId); uploadedFile.FileContent = await Adapter.ReadAsStringAsync(xmlFile); uploadedFile.FileRealName = xmlFile.FileName; uploadedFile.DbEntryId = uploadedFile.PreviousSignedFile.DbEntryId; } else { return(BadRequest("A file Should be Uploaded")); } _context.XmlFiles.Add(uploadedFile); LeaveApplication leaveApp = await _context.LeaveApplications.FindAsync(uploadedFile.DbEntryId); if (leaveApp == null) { return(BadRequest("A file Should be Uploaded")); } await _context.SaveChangesAsync(); leaveApp.LastSignedId = uploadedFile.Id; leaveApp.PreviousSignedFile = uploadedFile; _context.LeaveApplications.Update(leaveApp); await _context.SaveChangesAsync(); return(uploadedFile.Id); }
public async Task <ActionResult <string> > GetXmlFileDownloadToken(long id) { //Should add token verification XmlFile xmlFile = await _context.XmlFiles.FindAsync(id); if (xmlFile == null) { return(BadRequest("File Not Found")); } DownloadUploadToken dut = new DownloadUploadToken("Reason", TableName.LeaveApplication, id); dut.Signer = await _userManager.GetUserAsync(User); /*if (xmlFile.DownloadUploadTokens == null) * { * xmlFile.DownloadUploadTokens = new List<DownloadUploadToken>(); * } * xmlFile.DownloadUploadTokens.Add(dut);*/ _context.DownloadUploadTokens.Add(dut); _context.XmlFiles.Update(xmlFile); await _context.SaveChangesAsync(); return(dut.Token); }