Moved hires rasterization to new BSO structure
This commit is contained in:
@@ -220,6 +220,7 @@
|
||||
<Compile Include="Support\BackendServiceOrder.cs" />
|
||||
<Compile Include="Support\BackendServiceOrders.cs" />
|
||||
<Compile Include="Support\BackendServices\BackendServiceOrderBase.cs" />
|
||||
<Compile Include="Support\BackendServices\RasterizeDocumentHiresOrder.cs" />
|
||||
<Compile Include="Support\BackendServices\ReturnBitcoinEchoUtxoOrder.cs" />
|
||||
<Compile Include="Support\BlockchainTransaction.cs" />
|
||||
<Compile Include="Support\DocumentImage.cs" />
|
||||
@@ -385,6 +386,7 @@
|
||||
<Compile Include="Support\LogEntries\SecurityActionBase.cs" />
|
||||
<Compile Include="Support\LogEntries\VolunteerForPositionLogEntry.cs" />
|
||||
<Compile Include="Support\ObjectOptionalData.cs" />
|
||||
<Compile Include="Support\PdfProcessor.cs" />
|
||||
<Compile Include="Support\PilotInstallationIds.cs" />
|
||||
<Compile Include="Support\PluralBase.cs" />
|
||||
<Compile Include="Security\ActivityEnum.cs" />
|
||||
|
||||
@@ -7,7 +7,9 @@ using System.Threading.Tasks;
|
||||
using Swarmops.Basic.Types.System;
|
||||
using Swarmops.Common.Exceptions;
|
||||
using Swarmops.Database;
|
||||
using Swarmops.Logic.Structure;
|
||||
using Swarmops.Logic.Support.BackendServices;
|
||||
using Swarmops.Logic.Swarm;
|
||||
|
||||
namespace Swarmops.Logic.Support
|
||||
{
|
||||
@@ -58,6 +60,11 @@ namespace Swarmops.Logic.Support
|
||||
IBackendServiceOrderBase order = (IBackendServiceOrderBase) methodFromXml.Invoke(null, new object[] { this.OrderXml });
|
||||
order.ServiceOrderIdentity = this.Identity; // not part of serialization
|
||||
|
||||
// Supply with organization and person, if we have them
|
||||
|
||||
order.Person = this.PersonId == 0 ? null : Person.FromIdentity(this.PersonId);
|
||||
order.Organization = this.OrganizationId == 0 ? null : Organization.FromIdentity(this.OrganizationId);
|
||||
|
||||
// Mark as started
|
||||
|
||||
SwarmDb.GetDatabaseForWriting().SetBackendServiceOrderActive(this.Identity);
|
||||
@@ -72,6 +79,10 @@ namespace Swarmops.Logic.Support
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
if (!order.HasWorkerThread)
|
||||
{
|
||||
order.HasTerminated = true;
|
||||
}
|
||||
order.Close();
|
||||
SwarmDb.GetDatabaseForWriting().SetBackendServiceOrderException(this.Identity, exception);
|
||||
}
|
||||
@@ -81,6 +92,7 @@ namespace Swarmops.Logic.Support
|
||||
{
|
||||
try
|
||||
{
|
||||
order.HasTerminated = true;
|
||||
order.Close();
|
||||
}
|
||||
catch (DatabaseConcurrencyException)
|
||||
|
||||
@@ -71,6 +71,15 @@ namespace Swarmops.Logic.Support.BackendServices
|
||||
throw new NotImplementedException("The base Run() has no implementation. Derive and declare 'override'!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If you are deriving this class into something that spawns a worker thread, then that derived
|
||||
/// class MUST have the worker thread terminate within 1.00 seconds from when Terminate is called.
|
||||
/// </summary>
|
||||
public virtual void Terminate()
|
||||
{
|
||||
throw new NotImplementedException("The base Terminate() has no implementation. Derive and declare 'override'!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This function MUST be called when the task has been completed, either on completion of Run()
|
||||
/// if you take a short amount of time, or at the end of your worker thread.
|
||||
@@ -99,6 +108,12 @@ namespace Swarmops.Logic.Support.BackendServices
|
||||
public DateTime CreatedDateTime { get; set; }
|
||||
public string BackendServiceClass { get; set; }
|
||||
public int ServiceOrderIdentity { get; set; }
|
||||
public bool HasTerminated { get; set; }
|
||||
|
||||
[XmlIgnore]
|
||||
public Organization Organization { get; set; }
|
||||
[XmlIgnore]
|
||||
public Person Person { get; set; }
|
||||
|
||||
[XmlIgnore]
|
||||
public bool HasWorkerThread { get; set; }
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Swarmops.Logic.Support.BackendServices
|
||||
{
|
||||
[Serializable]
|
||||
public class RasterizeDocumentHiresOrder: BackendServiceOrderBase<RasterizeDocumentHiresOrder>, IBackendServiceOrderBase
|
||||
{
|
||||
[Obsolete("Do not call the parameterless public ctor directly. It is needed for serialization.", true)]
|
||||
public RasterizeDocumentHiresOrder()
|
||||
{ }
|
||||
|
||||
public RasterizeDocumentHiresOrder(Document document)
|
||||
{
|
||||
this.DocumentId = document.Identity;
|
||||
}
|
||||
|
||||
public override void Run()
|
||||
{
|
||||
Document document = Document.FromIdentity(this.DocumentId);
|
||||
HasWorkerThread = true;
|
||||
WorkerThread = new Thread(LongRun);
|
||||
WorkerThread.Start(this);
|
||||
}
|
||||
|
||||
private static void LongRun(object orderObject)
|
||||
{
|
||||
RasterizeDocumentHiresOrder order = (RasterizeDocumentHiresOrder) orderObject;
|
||||
Document document = Document.FromIdentity(order.DocumentId);
|
||||
|
||||
PdfProcessor.Rerasterize((Document) document, PdfProcessor.PdfProcessorOptions.HighQuality | PdfProcessor.PdfProcessorOptions.ForceOrphans);
|
||||
order.Close();
|
||||
order.HasTerminated = true;
|
||||
}
|
||||
|
||||
public override void Terminate()
|
||||
{
|
||||
|
||||
}
|
||||
int DocumentId { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Serialization;
|
||||
using Swarmops.Logic.Structure;
|
||||
using Swarmops.Logic.Swarm;
|
||||
|
||||
namespace Swarmops.Logic.Support
|
||||
{
|
||||
@@ -12,8 +14,16 @@ namespace Swarmops.Logic.Support
|
||||
{
|
||||
void Run();
|
||||
void Close();
|
||||
|
||||
|
||||
bool HasWorkerThread { get; set; }
|
||||
Thread WorkerThread { get; set; }
|
||||
void Terminate();
|
||||
bool HasTerminated { get; set; }
|
||||
|
||||
|
||||
int ServiceOrderIdentity { get; set; }
|
||||
Organization Organization { get; set; }
|
||||
Person Person { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing.Text;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Swarmops.Logic.Support;
|
||||
|
||||
namespace Swarmops.Logic.Support
|
||||
{
|
||||
public class PdfProcessor
|
||||
{
|
||||
private static readonly string StorageRoot = Document.StorageRoot;
|
||||
|
||||
[Flags]
|
||||
public enum PdfProcessorOptions
|
||||
{
|
||||
None = 0,
|
||||
HighQuality = 0x0001,
|
||||
ForceOrphans = 0x0002
|
||||
};
|
||||
|
||||
public static void RerasterizeAll()
|
||||
{
|
||||
// Because of a bug with transparent-background PDFs converting to black-background bitmaps, all PDFs imaged before
|
||||
// DB version 37 need to be re-bitmapped
|
||||
|
||||
Documents documents = Documents.GetAll();
|
||||
|
||||
string firstPart = string.Empty;
|
||||
string lastFirstPart = firstPart;
|
||||
int startId = 0;
|
||||
int lastId = 0;
|
||||
bool wroteDots = false;
|
||||
|
||||
foreach (Document document in documents)
|
||||
{
|
||||
if (document.Description.Length < 32)
|
||||
{
|
||||
continue; // no GUID
|
||||
}
|
||||
|
||||
// a ServerFileName looks like "2015/06/22/29c88f2b-44e6-4a05-a6b0-f9aaec835c5b-00000001-0007-01-0019.png"
|
||||
// 1234567890123456789012345678901234567890123456789012345678901234
|
||||
|
||||
if (document.ServerFileName.Length < 64)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (!document.ServerFileName.ToLowerInvariant().EndsWith(".png"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (document.ForeignId == 0)
|
||||
{
|
||||
// orphan document
|
||||
continue;
|
||||
}
|
||||
|
||||
firstPart = document.ServerFileName.Substring(0, 64);
|
||||
|
||||
if (firstPart != lastFirstPart)
|
||||
{
|
||||
if (startId != 0)
|
||||
{
|
||||
// wrap up the previous one
|
||||
if (lastId != startId)
|
||||
{
|
||||
Console.WriteLine("{0}, done.", lastId);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(", done.");
|
||||
}
|
||||
}
|
||||
|
||||
// Start processing a new document
|
||||
wroteDots = false; // this is just cosmetics
|
||||
|
||||
startId = lastId = document.Identity;
|
||||
|
||||
Console.Write(@"Regenerating document #{0}", startId);
|
||||
|
||||
Process process = Process.Start("bash",
|
||||
"-c \"convert -density 600 -background white -alpha remove " + StorageRoot + firstPart + " " +
|
||||
StorageRoot + firstPart +
|
||||
"-%04d.png\""); // Density 600 means 600dpi means production-grade conversion
|
||||
|
||||
process.WaitForExit();
|
||||
|
||||
lastFirstPart = firstPart;
|
||||
}
|
||||
else
|
||||
{
|
||||
// if firstPart and lastFirstPart still match
|
||||
// we've already regenerated this document
|
||||
|
||||
if (!wroteDots) // cosmetics
|
||||
{
|
||||
Console.Write("..");
|
||||
wroteDots = true;
|
||||
}
|
||||
lastId = document.Identity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Rerasterize(Document document, PdfProcessorOptions options = PdfProcessorOptions.None)
|
||||
{
|
||||
using (StreamWriter debugWriter = new StreamWriter("/tmp/pdfregen-debug.txt"))
|
||||
{
|
||||
debugWriter.WriteLine("Document description: " + document.Description);
|
||||
debugWriter.Flush();
|
||||
|
||||
if (document.Description.Length < 32)
|
||||
{
|
||||
throw new InvalidOperationException("Document has no GUID");
|
||||
}
|
||||
|
||||
// a ServerFileName looks like "2015/06/22/29c88f2b-44e6-4a05-a6b0-f9aaec835c5b-00000001-0007-01-0019.png"
|
||||
// 1234567890123456789012345678901234567890123456789012345678901234
|
||||
|
||||
debugWriter.WriteLine("Document server file name: " + document.ServerFileName);
|
||||
debugWriter.Flush();
|
||||
|
||||
if (document.ServerFileName.Length < 64)
|
||||
{
|
||||
throw new InvalidOperationException("Server file name has wrong scheme");
|
||||
}
|
||||
|
||||
if (!document.ServerFileName.ToLowerInvariant().EndsWith(".png"))
|
||||
{
|
||||
throw new InvalidOperationException("Document is not rasterized");
|
||||
}
|
||||
if (document.ForeignId == 0 && ((int) options & (int) PdfProcessorOptions.ForceOrphans) == 0)
|
||||
{
|
||||
throw new InvalidOperationException("Document is an orphan");
|
||||
}
|
||||
|
||||
int density = 75;
|
||||
string suffix = string.Empty;
|
||||
|
||||
if (((int) options & (int) PdfProcessorOptions.HighQuality) > 0)
|
||||
{
|
||||
density = 600;
|
||||
suffix = "-hires"; // hires conversion uses different filename
|
||||
}
|
||||
|
||||
|
||||
string firstPart = document.ServerFileName.Substring(0, 64);
|
||||
|
||||
debugWriter.WriteLine("Regenerating... ");
|
||||
debugWriter.Flush();
|
||||
|
||||
string commandLine = "convert -density " + density.ToString(CultureInfo.InvariantCulture) +
|
||||
" -background white -alpha remove " + StorageRoot + firstPart + " " +
|
||||
StorageRoot + firstPart +
|
||||
"-%04d" + suffix + ".png";
|
||||
|
||||
debugWriter.WriteLine("Running \"" + commandLine + "\"");
|
||||
debugWriter.Flush();
|
||||
|
||||
Process process = Process.Start("bash",
|
||||
"-c \"" + commandLine + "\"");
|
||||
process.PriorityClass = ProcessPriorityClass.Idle; // play nice - this is a heavy op
|
||||
|
||||
process.WaitForExit();
|
||||
|
||||
debugWriter.WriteLine("Done.");
|
||||
debugWriter.Flush();
|
||||
}
|
||||
}
|
||||
|
||||
public static string TemplateToPdf(string svgFileName)
|
||||
{
|
||||
if (svgFileName.Contains('\"'))
|
||||
{
|
||||
throw new ArgumentException("Filename must not contain double quotes");
|
||||
}
|
||||
if (svgFileName.Contains(' '))
|
||||
{
|
||||
throw new ArgumentException("Filename must not contain whitespace");
|
||||
}
|
||||
|
||||
if (!File.Exists(svgFileName))
|
||||
{
|
||||
throw new ArgumentException("File does not exist");
|
||||
}
|
||||
|
||||
string tempFolder = @"/tmp/";
|
||||
|
||||
Guid guid = new Guid();
|
||||
|
||||
string tempFileName = tempFolder + guid.ToString("D") + ".pdf";
|
||||
|
||||
Process process = Process.Start("bash",
|
||||
"-c \"inkscape --without-gui --export-to-pdf=" + tempFileName + " " + svgFileName + "\""); // inkscape is a package-level dependency
|
||||
|
||||
process.WaitForExit();
|
||||
|
||||
return tempFileName;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user