Insane amount of refactoring of file upload; work on Expense Groups
This commit is contained in:
+173
-196
@@ -57,20 +57,24 @@ namespace Swarmops.Frontend.Socket
|
||||
break;
|
||||
case "ConvertPdf":
|
||||
JArray pdfFilesArray = (JArray) json["PdfFiles"];
|
||||
List<string> pdfFilesList = new List<string>();
|
||||
foreach (string pdfFileString in pdfFilesArray)
|
||||
JArray pdfClientNamesArray = (JArray)json["ClientFileNames"];
|
||||
|
||||
List<RasterizationTarget> rasterizationTargets = new List<RasterizationTarget>();
|
||||
for (int loop = 0; loop < pdfFilesArray.Count; loop++)
|
||||
{
|
||||
pdfFilesList.Add(pdfFileString);
|
||||
rasterizationTargets.Add(new RasterizationTarget
|
||||
{
|
||||
FullyQualifiedFileName = (string) pdfFilesArray[loop],
|
||||
ClientFileName = (string) pdfClientNamesArray[loop],
|
||||
});
|
||||
}
|
||||
|
||||
JArray pdfClientNamesArray = (JArray) json["ClientFileNames"];
|
||||
List<string> pdfClientNames = new List<string>();
|
||||
foreach (string pdfClientName in pdfClientNamesArray)
|
||||
{
|
||||
pdfClientNames.Add(pdfClientName);
|
||||
}
|
||||
Organization organization = Organization.FromIdentity((int)json["OrganizationId"]);
|
||||
|
||||
ConvertPdf(pdfFilesList.ToArray(), pdfClientNames.ToArray(), (string) json["Guid"], Person.FromIdentity((int) json["PersonId"]), Organization.FromIdentity((int) json["OrganizationId"]));
|
||||
PdfProcessor pdfMaker = new PdfProcessor();
|
||||
pdfMaker.RasterizationProgress += BroadcastGuidProgress;
|
||||
pdfMaker.RasterizeMany(rasterizationTargets.ToArray(), (string) json["Guid"], _authority.Person,
|
||||
_authority.Organization);
|
||||
|
||||
break;
|
||||
case "ConvertPdfHires":
|
||||
@@ -142,214 +146,187 @@ namespace Swarmops.Frontend.Socket
|
||||
// Sessions.Broadcast("{\"messageType\":\"EditorCount\"," + String.Format("\"editorCount\":\"{0}\"", Sessions.ActiveIDs.ToArray().Length) + '}');
|
||||
}
|
||||
|
||||
/*
|
||||
private void ConvertPdf(string[] pdfFiles, string[] pdfClientNames, string guid, Person person, Organization organization)
|
||||
{
|
||||
List<string> failedConversionFileNames = new List<string>();
|
||||
|
||||
using (StreamWriter debugWriter = new StreamWriter("/tmp/PdfConversionDebug-" + guid + ".txt"))
|
||||
try
|
||||
{
|
||||
int fileCount = pdfFiles.Length;
|
||||
|
||||
try
|
||||
Process process = null;
|
||||
Document lastDocument = null;
|
||||
|
||||
for (int fileIndex = 0; fileIndex < fileCount; fileIndex++)
|
||||
{
|
||||
debugWriter.WriteLine("ConvertPdf started");
|
||||
// Set progress to indicate we're at file 'index' of 'fileCount'
|
||||
|
||||
int fileCount = pdfFiles.Length;
|
||||
int progress = fileIndex * 99 / fileCount; // 99 at most -- 100 indicates complete
|
||||
int progressFileStep = 99 / fileCount;
|
||||
int currentFilePageCount = 0;
|
||||
int currentFilePageStepMilli = 0;
|
||||
|
||||
Process process = null;
|
||||
Document lastDocument = null;
|
||||
string relativeFileName = pdfFiles[fileIndex];
|
||||
|
||||
for (int fileIndex = 0; fileIndex < fileCount; fileIndex++)
|
||||
// Use qpdf to determine the number of pages in the PDF
|
||||
|
||||
string pageCountFileName = "/tmp/pagecount-" + guid + ".txt";
|
||||
|
||||
process = Process.Start("bash",
|
||||
"-c \"qpdf --show-npages " + Document.StorageRoot + relativeFileName + " > " + pageCountFileName + "\"");
|
||||
|
||||
process.WaitForExit();
|
||||
|
||||
if (process.ExitCode == 2)
|
||||
{
|
||||
// Set progress to indicate we're at file 'index' of 'fileCount'
|
||||
|
||||
int progress = fileIndex * 99 / fileCount; // 99 at most -- 100 indicates complete
|
||||
int progressFileStep = 99 / fileCount;
|
||||
int currentFilePageCount = 0;
|
||||
int currentFilePageStepMilli = 0;
|
||||
|
||||
string relativeFileName = pdfFiles[fileIndex];
|
||||
|
||||
debugWriter.WriteLine("------------------------------------");
|
||||
debugWriter.WriteLine("{3:D2}%, Converting PDF file {0} of {1}; {2}", fileIndex + 1, fileCount,
|
||||
relativeFileName, progress);
|
||||
|
||||
// Use qpdf to determine the number of pages in the PDF
|
||||
|
||||
string pageCountFileName = "/tmp/pagecount-" + guid + ".txt";
|
||||
|
||||
process = Process.Start("bash",
|
||||
"-c \"qpdf --show-npages " + Document.StorageRoot + relativeFileName + " > " + pageCountFileName + "\"");
|
||||
|
||||
process.WaitForExit();
|
||||
|
||||
if (process.ExitCode == 2)
|
||||
{
|
||||
// Bad PDF file
|
||||
failedConversionFileNames.Add(
|
||||
((string[])GuidCache.Get("PdfClientNames-" + guid))[fileIndex].Replace("'", "")
|
||||
.Replace("\"", "")); // caution; we're displaying user input, guard against XSS
|
||||
continue;
|
||||
}
|
||||
|
||||
// Read the resulting page count from the file we piped
|
||||
|
||||
using (StreamReader pageCountReader = new StreamReader(pageCountFileName))
|
||||
{
|
||||
string line = pageCountReader.ReadLine().Trim();
|
||||
while (line.StartsWith("WARNING") || line.StartsWith("qpdf:"))
|
||||
{
|
||||
line = pageCountReader.ReadLine().Trim(); // ignore warnings and chatter
|
||||
}
|
||||
currentFilePageCount = Int32.Parse(line);
|
||||
debugWriter.WriteLine("{0:D2}%, parsed to int as {1}", progress, currentFilePageCount);
|
||||
currentFilePageStepMilli = progressFileStep * 1000 / currentFilePageCount;
|
||||
}
|
||||
|
||||
File.Delete(pageCountFileName);
|
||||
|
||||
// Begin the conversion
|
||||
|
||||
debugWriter.WriteLine("{0:D2}%, deleted page count file, starting conversion process", progress);
|
||||
|
||||
// Density 75 means 75dpi means conversion
|
||||
// Hires 600dpi conversion is also made, but from backend after this conversion
|
||||
|
||||
process = Process.Start("bash",
|
||||
"-c \"convert -density 75 -background white -alpha remove " + Document.StorageRoot + relativeFileName +
|
||||
" " + Document.StorageRoot + relativeFileName + "-%04d.png\"");
|
||||
|
||||
int pageCounter = 0; // the first produced page will be zero
|
||||
string testPageFileName = String.Format("{0}-{1:D4}.png", relativeFileName, pageCounter);
|
||||
debugWriter.WriteLine("{0:D2}%, testPageFileName set to {1}", progress, testPageFileName);
|
||||
string lastPageFileName = testPageFileName;
|
||||
|
||||
// Convert works by first calling imagemagick that creates /tmp/magick-* files
|
||||
|
||||
int lastProgress = 0;
|
||||
|
||||
while (pageCounter < currentFilePageCount)
|
||||
{
|
||||
while (!File.Exists(Document.StorageRoot + testPageFileName))
|
||||
{
|
||||
// Wait for file to appear
|
||||
|
||||
debugWriter.WriteLine("{0:D2}%, {2:O}, waiting for page #{1} to appear", progress, pageCounter + 1, DateTime.UtcNow);
|
||||
debugWriter.Flush();
|
||||
GuidCache.Set("Pdf-" + guid + "-Progress", progress);
|
||||
|
||||
if (!process.HasExited)
|
||||
{
|
||||
process.WaitForExit(250);
|
||||
}
|
||||
|
||||
if (pageCounter == 0)
|
||||
{
|
||||
// If first page hasn't appeared yet, check for the Magick temp files
|
||||
|
||||
int currentMagickCount = Directory.GetFiles("/tmp", "magick-*").Count();
|
||||
int currentFilePercentage = currentMagickCount*50/currentFilePageCount;
|
||||
if (currentFilePercentage > 50)
|
||||
{
|
||||
currentFilePercentage = 50; // we may be not the only one converting right now
|
||||
}
|
||||
|
||||
progress = progressFileStep*fileIndex + currentFilePercentage*100/progressFileStep;
|
||||
if (progress > lastProgress) // can't use Not-Equal; temp files slowly deleted before next step
|
||||
{
|
||||
BroadcastGuidProgress(organization, guid, progress);
|
||||
lastProgress = progress;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
progress = progressFileStep * fileIndex + progressFileStep / 2 + currentFilePageStepMilli * (pageCounter + 1) / 2000;
|
||||
if (progress != lastProgress)
|
||||
{
|
||||
BroadcastGuidProgress(organization, guid, progress);
|
||||
lastProgress = progress;
|
||||
}
|
||||
|
||||
debugWriter.WriteLine("{0:D2}%, found page #{1}", progress, pageCounter + 1);
|
||||
|
||||
// If the page# file that has appeared is 1+, then the preceding file is ready
|
||||
|
||||
if (pageCounter > 0)
|
||||
{
|
||||
long fileLength = new FileInfo(Document.StorageRoot + lastPageFileName).Length;
|
||||
debugWriter.WriteLine("{0:D2}%, saving page #{1}, bytecount {2}", progress, pageCounter, fileLength);
|
||||
|
||||
Document.Create(lastPageFileName, pdfClientNames[fileIndex] + " " + pageCounter.ToString(CultureInfo.InvariantCulture),
|
||||
fileLength, guid, null, person);
|
||||
|
||||
// Set to readonly, lock out changes, permit all read
|
||||
|
||||
Syscall.chmod(Document.StorageRoot + lastPageFileName,
|
||||
FilePermissions.S_IRUSR | FilePermissions.S_IRGRP | FilePermissions.S_IROTH);
|
||||
|
||||
// Prepare to save the next file
|
||||
lastPageFileName = testPageFileName;
|
||||
}
|
||||
|
||||
// Increase the page counter and the file we're looking for
|
||||
|
||||
pageCounter++;
|
||||
testPageFileName = String.Format("{0}-{1:D4}.png", relativeFileName, pageCounter);
|
||||
debugWriter.WriteLine("{0:D2}%, testPageFileName set to {1}", progress, testPageFileName);
|
||||
}
|
||||
|
||||
// We've seen the last page being written -- wait for process to exit to assure it's complete
|
||||
debugWriter.WriteLine("{0:D2}%, waiting for process exit", progress);
|
||||
|
||||
if (!process.HasExited)
|
||||
{
|
||||
process.WaitForExit();
|
||||
}
|
||||
|
||||
// Save the last page
|
||||
|
||||
long fileLengthLastPage = new FileInfo(Document.StorageRoot + lastPageFileName).Length;
|
||||
debugWriter.WriteLine("{0:D2}%, saving last page #{1}, bytecount {2}", progress, pageCounter, fileLengthLastPage);
|
||||
|
||||
lastDocument = Document.Create(lastPageFileName,
|
||||
pdfClientNames[fileIndex] + " " + pageCounter.ToString(CultureInfo.InvariantCulture),
|
||||
fileLengthLastPage, guid, null, person);
|
||||
|
||||
// Set to readonly, lock out changes, permit all read
|
||||
|
||||
Syscall.chmod(Document.StorageRoot + lastPageFileName,
|
||||
FilePermissions.S_IRUSR | FilePermissions.S_IRGRP | FilePermissions.S_IROTH);
|
||||
|
||||
// Finally, ask the backend to do the high-res conversions, but now we have the basic, fast ones
|
||||
|
||||
RasterizeDocumentHiresOrder backendOrder = new RasterizeDocumentHiresOrder(lastDocument);
|
||||
backendOrder.Create();
|
||||
|
||||
// Bad PDF file
|
||||
failedConversionFileNames.Add(
|
||||
((string[])GuidCache.Get("PdfClientNames-" + guid))[fileIndex].Replace("'", "")
|
||||
.Replace("\"", "")); // caution; we're displaying user input, guard against XSS
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
debugWriter.WriteLine("Exception thrown: " + e.ToString());
|
||||
// Read the resulting page count from the file we piped
|
||||
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
debugWriter.WriteLine("Handler exiting");
|
||||
using (StreamReader pageCountReader = new StreamReader(pageCountFileName))
|
||||
{
|
||||
string line = pageCountReader.ReadLine().Trim();
|
||||
while (line.StartsWith("WARNING") || line.StartsWith("qpdf:"))
|
||||
{
|
||||
line = pageCountReader.ReadLine().Trim(); // ignore warnings and chatter
|
||||
}
|
||||
currentFilePageCount = Int32.Parse(line);
|
||||
currentFilePageStepMilli = progressFileStep * 1000 / currentFilePageCount;
|
||||
}
|
||||
|
||||
File.Delete(pageCountFileName);
|
||||
|
||||
// Begin the conversion
|
||||
|
||||
// Density 75 means 75dpi means conversion
|
||||
// Hires 600dpi conversion is also made, but from backend after this conversion
|
||||
|
||||
process = Process.Start("bash",
|
||||
"-c \"convert -density 75 -background white -alpha remove " + Document.StorageRoot + relativeFileName +
|
||||
" " + Document.StorageRoot + relativeFileName + "-%04d.png\"");
|
||||
|
||||
int pageCounter = 0; // the first produced page will be zero
|
||||
string testPageFileName = String.Format("{0}-{1:D4}.png", relativeFileName, pageCounter);
|
||||
string lastPageFileName = testPageFileName;
|
||||
|
||||
// Convert works by first calling imagemagick that creates /tmp/magick-* files
|
||||
|
||||
int lastProgress = 0;
|
||||
|
||||
while (pageCounter < currentFilePageCount)
|
||||
{
|
||||
while (!File.Exists(Document.StorageRoot + testPageFileName))
|
||||
{
|
||||
// Wait for file to appear
|
||||
|
||||
if (!process.HasExited)
|
||||
{
|
||||
process.WaitForExit(250);
|
||||
}
|
||||
|
||||
if (pageCounter == 0)
|
||||
{
|
||||
// If first page hasn't appeared yet, check for the Magick temp files
|
||||
|
||||
int currentMagickCount = Directory.GetFiles("/tmp", "magick-*").Count();
|
||||
int currentFilePercentage = currentMagickCount*50/currentFilePageCount;
|
||||
if (currentFilePercentage > 50)
|
||||
{
|
||||
currentFilePercentage = 50; // we may be not the only one converting right now
|
||||
}
|
||||
|
||||
progress = progressFileStep*fileIndex + currentFilePercentage*100/progressFileStep;
|
||||
if (progress > lastProgress) // can't use Not-Equal; temp files slowly deleted before next step
|
||||
{
|
||||
BroadcastGuidProgress(organization, guid, progress);
|
||||
lastProgress = progress;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
progress = progressFileStep * fileIndex + progressFileStep / 2 + currentFilePageStepMilli * (pageCounter + 1) / 2000;
|
||||
if (progress != lastProgress)
|
||||
{
|
||||
BroadcastGuidProgress(organization, guid, progress);
|
||||
lastProgress = progress;
|
||||
}
|
||||
|
||||
// If the page# file that has appeared is 1+, then the preceding file is ready
|
||||
|
||||
if (pageCounter > 0)
|
||||
{
|
||||
long fileLength = new FileInfo(Document.StorageRoot + lastPageFileName).Length;
|
||||
|
||||
Document.Create(lastPageFileName, pdfClientNames[fileIndex] + " " + pageCounter.ToString(CultureInfo.InvariantCulture),
|
||||
fileLength, guid, null, person);
|
||||
|
||||
// Set to readonly, lock out changes, permit all read
|
||||
|
||||
Syscall.chmod(Document.StorageRoot + lastPageFileName,
|
||||
FilePermissions.S_IRUSR | FilePermissions.S_IRGRP | FilePermissions.S_IROTH);
|
||||
|
||||
// Prepare to save the next file
|
||||
lastPageFileName = testPageFileName;
|
||||
}
|
||||
|
||||
// Increase the page counter and the file we're looking for
|
||||
|
||||
pageCounter++;
|
||||
testPageFileName = String.Format("{0}-{1:D4}.png", relativeFileName, pageCounter);
|
||||
}
|
||||
|
||||
// We've seen the last page being written -- wait for process to exit to assure it's complete
|
||||
|
||||
if (!process.HasExited)
|
||||
{
|
||||
process.WaitForExit();
|
||||
}
|
||||
|
||||
// Save the last page
|
||||
|
||||
long fileLengthLastPage = new FileInfo(Document.StorageRoot + lastPageFileName).Length;
|
||||
|
||||
lastDocument = Document.Create(lastPageFileName,
|
||||
pdfClientNames[fileIndex] + " " + pageCounter.ToString(CultureInfo.InvariantCulture),
|
||||
fileLengthLastPage, guid, null, person);
|
||||
|
||||
// Set to readonly, lock out changes, permit all read
|
||||
|
||||
Syscall.chmod(Document.StorageRoot + lastPageFileName,
|
||||
FilePermissions.S_IRUSR | FilePermissions.S_IRGRP | FilePermissions.S_IROTH);
|
||||
|
||||
// Finally, ask the backend to do the high-res conversions, but now we have the basic, fast ones
|
||||
|
||||
RasterizeDocumentHiresOrder backendOrder = new RasterizeDocumentHiresOrder(lastDocument);
|
||||
backendOrder.Create();
|
||||
|
||||
BroadcastGuidProgress(organization, guid, 100);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
BroadcastGuidProgress(organization, guid, 100);
|
||||
}
|
||||
}*/
|
||||
|
||||
private void BroadcastGuidProgress(Organization organization, string guid, int progress)
|
||||
private void BroadcastGuidProgress(object sender, ProgressEventArgs args)
|
||||
{
|
||||
JObject json = new JObject();
|
||||
json["MessageType"] = "ProgressUpdate";
|
||||
json["Guid"] = guid.Replace("-", "_"); // necessary to be a JS token
|
||||
json["Progress"] = progress;
|
||||
json["Guid"] = args.Guid.Replace("-", "_"); // necessary to be a JS token
|
||||
json["Progress"] = args.Progress;
|
||||
|
||||
FrontendLoop.BroadcastToOrganization(organization, json);
|
||||
FrontendLoop.BroadcastToOrganization(args.Organization, json);
|
||||
}
|
||||
|
||||
public Authority Authority { get { return this._authority; } }
|
||||
|
||||
Reference in New Issue
Block a user