200 lines
6.8 KiB
C#
200 lines
6.8 KiB
C#
using DigitalData.Modules.Logging;
|
|
using EnvelopeGenerator.Common;
|
|
using EnvelopeGenerator.Common.My.Resources;
|
|
using EnvelopeGenerator.Web.Services;
|
|
using Microsoft.Extensions.Primitives;
|
|
using System.IO.Pipelines;
|
|
using System.Reflection.Metadata.Ecma335;
|
|
|
|
namespace EnvelopeGenerator.Web.Handler
|
|
{
|
|
public class FileHandler
|
|
{
|
|
public async static Task<IResult> HandleFileDownload(HttpContext ctx, DatabaseService database, LoggingService logging)
|
|
{
|
|
var logger = logging.LogConfig.GetLogger("FileHandler");
|
|
|
|
try
|
|
{
|
|
logger.Info("Handling file download.");
|
|
|
|
// Load Envelope from EnvelopeKey
|
|
string envelopeKey = EnsureValidEnvelopeKey(logger, ctx.Request);
|
|
EnvelopeResponse r = database.LoadEnvelope(envelopeKey);
|
|
|
|
// Get the document Index
|
|
int documentId = EnsureValidDocumentIndex(logger, ctx.Request);
|
|
var document = GetDocument(r.Envelope, documentId);
|
|
|
|
// Load the document from disk
|
|
var bytes = await File.ReadAllBytesAsync(document.Filepath);
|
|
logger.Info("Serving file, size: [{0}]", bytes.Length);
|
|
|
|
// Return the document as bytes
|
|
return Results.File(bytes);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// Better error handling & reporting
|
|
logger.Error(e);
|
|
return Results.Problem();
|
|
|
|
}
|
|
}
|
|
|
|
public async static Task<IResult> HandleFileUpload(HttpContext ctx, DatabaseService database, LoggingService logging)
|
|
{
|
|
var logger = logging.LogConfig.GetLogger("FileHandler");
|
|
|
|
try
|
|
{
|
|
logger.Info("Handling file upload.");
|
|
|
|
// Load Envelope from EnvelopeKey
|
|
string envelopeKey = EnsureValidEnvelopeKey(logger, ctx.Request);
|
|
EnvelopeResponse r = database.LoadEnvelope(envelopeKey);
|
|
|
|
// Get the document Index
|
|
int documentId = EnsureValidDocumentIndex(logger, ctx.Request);
|
|
var document = GetDocument(r.Envelope, documentId);
|
|
|
|
using FileStream fs = new(document.Filepath, FileMode.Open);
|
|
await ctx.Request.Body.CopyToAsync(fs);
|
|
fs.Flush();
|
|
|
|
return Results.Ok();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// Better error handling & reporting
|
|
logger.Error(e);
|
|
return Results.Problem();
|
|
}
|
|
}
|
|
|
|
public async static Task<IResult> HandleGetData(HttpContext ctx, DatabaseService database, LoggingService logging)
|
|
{
|
|
var logger = logging.LogConfig.GetLogger("FileHandler");
|
|
|
|
try
|
|
{
|
|
logger.Info("Handling file download.");
|
|
|
|
// Load Envelope from EnvelopeKey
|
|
string envelopeKey = EnsureValidEnvelopeKey(logger, ctx.Request);
|
|
EnvelopeResponse r = database.LoadEnvelope(envelopeKey);
|
|
|
|
// Get the document Index
|
|
int documentId = EnsureValidDocumentIndex(logger, ctx.Request);
|
|
var document = GetDocument(r.Envelope, documentId);
|
|
|
|
// Load the document from disk
|
|
var bytes = await File.ReadAllBytesAsync(document.Filepath);
|
|
logger.Info("Serving file, size: [{0}]", bytes.Length);
|
|
|
|
// Return the envelope and additional data as json
|
|
return Results.Json(r);
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// Better error handling & reporting
|
|
logger.Error(e);
|
|
return Results.Problem();
|
|
|
|
}
|
|
}
|
|
|
|
public async static Task<IResult> HandlePostData(HttpContext ctx, DatabaseService database, LoggingService logging)
|
|
{
|
|
var logger = logging.LogConfig.GetLogger("FileHandler");
|
|
|
|
try
|
|
{
|
|
logger.Info("Handling file download.");
|
|
|
|
// Load Envelope from EnvelopeKey
|
|
string envelopeKey = EnsureValidEnvelopeKey(logger, ctx.Request);
|
|
EnvelopeResponse r = database.LoadEnvelope(envelopeKey);
|
|
|
|
// Get the document Index
|
|
int documentId = EnsureValidDocumentIndex(logger, ctx.Request);
|
|
var document = GetDocument(r.Envelope, documentId);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// Better error handling & reporting
|
|
logger.Error(e);
|
|
return Results.Problem();
|
|
}
|
|
|
|
var envelopeKey = ctx.Request.RouteValues["envelopeKey"] as string;
|
|
var documentIdString = ctx.Request.RouteValues["documentId"] as string;
|
|
|
|
if (int.TryParse(documentIdString, out int documentId) == false)
|
|
{
|
|
return Results.Problem();
|
|
}
|
|
|
|
database.LoadDocument(documentId);
|
|
|
|
return Results.Ok();
|
|
}
|
|
|
|
private static int EnsureValidDocumentIndex(Logger logger, HttpRequest request)
|
|
{
|
|
if (request.Query.TryGetValue("index", out StringValues documentIndexString))
|
|
{
|
|
try
|
|
{
|
|
return int.Parse(documentIndexString.First());
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw new ArgumentNullException("DocumentIndex", e);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new ArgumentNullException("DocumentIndex");
|
|
}
|
|
}
|
|
|
|
private static string EnsureValidEnvelopeKey(Logger logger, HttpRequest request)
|
|
{
|
|
logger.Debug("Parsing EnvelopeKey..");
|
|
var envelopeKey = request.RouteValues["envelopeKey"] as string;
|
|
|
|
if (string.IsNullOrEmpty(envelopeKey))
|
|
throw new ArgumentNullException("EnvelopeKey");
|
|
|
|
Tuple<string, string> result = Helpers.DecodeEnvelopeReceiverId(envelopeKey);
|
|
logger.Debug("EnvelopeUUID: [{0}]", result.Item1);
|
|
logger.Debug("ReceiverSignature: [{0}]", result.Item2);
|
|
|
|
if (string.IsNullOrEmpty(result.Item1))
|
|
throw new ArgumentNullException("EnvelopeUUID");
|
|
|
|
if (string.IsNullOrEmpty(result.Item2))
|
|
throw new ArgumentNullException("ReceiverSignature");
|
|
|
|
return envelopeKey;
|
|
}
|
|
|
|
private static EnvelopeDocument GetDocument(Common.Envelope envelope, int documentId)
|
|
{
|
|
var document = envelope.Documents.
|
|
Where(d => d.Id == documentId).
|
|
FirstOrDefault();
|
|
|
|
if (document == null)
|
|
throw new ArgumentException("DocumentId");
|
|
|
|
return document;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|