From 1e822ca4eda525cf306cec954b23d4e3c2067980 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Wed, 23 Aug 2017 18:44:15 +0200 Subject: [PATCH] Fixing PDF bitmappign bug; adding ability to regen bitmaps --- Backend/BackendLoop.cs | 9 +++ Database/Database-Documents.cs | 26 +++++++ Logic/Support/Documents.cs | 6 ++ Site/Automation/UploadFileHandler.ashx | 10 ++- Utility/BotCode/PdfProcessor.cs | 94 ++++++++++++++++++++++++++ Utility/Utility.csproj | 1 + 6 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 Utility/BotCode/PdfProcessor.cs diff --git a/Backend/BackendLoop.cs b/Backend/BackendLoop.cs index b7dbde7f..95953375 100644 --- a/Backend/BackendLoop.cs +++ b/Backend/BackendLoop.cs @@ -166,6 +166,15 @@ namespace Swarmops.Backend return; } + if (args[0].ToLowerInvariant() == "pdfregen") + { + Console.WriteLine("Regenerating all bitmaps from PDF uploads."); + PdfProcessor.RegenerateAll(); + Console.WriteLine("Done."); + return; + } + + if (args[0].ToLower() == "rsm") { Console.WriteLine ("Testing character encoding: räksmörgås RÄKSMÖRGÅS"); diff --git a/Database/Database-Documents.cs b/Database/Database-Documents.cs index 5250c3e9..70d1a92c 100644 --- a/Database/Database-Documents.cs +++ b/Database/Database-Documents.cs @@ -36,6 +36,32 @@ namespace Swarmops.Database } + public BasicDocument[] GetAllDocuments() + { + List result = new List(); + + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = + GetDbCommand( + "SELECT Documents.DocumentId,Documents.ServerFileName,Documents.ClientFileName,Documents.Description,DocumentTypes.Name AS DocumentType,Documents.ForeignId,Documents.FileSize,Documents.UploadedByPersonId,Documents.UploadedDateTime From Documents,DocumentTypes " + + "WHERE Documents.DocumentTypeId=DocumentTypes.DocumentTypeId ORDER BY Documents.DocumentId;", connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + while (reader.Read()) + { + result.Add(ReadDocumentFromDataReader(reader)); + } + + return result.ToArray(); + } + } + } + + public BasicDocument[] GetDocumentsForForeignObject (DocumentType documentType, int foreignId) { List result = new List(); diff --git a/Logic/Support/Documents.cs b/Logic/Support/Documents.cs index dccc3761..5468594b 100644 --- a/Logic/Support/Documents.cs +++ b/Logic/Support/Documents.cs @@ -37,6 +37,12 @@ namespace Swarmops.Logic.Support } + public static Documents GetAll() + { + return FromArray(SwarmDb.GetDatabaseForReading().GetAllDocuments()); + } + + public Document Add (string serverFileName, string clientFileName, Int64 fileSize, string description, Person uploader) { diff --git a/Site/Automation/UploadFileHandler.ashx b/Site/Automation/UploadFileHandler.ashx index efcdd676..763028d1 100644 --- a/Site/Automation/UploadFileHandler.ashx +++ b/Site/Automation/UploadFileHandler.ashx @@ -261,16 +261,20 @@ namespace Swarmops.Frontend.Automation if (WeAreInDebugEnvironment) { process = Process.Start("cmd.exe", - "/c convert -background white -alpha remove " + StorageRoot + relativeFileName + " " + + "/c convert -background white -flatten " + StorageRoot + relativeFileName + " " + StorageRoot + relativeFileName + "-%04d.png"); } else { + // TODO: Send the tast to do this to backend; tell frontend to display an "in progress" + // TODO icon (rotating cogs on top of document maybe) and to listen for "conversion complete" + // TODO message from backend to know there's an accepted document + process = Process.Start("bash", - "-c \"convert -density 300 -background white -alpha remove " + StorageRoot + relativeFileName + " " + + "-c \"convert -density 600 -background white -flatten " + StorageRoot + relativeFileName + " " + StorageRoot + relativeFileName + - "-%04d.png\""); // Density 300 means 300dpi means production-grade conversion + "-%04d.png\""); // Density 600 means 600dpi means production-grade conversion } process.WaitForExit(); diff --git a/Utility/BotCode/PdfProcessor.cs b/Utility/BotCode/PdfProcessor.cs new file mode 100644 index 00000000..ed77c37b --- /dev/null +++ b/Utility/BotCode/PdfProcessor.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Drawing.Text; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Swarmops.Logic.Support; + +namespace Swarmops.Utility.BotCode +{ + public class PdfProcessor + { + private static readonly string StorageRoot = "/var/lib/swarmops/upload/"; + + public static void RegenerateAll() + { + // 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; + } + + 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 -flatten " + StorageRoot + firstPart + " " + + StorageRoot + firstPart + + "-%04d.png\""); // Density 600 means 600dpi means production-grade conversion + + process.WaitForExit(); + } + else + { + // if firstPart and lastFirstPart still match + // we've already regenerated this document + + if (!wroteDots) // cosmetics + { + Console.Write(".."); + wroteDots = true; + } + lastId = document.Identity; + } + } + } + } +} diff --git a/Utility/Utility.csproj b/Utility/Utility.csproj index b32c0fa5..95950b3f 100644 --- a/Utility/Utility.csproj +++ b/Utility/Utility.csproj @@ -96,6 +96,7 @@ +