Fixing PDF bitmappign bug; adding ability to regen bitmaps

This commit is contained in:
Rick Falkvinge
2017-08-23 18:44:15 +02:00
parent 569ad165c7
commit 1e822ca4ed
6 changed files with 143 additions and 3 deletions
+9
View File
@@ -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");
+26
View File
@@ -36,6 +36,32 @@ namespace Swarmops.Database
}
public BasicDocument[] GetAllDocuments()
{
List<BasicDocument> result = new List<BasicDocument>();
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<BasicDocument> result = new List<BasicDocument>();
+6
View File
@@ -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)
{
+7 -3
View File
@@ -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();
+94
View File
@@ -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;
}
}
}
}
}
+1
View File
@@ -96,6 +96,7 @@
<Compile Include="BotCode\Mappery.cs" />
<Compile Include="BotCode\MediaWatcher.cs" />
<Compile Include="BotCode\NewsletterChecker.cs" />
<Compile Include="BotCode\PdfProcessor.cs" />
<Compile Include="BotCode\PressReleaseChecker.cs" />
<Compile Include="BotCode\RosterHousekeeping.cs" />
<Compile Include="BotCode\SalaryProcessor.cs" />