Merge from booking branch
This commit is contained in:
parent
25906d0227
commit
9a2652739b
266 changed files with 12833 additions and 2337 deletions
95
booking/Formatters/ErrorHtmlFormatter.cs
Normal file
95
booking/Formatters/ErrorHtmlFormatter.cs
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
//
|
||||
// ErrorHtmlFormatter.cs
|
||||
//
|
||||
// Author:
|
||||
// paul <${AuthorEmail}>
|
||||
//
|
||||
// Copyright (c) 2015 paul
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using System;
|
||||
using System.Net.Http.Formatting;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Web.Mvc;
|
||||
using System.Net;
|
||||
using MarkdownDeep;
|
||||
using Yavsc.Helpers;
|
||||
using Yavsc.Model.Blogs;
|
||||
|
||||
namespace Yavsc.Formatters
|
||||
{
|
||||
/// <summary>
|
||||
/// Formats a given error message to respond
|
||||
/// in case of error, and in an html format
|
||||
/// </summary>
|
||||
public class ErrorHtmlFormatter:SimpleFormatter {
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the title.
|
||||
/// </summary>
|
||||
/// <value>The title.</value>
|
||||
public string Title { get ; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the error code.
|
||||
/// </summary>
|
||||
/// <value>The error code.</value>
|
||||
public HttpStatusCode ErrorCode { get ; set; }
|
||||
|
||||
string doctype="<!DOCTYPE html>";
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Yavsc.Formatters.ErrorHtmlFormatter"/> class.
|
||||
/// </summary>
|
||||
/// <param name="errorCode">Error code.</param>
|
||||
/// <param name="title">Title.</param>
|
||||
public ErrorHtmlFormatter
|
||||
(HttpStatusCode errorCode, string title):base("text/html")
|
||||
{
|
||||
ErrorCode = errorCode;
|
||||
Title = title;
|
||||
|
||||
}
|
||||
|
||||
public override void WriteToStream (Type type, object value, Stream stream, HttpContent content)
|
||||
{
|
||||
// TODO create a type containing T4 parameters, and generate from them
|
||||
using (var writer = new StreamWriter(stream))
|
||||
{
|
||||
string message = value as string;
|
||||
TagBuilder doc = new TagBuilder ("html");
|
||||
TagBuilder body = new TagBuilder ("body");
|
||||
TagBuilder h1 = new TagBuilder ("h1");
|
||||
TagBuilder p = new TagBuilder ("p");
|
||||
TagBuilder head = new TagBuilder ("head");
|
||||
head.InnerHtml = "<meta http-equiv=\"Content-Type\" " +
|
||||
"content=\"text/html; charset=utf-8\"/>" +
|
||||
"<link rel=\"stylesheet\" " +
|
||||
"href=\"/Theme/style.css\" />" +
|
||||
"<link rel=\"icon\" type=\"image/png\"" +
|
||||
" href=\"/favicon.png\" />";
|
||||
p.InnerHtml = MarkdownHelper.Markdown(message).ToHtmlString();
|
||||
h1.InnerHtml = MvcHtmlString.Create (Title).ToHtmlString();
|
||||
body.InnerHtml = h1.ToString()+p.ToString ();
|
||||
doc.InnerHtml = head.ToString()+"\n"+body.ToString ();
|
||||
writer.WriteLine (doctype);
|
||||
writer.Write (doc.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
138
booking/Formatters/EstimToPdfFormatter.MSAN.cs
Normal file
138
booking/Formatters/EstimToPdfFormatter.MSAN.cs
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
//
|
||||
// EstimToPdfFormatter.cs
|
||||
//
|
||||
// Author:
|
||||
// Paul Schneider <paulschneider@free.fr>
|
||||
//
|
||||
// Copyright (c) 2014 Paul Schneider
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#if MicrosoftAspNetMvc
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using System.Web.Profile;
|
||||
using Yavsc.Model.RolesAndMembers;
|
||||
using Yavsc.Model.WorkFlow;
|
||||
using System.Net.Http.Formatting;
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
namespace Yavsc.Formatters
|
||||
{
|
||||
/// <summary>
|
||||
/// Estim to pdf formatter.
|
||||
/// </summary>
|
||||
public class EstimToPdfFormatter: BufferedMediaTypeFormatter
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Yavsc.Formatters.EstimToPdfFormatter"/> class.
|
||||
/// </summary>
|
||||
public EstimToPdfFormatter ()
|
||||
{
|
||||
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/pdf"));
|
||||
}
|
||||
/// <summary>
|
||||
/// Determines whether this instance can read type the specified type.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if this instance can read type the specified type; otherwise, <c>false</c>.</returns>
|
||||
/// <param name="type">Type.</param>
|
||||
public override bool CanReadType(Type type)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
/// <summary>
|
||||
/// Determines whether this instance can write type the specified type.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if this instance can write type the specified type; otherwise, <c>false</c>.</returns>
|
||||
/// <param name="type">Type.</param>
|
||||
public override bool CanWriteType(System.Type type)
|
||||
{
|
||||
if (type == typeof(Estimate))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Type enumerableType = typeof(IEnumerable<Estimate>);
|
||||
return enumerableType.IsAssignableFrom(type);
|
||||
}
|
||||
}
|
||||
public override void WriteToStream (Type type, object value, Stream writeStream, System.Net.Http.HttpContent content)
|
||||
{
|
||||
|
||||
// TODO create a type containing generation parameters, including a template path, and generate from them
|
||||
|
||||
Yavsc.templates.Estim tmpe = new Yavsc.templates.Estim();
|
||||
tmpe.Session = new Dictionary<string,object>();
|
||||
Estimate e = value as Estimate;
|
||||
tmpe.Session.Add ("estim", e);
|
||||
|
||||
Profile prpro = new Profile (ProfileBase.Create (e.Responsible));
|
||||
|
||||
var pbc = ProfileBase.Create (e.Client);
|
||||
Profile prcli = new Profile (pbc);
|
||||
|
||||
if (!prpro.HasBankAccount || !prcli.IsBillable)
|
||||
throw new Exception("account number for provider, or client not billable.");
|
||||
|
||||
tmpe.Session.Add ("from", prpro);
|
||||
tmpe.Session.Add ("to", prcli);
|
||||
tmpe.Init ();
|
||||
|
||||
string contentStr = tmpe.TransformText ();
|
||||
|
||||
string name = string.Format ("tmpestimtex-{0}", e.Id);
|
||||
string fullname = Path.Combine (
|
||||
HttpRuntime.CodegenDir, name);
|
||||
FileInfo fi = new FileInfo(fullname + ".tex");
|
||||
FileInfo fo = new FileInfo(fullname + ".pdf");
|
||||
using (StreamWriter sw = new StreamWriter (fi.FullName))
|
||||
{
|
||||
sw.Write (contentStr);
|
||||
}
|
||||
using (Process p = new Process ()) {
|
||||
p.StartInfo.WorkingDirectory = HttpRuntime.CodegenDir;
|
||||
p.StartInfo = new ProcessStartInfo ();
|
||||
p.StartInfo.UseShellExecute = false;
|
||||
p.StartInfo.FileName = "/usr/bin/texi2pdf";
|
||||
p.StartInfo.Arguments =
|
||||
string.Format ("--batch --build-dir={2} -o {0} {1}",
|
||||
fo.FullName,
|
||||
fi.FullName,HttpRuntime.CodegenDir);
|
||||
p.Start ();
|
||||
p.WaitForExit ();
|
||||
if (p.ExitCode != 0)
|
||||
throw new Exception ("Pdf generation failed with exit code:" + p.ExitCode);
|
||||
}
|
||||
|
||||
using (StreamReader sr = new StreamReader (fo.FullName)) {
|
||||
byte[] buffer = File.ReadAllBytes (fo.FullName);
|
||||
writeStream.Write(buffer,0,buffer.Length);
|
||||
}
|
||||
fi.Delete();
|
||||
fo.Delete();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
134
booking/Formatters/EstimToPdfFormatter.cs
Normal file
134
booking/Formatters/EstimToPdfFormatter.cs
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
//
|
||||
// EstimToPdfFormatter.cs
|
||||
//
|
||||
// Author:
|
||||
// Paul Schneider <paulschneider@free.fr>
|
||||
//
|
||||
// Copyright (c) 2014 Paul Schneider
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Formatting;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Web;
|
||||
using System.Web.Profile;
|
||||
using Yavsc.Model.RolesAndMembers;
|
||||
using Yavsc.Model.WorkFlow;
|
||||
|
||||
namespace Yavsc.Formatters
|
||||
{
|
||||
/// <summary>
|
||||
/// Estim to pdf formatter.
|
||||
/// </summary>
|
||||
public class EstimToPdfFormatter: BufferedMediaTypeFormatter
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Yavsc.Formatters.EstimToPdfFormatter"/> class.
|
||||
/// </summary>
|
||||
public EstimToPdfFormatter ()
|
||||
{
|
||||
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/pdf"));
|
||||
}
|
||||
/// <summary>
|
||||
/// Determines whether this instance can read type the specified type.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if this instance can read type the specified type; otherwise, <c>false</c>.</returns>
|
||||
/// <param name="type">Type.</param>
|
||||
public override bool CanReadType(Type type)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
/// <summary>
|
||||
/// Determines whether this instance can write type the specified type.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if this instance can write type the specified type; otherwise, <c>false</c>.</returns>
|
||||
/// <param name="type">Type.</param>
|
||||
public override bool CanWriteType(System.Type type)
|
||||
{
|
||||
if (type == typeof(Estimate))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Type enumerableType = typeof(IEnumerable<Estimate>);
|
||||
return enumerableType.IsAssignableFrom(type);
|
||||
}
|
||||
}
|
||||
|
||||
public override void WriteToStream (Type type, object value, Stream stream, HttpContent content)
|
||||
{
|
||||
|
||||
// TODO create a type containing generation parameters, including a template path, and generate from them
|
||||
|
||||
Yavsc.templates.Estim tmpe = new Yavsc.templates.Estim();
|
||||
tmpe.Session = new Dictionary<string,object>();
|
||||
Estimate e = value as Estimate;
|
||||
tmpe.Session.Add ("estim", e);
|
||||
|
||||
Profile prpro = new Profile (ProfileBase.Create (e.Responsible));
|
||||
|
||||
var pbc = ProfileBase.Create (e.Client);
|
||||
Profile prcli = new Profile (pbc);
|
||||
|
||||
if (!prpro.HasBankAccount || !prcli.IsBillable)
|
||||
throw new Exception("account number for provider, or client not billable.");
|
||||
|
||||
tmpe.Session.Add ("from", prpro);
|
||||
tmpe.Session.Add ("to", prcli);
|
||||
tmpe.Init ();
|
||||
|
||||
string contentStr = tmpe.TransformText ();
|
||||
|
||||
string name = string.Format ("tmpestimtex-{0}", e.Id);
|
||||
string fullname = Path.Combine (
|
||||
HttpRuntime.CodegenDir, name);
|
||||
FileInfo fi = new FileInfo(fullname + ".tex");
|
||||
FileInfo fo = new FileInfo(fullname + ".pdf");
|
||||
using (StreamWriter sw = new StreamWriter (fi.FullName))
|
||||
{
|
||||
sw.Write (contentStr);
|
||||
}
|
||||
using (Process p = new Process ()) {
|
||||
p.StartInfo.WorkingDirectory = HttpRuntime.CodegenDir;
|
||||
p.StartInfo = new ProcessStartInfo ();
|
||||
p.StartInfo.UseShellExecute = false;
|
||||
p.StartInfo.FileName = "/usr/bin/texi2pdf";
|
||||
p.StartInfo.Arguments =
|
||||
string.Format ("--batch --build-dir={2} -o {0} {1}",
|
||||
fo.FullName,
|
||||
fi.FullName,HttpRuntime.CodegenDir);
|
||||
p.Start ();
|
||||
p.WaitForExit ();
|
||||
if (p.ExitCode != 0)
|
||||
throw new Exception ("Pdf generation failed with exit code:" + p.ExitCode);
|
||||
}
|
||||
|
||||
using (StreamReader sr = new StreamReader (fo.FullName)) {
|
||||
byte[] buffer = File.ReadAllBytes (fo.FullName);
|
||||
stream.Write(buffer,0,buffer.Length);
|
||||
}
|
||||
fi.Delete();
|
||||
fo.Delete();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
57
booking/Formatters/FormatterException.cs
Normal file
57
booking/Formatters/FormatterException.cs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
//
|
||||
// TexToPdfFormatter.cs
|
||||
//
|
||||
// Author:
|
||||
// Paul Schneider <paulschneider@free.fr>
|
||||
//
|
||||
// Copyright (c) 2015 Paul Schneider
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
using System;
|
||||
using System.Net.Http.Formatting;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Http;
|
||||
using Yavsc.Helpers;
|
||||
|
||||
namespace Yavsc.Formatters
|
||||
{
|
||||
/// <summary>
|
||||
/// Formatter exception.
|
||||
/// </summary>
|
||||
public class FormatterException : Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Yavsc.Formatters.FormatterException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="message">Message.</param>
|
||||
public FormatterException(string message):base(message)
|
||||
{
|
||||
}
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Yavsc.Formatters.FormatterException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="message">Message.</param>
|
||||
/// <param name="innerException">Inner exception.</param>
|
||||
public FormatterException(string message,Exception innerException):base(message,innerException)
|
||||
{
|
||||
}
|
||||
|
||||
public string Output { get; set; }
|
||||
public string Error { get; set; }
|
||||
}
|
||||
}
|
||||
100
booking/Formatters/RssFeedsFormatter.cs
Normal file
100
booking/Formatters/RssFeedsFormatter.cs
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
//
|
||||
// RssFormatter.cs
|
||||
//
|
||||
// Author:
|
||||
// paul <${AuthorEmail}>
|
||||
//
|
||||
// Copyright (c) 2015 paul
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using System;
|
||||
using System.Net.Http.Formatting;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Web.Mvc;
|
||||
using System.Net;
|
||||
using Yavsc.Model;
|
||||
using System.Text;
|
||||
|
||||
namespace Yavsc.Formatters
|
||||
{
|
||||
/// <summary>
|
||||
/// Rss feeds formatter.
|
||||
/// </summary>
|
||||
public class RssFeedsFormatter:SimpleFormatter
|
||||
{
|
||||
string doctype = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Yavsc.Formatters.RssFeedsFormatter"/> class.
|
||||
/// </summary>
|
||||
public RssFeedsFormatter
|
||||
() : base ("application/rss+xml")
|
||||
{
|
||||
}
|
||||
|
||||
private const string dateformat = "ddd, dd MMM yyyy HH:mm:ss K";
|
||||
public override void WriteToStream (Type type, object value, Stream stream, HttpContent content)
|
||||
{
|
||||
RssFeedsChannel feeds = value as RssFeedsChannel;
|
||||
using (var writer = new StreamWriter (stream)) {
|
||||
TagBuilder rss = new TagBuilder ("rss");
|
||||
rss.Attributes.Add ("version", "2.0");
|
||||
TagBuilder channel = new TagBuilder ("channel");
|
||||
TagBuilder title = new TagBuilder ("title");
|
||||
TagBuilder description = new TagBuilder ("description");
|
||||
TagBuilder lastBuildDate = new TagBuilder ("lastBuildDate");
|
||||
TagBuilder link = new TagBuilder ("link");
|
||||
|
||||
title.InnerHtml = MvcHtmlString.Create (feeds.Title).ToHtmlString ();
|
||||
description.InnerHtml = MvcHtmlString.Create (feeds.Description).ToHtmlString ();
|
||||
lastBuildDate.InnerHtml = MvcHtmlString.Create (feeds.LastBuildDate.ToString (dateformat)).ToHtmlString ();
|
||||
link.InnerHtml = MvcHtmlString.Create (feeds.Link).ToHtmlString ();
|
||||
StringBuilder sb = new StringBuilder ();
|
||||
foreach (RssFeedsEntry e in feeds.Entries) {
|
||||
TagBuilder item = new TagBuilder ("item");
|
||||
TagBuilder ititle = new TagBuilder ("title");
|
||||
ititle.InnerHtml = e.Title;
|
||||
|
||||
TagBuilder idescription = new TagBuilder ("description");
|
||||
idescription.InnerHtml = MvcHtmlString.Create (e.Description).ToHtmlString ();
|
||||
TagBuilder ipubDate = new TagBuilder ("pubDate");
|
||||
ipubDate.InnerHtml = MvcHtmlString.Create (
|
||||
e.PubDate.ToString (dateformat)).ToHtmlString ();
|
||||
|
||||
TagBuilder ilink = new TagBuilder ("link");
|
||||
ilink.InnerHtml = MvcHtmlString.Create (e.Link).ToHtmlString ();
|
||||
|
||||
item.InnerHtml = ititle.ToString () + "\n" +
|
||||
idescription.ToString () + "\n" +
|
||||
ipubDate.ToString () + "\n" +
|
||||
ilink.ToString () + "\n";
|
||||
|
||||
sb.Append (item.ToString () + "\n");
|
||||
}
|
||||
channel.InnerHtml = title.ToString () + "\n" +
|
||||
description.ToString () + "\n" +
|
||||
lastBuildDate.ToString () + "\n" +
|
||||
link.ToString () + "\n" +
|
||||
sb.ToString () + "\n";
|
||||
rss.InnerHtml = channel.ToString ();
|
||||
writer.WriteLine (doctype);
|
||||
writer.Write (rss.ToString ());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
84
booking/Formatters/SimpleFormatter.cs
Normal file
84
booking/Formatters/SimpleFormatter.cs
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
//
|
||||
// TexFormatter.cs
|
||||
//
|
||||
// Author:
|
||||
// Paul Schneider <paulschneider@free.fr>
|
||||
//
|
||||
// Copyright (c) 2014 Paul Schneider
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
using System;
|
||||
using System.Net.Http.Formatting;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Web.Mvc;
|
||||
using System.Net;
|
||||
|
||||
namespace Yavsc.Formatters
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple formatter.
|
||||
/// </summary>
|
||||
public class SimpleFormatter : BufferedMediaTypeFormatter
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Yavsc.Formatters.SimpleFormatter"/> class.
|
||||
/// </summary>
|
||||
/// <param name="mimetype">Mimetype.</param>
|
||||
public SimpleFormatter (string mimetype)
|
||||
{
|
||||
SupportedMediaTypes.Add(new MediaTypeHeaderValue(mimetype));
|
||||
}
|
||||
/// <summary>
|
||||
/// Determines whether this instance can write type the specified type.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if this instance can write type the specified type; otherwise, <c>false</c>.</returns>
|
||||
/// <param name="type">Type.</param>
|
||||
public override bool CanWriteType(System.Type type)
|
||||
{
|
||||
if (type == typeof(string))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Type enumerableType = typeof(IEnumerable<string>);
|
||||
return enumerableType.IsAssignableFrom(type);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Determines whether this instance can read type the specified type.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if this instance can read type the specified type; otherwise, <c>false</c>.</returns>
|
||||
/// <param name="type">Type.</param>
|
||||
public override bool CanReadType(Type type)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public override void WriteToStream (Type type, object value, Stream writeStream, HttpContent content)
|
||||
{
|
||||
using (var writer = new StreamWriter(writeStream))
|
||||
{
|
||||
string doc = value as string;
|
||||
writer.Write (doc);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
137
booking/Formatters/TexToPdfFormatter.cs
Normal file
137
booking/Formatters/TexToPdfFormatter.cs
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
//
|
||||
// TexToPdfFormatter.cs
|
||||
//
|
||||
// Author:
|
||||
// Paul Schneider <paulschneider@free.fr>
|
||||
//
|
||||
// Copyright (c) 2015 Paul Schneider
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
using System;
|
||||
using System.Net.Http.Formatting;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Http;
|
||||
using Yavsc.Helpers;
|
||||
|
||||
namespace Yavsc.Formatters
|
||||
{
|
||||
/// <summary>
|
||||
/// Tex to pdf formatter.
|
||||
/// </summary>
|
||||
public class TexToPdfFormatter: BufferedMediaTypeFormatter
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Yavsc.Formatters.TexToPdfFormatter"/> class.
|
||||
/// </summary>
|
||||
public TexToPdfFormatter ()
|
||||
{
|
||||
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/pdf"));
|
||||
}
|
||||
/// <summary>
|
||||
/// Determines whether this instance can read type the specified type.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if this instance can read type the specified type; otherwise, <c>false</c>.</returns>
|
||||
/// <param name="type">Type.</param>
|
||||
public override bool CanReadType(Type type)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
/// <summary>
|
||||
/// Determines whether this instance can write type the specified type.
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if this instance can write type the specified type; otherwise, <c>false</c>.</returns>
|
||||
/// <param name="type">Type.</param>
|
||||
public override bool CanWriteType(System.Type type)
|
||||
{
|
||||
if (type == typeof(string))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Type enumerableType = typeof(IEnumerable<string>);
|
||||
return enumerableType.IsAssignableFrom(type);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Writes to stream.
|
||||
/// </summary>
|
||||
/// <param name="type">Type.</param>
|
||||
/// <param name="value">Value.</param>
|
||||
/// <param name="stream">Stream.</param>
|
||||
/// <param name="contentHeaders">Content headers.</param>
|
||||
///
|
||||
public override void WriteToStream (Type type, object value, Stream stream, HttpContent content)
|
||||
{
|
||||
|
||||
string temp = Path.GetTempPath ();
|
||||
string cntStr = value as string;
|
||||
string name = "tmpdoc-"+Guid.NewGuid().ToString();
|
||||
string fullname = Path.Combine (temp, name);
|
||||
FileInfo fi = new FileInfo(fullname + ".tex");
|
||||
FileInfo fo = null;
|
||||
using (StreamWriter sw = new StreamWriter (fi.OpenWrite()))
|
||||
{
|
||||
sw.Write (cntStr);
|
||||
sw.Close ();
|
||||
}
|
||||
|
||||
using (Process p = new Process ()) {
|
||||
|
||||
Directory.SetCurrentDirectory (temp);
|
||||
|
||||
p.StartInfo.WorkingDirectory = temp;
|
||||
p.StartInfo = new ProcessStartInfo ();
|
||||
p.StartInfo.UseShellExecute = false;
|
||||
p.StartInfo.FileName = "texi2pdf";
|
||||
p.StartInfo.Arguments =
|
||||
string.Format ("--batch {0}",
|
||||
fi.FullName);
|
||||
p.StartInfo.RedirectStandardOutput = true;
|
||||
p.StartInfo.RedirectStandardError = true;
|
||||
|
||||
p.Start ();
|
||||
p.WaitForExit ();
|
||||
|
||||
if (p.ExitCode != 0) {
|
||||
var ex = new FormatterException ("Pdf generation failed with exit code:" + p.ExitCode);
|
||||
ex.Output = p.StandardOutput.ReadToEnd ()+"\nCWD:"+temp;
|
||||
ex.Error = p.StandardError.ReadToEnd ();
|
||||
throw ex;
|
||||
}
|
||||
fo = new FileInfo(name + ".pdf");
|
||||
}
|
||||
|
||||
byte[] buffer = File.ReadAllBytes (fo.Name);
|
||||
stream.Write(buffer,0,buffer.Length);
|
||||
if (content.Headers != null)
|
||||
SetFileName(content.Headers, value.GetHashCode ().ToString ());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the name of the file.
|
||||
/// </summary>
|
||||
/// <param name="contentHeaders">Content headers.</param>
|
||||
/// <param name="basename">Basename.</param>
|
||||
public static void SetFileName(HttpContentHeaders contentHeaders, string basename) {
|
||||
contentHeaders.ContentDisposition = new ContentDispositionHeaderValue ("attachment") {
|
||||
FileName = "doc-" + basename + ".pdf"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue