122 lines
4.0 KiB
C#
122 lines
4.0 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using DigitalData.Modules.Logging;
|
|
using EnvelopeGenerator.Common;
|
|
using EnvelopeGenerator.Web.Services;
|
|
using static EnvelopeGenerator.Common.Constants;
|
|
using static EnvelopeGenerator.Web.Constants;
|
|
|
|
namespace EnvelopeGenerator.Web.Controllers
|
|
{
|
|
public class DocumentController : Controller
|
|
{
|
|
private readonly DatabaseService database;
|
|
private readonly LoggingService logging;
|
|
private readonly Logger logger;
|
|
private readonly ApiService api;
|
|
|
|
public DocumentController(DatabaseService database, LoggingService logging, ApiService api)
|
|
{
|
|
this.database = database;
|
|
this.logging = logging;
|
|
this.logger = logging.LogConfig.GetLoggerFor(GetType().Name);
|
|
this.api = api;
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("api/document/{envelopeKey}")]
|
|
public async Task<IActionResult> Get(string envelopeKey)
|
|
{
|
|
try
|
|
{
|
|
logger.Info("Handling file download.");
|
|
|
|
// Validate Envelope Key
|
|
EnvelopeResponse r = api.EnsureValidEnvelopeKey(envelopeKey);
|
|
|
|
// Load document info
|
|
var Request = ControllerContext.HttpContext.Request;
|
|
var document = api.GetDocument(Request, envelopeKey);
|
|
|
|
// Load the document from disk
|
|
var bytes = await System.IO.File.ReadAllBytesAsync(document.Filepath);
|
|
logger.Info("Serving file, size: [{0}]", bytes.Length);
|
|
|
|
// Return the document as bytes
|
|
return File(bytes, "application/octet-stream");
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
logger.Error(e);
|
|
return Problem(
|
|
statusCode: 500,
|
|
detail: e.Message,
|
|
type: ErrorType.ServerError.ToString());
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// Better error handling & reporting
|
|
logger.Error(e);
|
|
return Problem(
|
|
statusCode: 500,
|
|
detail: e.Message,
|
|
type: ErrorType.ServerError.ToString());
|
|
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("api/document/{envelopeKey}")]
|
|
public async Task<IActionResult> Update(string envelopeKey)
|
|
{
|
|
try
|
|
{
|
|
logger.Info("Handling file update.");
|
|
|
|
// Validate Envelope Key
|
|
EnvelopeResponse r = api.EnsureValidEnvelopeKey(envelopeKey);
|
|
|
|
// Load Document info
|
|
var Request = ControllerContext.HttpContext.Request;
|
|
var document = api.GetDocument(Request, envelopeKey);
|
|
|
|
// Try to update the document with new data
|
|
if (!await api.UpdateDocument(Request.Body, document.Filepath))
|
|
{
|
|
throw new IOException("Document could not be saved to disk!");
|
|
}
|
|
|
|
// Add history entry
|
|
database.InsertHistoryEntry(new EnvelopeHistoryEntry()
|
|
{
|
|
ActionDescription = "Dokument wurde signiert",
|
|
ActionDate = DateTime.Now,
|
|
ActionType = EnvelopeHistoryActionType.Signed,
|
|
EnvelopeId = r.Envelope.Id,
|
|
UserReference = r.Receiver.Email
|
|
});
|
|
|
|
return Ok();
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
logger.Error(e);
|
|
return Problem(
|
|
statusCode: 500,
|
|
detail: e.Message,
|
|
type: ErrorType.ServerError.ToString());
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// Better error handling & reporting
|
|
logger.Error(e);
|
|
return Problem(
|
|
statusCode: 500,
|
|
detail: e.Message,
|
|
type: ErrorType.ServerError.ToString());
|
|
}
|
|
}
|
|
}
|
|
}
|