yavsc/Yavsc/ApiControllers/PdfEstimateController.cs

84 lines
2.7 KiB
C#

8 years ago
using System.IO;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Mvc;
8 years ago
using System.Web.Routing;
8 years ago
namespace Yavsc.ApiControllers
{
8 years ago
using Models;
using Helpers;
using System.Linq;
using Microsoft.Data.Entity;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
8 years ago
8 years ago
[Route("api/pdfestimate"), Authorize]
public class PdfEstimateController : Controller
{
8 years ago
ApplicationDbContext dbContext;
private IAuthorizationService authorizationService;
private ILogger logger;
8 years ago
public PdfEstimateController(
IAuthorizationService authorizationService,
ILoggerFactory loggerFactory,
8 years ago
ApplicationDbContext context)
{
this.authorizationService = authorizationService;
8 years ago
dbContext = context;
logger = loggerFactory.CreateLogger<PdfEstimateController>();
8 years ago
}
[HttpGet("get/{id}", Name = "Get"), Authorize]
public async Task<IActionResult> Get(long id)
8 years ago
{
var estimate = dbContext.Estimates.Include(
e=>e.Query
).FirstOrDefault(e=>e.Id == id);
logger.LogWarning($"#######ESTIMATE OWNER ID {estimate.OwnerId} ########");
if (!await authorizationService.AuthorizeAsync(User, estimate, new ViewRequirement()))
{
return new ChallengeResult();
}
8 years ago
var filename = $"estimate-{id}.pdf";
8 years ago
8 years ago
var cd = new System.Net.Mime.ContentDisposition
{
// for example foo.bak
FileName = filename,
// always prompt the user for downloading, set to true if you want
// the browser to try to show the file inline
Inline = false,
};
8 years ago
FileInfo fi = new FileInfo(Path.Combine(Startup.UserBillsDirName, filename));
if (!fi.Exists) return Ok(new { Error = "Not generated" });
return File(fi.OpenRead(), "application/x-pdf", filename); ;
}
[HttpGet("estimate-{id}.tex", Name = "GetTex"), Authorize]
public IActionResult GetTex(long id)
{
Response.ContentType = "text/x-tex";
return ViewComponent("Estimate",new object[] { id, "LaTeX" });
8 years ago
}
[HttpPost("gen/{id}")]
8 years ago
public IActionResult GeneratePdf(long id)
8 years ago
{
return ViewComponent("Estimate",new object[] { id, "Pdf" } );
8 years ago
}
[HttpPost("prosign/{id}")]
public IActionResult ProSign(long id)
{
if (Request.Form.Files.Count!=1)
return new BadRequestResult();
return Ok (User.ReceiveProSignature(id,Request.Form.Files[0]));
}
8 years ago
}
}