From 5deb4e4b2fc5f37ac079fbffa053b434147eb4b4 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sat, 7 Dec 2013 11:47:21 +0000 Subject: [PATCH] Added stub to PDF upload handling (and package dependencies) --- .../control-frontend-internal.template | 2 +- .../control-frontend-stable.template | 2 +- Site5/Automation/UploadFileHandler.ashx | 53 ++++++++++++++++--- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/BuildFiles/Packaging/templates/control-frontend-internal.template b/BuildFiles/Packaging/templates/control-frontend-internal.template index ab74294a..b34c058f 100644 --- a/BuildFiles/Packaging/templates/control-frontend-internal.template +++ b/BuildFiles/Packaging/templates/control-frontend-internal.template @@ -2,7 +2,7 @@ Package: swarmops-frontend-internal Version: --BuildNumber-- Architecture: all Maintainer: Rick Falkvinge (swarmops-maintain@falkvinge.net) -Depends: swarmops-common-internal, apache2 (>= 2.2.20), mono-apache-server2 (>= 2.10-1), libapache2-mod-mono (>= 2.10-1), libgdiplus (>= 2.10-3) +Depends: swarmops-common-internal, apache2 (>= 2.2.20), mono-apache-server2 (>= 2.10-1), libapache2-mod-mono (>= 2.10-1), libgdiplus (>= 2.10-3), imagemagick, ghostscript Conflicts: swarmops-frontend Homepage: http://swarmops.com/ Description: Swarmops Frontend diff --git a/BuildFiles/Packaging/templates/control-frontend-stable.template b/BuildFiles/Packaging/templates/control-frontend-stable.template index 8f40c5ed..fb4445f1 100644 --- a/BuildFiles/Packaging/templates/control-frontend-stable.template +++ b/BuildFiles/Packaging/templates/control-frontend-stable.template @@ -2,7 +2,7 @@ Package: swarmops-frontend Version: --BuildNumber-- Architecture: all Maintainer: Rick Falkvinge (swarmops-maintain@falkvinge.net) -Depends: swarmops-common, apache2 (>= 2.2.20), mono-apache-server2 (>= 2.10-1), libapache2-mod-mono (>= 2.10-1), libgdiplus (>= 2.10-3) +Depends: swarmops-common, apache2 (>= 2.2.20), mono-apache-server2 (>= 2.10-1), libapache2-mod-mono (>= 2.10-1), libgdiplus (>= 2.10-3), imagemagick, ghostscript Conflicts: swarmops-frontend-internal Homepage: http://swarmops.com/ Description: Swarmops Frontend diff --git a/Site5/Automation/UploadFileHandler.ashx b/Site5/Automation/UploadFileHandler.ashx index cefdd35b..a66b5810 100644 --- a/Site5/Automation/UploadFileHandler.ashx +++ b/Site5/Automation/UploadFileHandler.ashx @@ -151,13 +151,23 @@ namespace Swarmops.Frontend.Automation HttpPostedFile file = context.Request.Files[i]; string fullName = Path.GetFileName (file.FileName); - // Store in MemoryStream in order to try to load as an image - AuthenticationData authData = GetAuthenticationDataAndCulture (context); Person uploadingPerson = authData.CurrentUser; Organization currentOrg = authData.CurrentOrganization; + + bool convertPdf = false; + + if (file.FileName.ToLower().EndsWith(".pdf")) + { + convertPdf = true; + } + else + { + + // If not PDF, try to load as an image + /* MemoryStream ms = new MemoryStream(); byte[] fileData = new byte[file.ContentLength]; @@ -171,9 +181,14 @@ namespace Swarmops.Frontend.Automation try { Image image = Image.FromStream (file.InputStream); + image.Dispose(); } catch (Exception) { + // TODO: If general files accepted, then ok, otherwise fuck off + + // TODO: Accept general files + var errorStatus = new FilesStatus (fullName, file.ContentLength); errorStatus.error = "ERR_NOT_IMAGE"; errorStatus.url = string.Empty; @@ -184,6 +199,7 @@ namespace Swarmops.Frontend.Automation // -1 for length means the file was NOT saved, and that it could not be parsed as image. return; } + } string guid = context.Request.QueryString["Guid"]; @@ -221,11 +237,36 @@ namespace Swarmops.Frontend.Automation "File name determination failed; probable file system permissions error"); } - file.InputStream.Position = 0; - file.SaveAs (StorageRoot + fileFolder + Path.DirectorySeparatorChar + fileName); + string relativeFileName = fileFolder + Path.DirectorySeparatorChar + fileName; - Document.Create (fileFolder + Path.DirectorySeparatorChar + fileName, file.FileName, file.ContentLength, - guid, null, authData.CurrentUser); + file.InputStream.Position = 0; + file.SaveAs (StorageRoot + relativeFileName); + + if (convertPdf) + { + // Convert PDF file into a series of PNG images, one per page + + int pageCounter = 0; + + // TODO: ShellExecute ImageMagick Convert + + string testPageFileName = String.Format("{0}-{1:D4}.png", relativeFileName, pageCounter); + + while (File.Exists (StorageRoot + testPageFileName)) + { + Document.Create (fileFolder + Path.DirectorySeparatorChar + fileName, file.FileName, file.ContentLength, + guid, null, authData.CurrentUser); + + pageCounter++; + testPageFileName = String.Format("{0}-{1:D4}.png", relativeFileName, pageCounter); + } + } + else + { + Document.Create (fileFolder + Path.DirectorySeparatorChar + fileName, file.FileName, file.ContentLength, + guid, null, authData.CurrentUser); + + } statuses.Add (new FilesStatus (fullName, file.ContentLength)); }