147 lines
4.5 KiB
C#
147 lines
4.5 KiB
C#
// /**
|
|
// * File: Daemon.cs
|
|
// * Author: haraldwolff
|
|
// *
|
|
// * This file and it's content is copyrighted by the Author and / or copyright holder.
|
|
// * Any use wihtout proper permission is illegal and may lead to legal actions.
|
|
// *
|
|
// *
|
|
// **/
|
|
using System;
|
|
using ln.http;
|
|
using ln.http.router;
|
|
using System.IO;
|
|
using ln.types.net;
|
|
using ln.http.message;
|
|
using ln.types;
|
|
using iTextSharp.text.pdf;
|
|
using System.Collections.Generic;
|
|
using iTextSharp.text;
|
|
namespace ln.pdfstamp
|
|
{
|
|
public class Daemon
|
|
{
|
|
public string SpoolPath { get; }
|
|
|
|
HTTPServer server;
|
|
SimpleRouter router;
|
|
|
|
public Daemon()
|
|
{
|
|
int prio = 1;
|
|
router = new SimpleRouter();
|
|
|
|
foreach (String p in new string[] { "www", "../../www" })
|
|
{
|
|
if (Directory.Exists(p))
|
|
{
|
|
StaticRouter staticRouter = new StaticRouter(p);
|
|
router.AddSimpleRoute("/*", staticRouter, prio++);
|
|
}
|
|
}
|
|
|
|
if (Directory.Exists("/var/spool/ln.pdfstamp"))
|
|
{
|
|
SpoolPath = "/var/spool/ln.pdfstamp";
|
|
}
|
|
else
|
|
{
|
|
SpoolPath = "./spool";
|
|
}
|
|
|
|
EnsureDirectory(SpoolPath);
|
|
EnsureDirectory(Path.Combine(SpoolPath, "stamps"));
|
|
EnsureDirectory(Path.Combine(SpoolPath, "stamped"));
|
|
|
|
StaticRouter spoolRouter = new StaticRouter(SpoolPath);
|
|
router.AddSimpleRoute("/spool/*", spoolRouter);
|
|
router.AddSimpleRoute("/api/stamp/:stamp", new RouterTarget(PostPDF));
|
|
}
|
|
|
|
public void EnsureDirectory(string path)
|
|
{
|
|
if (!Directory.Exists(path))
|
|
Directory.CreateDirectory(path);
|
|
}
|
|
|
|
public void Start()
|
|
{
|
|
server = new HTTPServer(new Endpoint(IPv6.ANY, 8888),
|
|
new LoggingRouter(router)
|
|
);
|
|
server.Start();
|
|
}
|
|
|
|
HttpResponse PostPDF(HttpRequest request)
|
|
{
|
|
MemoryStream pdfOutput = new MemoryStream();
|
|
Stream pdfInput = null;
|
|
|
|
if (request.Method.Equals("POST"))
|
|
{
|
|
switch (request.RequestHeaders["Content-Type"].Value)
|
|
{
|
|
case "application/pdf":
|
|
pdfInput = request.ContentStream;
|
|
break;
|
|
case "multipart/form-data":
|
|
pdfInput = FindMultipartPDF(request);
|
|
break;
|
|
default:
|
|
throw new NotSupportedException();
|
|
}
|
|
} else if (request.Method.Equals("PUT"))
|
|
{
|
|
pdfInput = request.ContentStream;
|
|
}
|
|
|
|
Stamp(
|
|
pdfInput,
|
|
request.GetParameter("stamp"),
|
|
float.Parse(request.Query.GetValue("left","20")),
|
|
float.Parse(request.Query.GetValue("top", "10")),
|
|
float.Parse(request.Query.GetValue("width", "70")),
|
|
pdfOutput
|
|
);
|
|
|
|
string spoolFileName = String.Format("{0}-stamped.pdf",DateTime.Now.ToString("yyyyMMdd-HHmmss"));
|
|
|
|
pdfOutput = new MemoryStream(pdfOutput.ToArray());
|
|
|
|
using (FileStream pdfOutputStream = new FileStream(Path.Combine(SpoolPath, "stamped", spoolFileName), FileMode.CreateNew))
|
|
{
|
|
pdfOutput.CopyTo(pdfOutputStream);
|
|
}
|
|
|
|
HttpResponse response = new HttpResponse(request);
|
|
response.StatusCode = 302;
|
|
response.AddHeader("Location", String.Format("/spool/stamped/{0}",spoolFileName));
|
|
return response;
|
|
|
|
}
|
|
|
|
public Stream FindMultipartPDF(HttpRequest request)
|
|
{
|
|
Message message = new Message(request.RequestHeaders, request.ContentStream.ReadToEnd());
|
|
|
|
foreach (Message part in message.Parts)
|
|
{
|
|
if (part.Headers["CONTENT-TYPE"].Value.Equals("application/pdf"))
|
|
return part.OpenBodyStream();
|
|
}
|
|
|
|
throw new KeyNotFoundException("Could not find valid PDF file to stamp");
|
|
}
|
|
|
|
public void Stamp(Stream pdfInput,string stampImageFileName,float left,float top,float width,Stream pdfOutput)
|
|
{
|
|
Image stampImage = Image.GetInstance(Path.GetFullPath(Path.Combine(SpoolPath, "stamps", stampImageFileName)));
|
|
PDFStamp.Stamp(pdfInput, stampImage, left, top, width, pdfOutput);
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|