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 300 -background white -alpha remove " + StorageRoot + firstPart + " " + StorageRoot + firstPart + "-%04d-hires.png\""); // Density 300 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 Documents RasterizeOne(string fullyQualifiedFileName, string clientFileName, string guid, Person uploader, Organization organization = null, ProgressRange progressRange = null) { int pdfPageCount = GetPageCount(fullyQualifiedFileName); string relativeFileName = fullyQualifiedFileName.Substring(Document.StorageRoot.Length); Process process = Process.Start("bash", "-c \"gs -dNOPAUSE -dBATCH -sDEVICE=png16m -r75 -sOutputFile=" + fullyQualifiedFileName + "-%04d.png " + fullyQualifiedFileName + "\""); Documents documents = new Documents(); if (progressRange == null) { progressRange = new ProgressRange(); } int pageCounter = 1; // the first produced page will be numbered ONE, not ImageMagick's 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 = progressRange.Minimum; int progress = progressRange.Minimum; while (pageCounter <= pdfPageCount) { while (!File.Exists(Document.StorageRoot + testPageFileName)) { // Wait for file to appear if (!process.HasExited) { process.WaitForExit(250); } /* -- old ImageMagick-dependent code * 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 / pdfPageCount; * if (currentFilePercentage > 50) * { * currentFilePercentage = 50; // we may be not the only one converting right now * } * * progress = progressRange.Minimum + currentFilePercentage * 100 / progressRange.Range; * if (progress > lastProgress) // can't use Not-Equal; temp files slowly deleted before next step * { * BroadcastProgress(organization, guid, progress); * lastProgress = progress; * } * }*/ } progress = progressRange.Minimum + ((pageCounter + 1) * progressRange.Range / pdfPageCount); if (progress > lastProgress) { BroadcastProgress(organization, guid, progress); lastProgress = progress; } // If the page# file that has appeared is 2+, then the preceding file is ready if (pageCounter > 1) { long fileLength = new FileInfo(Document.StorageRoot + lastPageFileName).Length; documents.Add(Document.Create(lastPageFileName, clientFileName + " {{LOCPAGE-" + (pageCounter - 1).ToString(CultureInfo.InvariantCulture) + "-" + pdfPageCount.ToString(CultureInfo.InvariantCulture) + "}}", fileLength, guid, null, uploader)); // 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; documents.Add(Document.Create(lastPageFileName, clientFileName + " {{LOCPAGE-" + pdfPageCount.ToString(CultureInfo.InvariantCulture) + "-" + pdfPageCount.ToString(CultureInfo.InvariantCulture) + "}}", fileLengthLastPage, guid, null, uploader)); // Set to readonly, lock out changes, permit all read Syscall.chmod(Document.StorageRoot + lastPageFileName, FilePermissions.S_IRUSR | FilePermissions.S_IRGRP | FilePermissions.S_IROTH); return(documents); }
public Documents RasterizeOne(string fullyQualifiedFileName, string clientFileName, string guid, Person uploader, Organization organization = null, ProgressRange progressRange = null) { int pdfPageCount = GetPageCount(fullyQualifiedFileName); string relativeFileName = fullyQualifiedFileName.Substring(Document.StorageRoot.Length); Process process = Process.Start("bash", "-c \"convert -density 75 -background white -alpha remove " + fullyQualifiedFileName + " " + fullyQualifiedFileName + "-%04d.png\""); Documents documents = new Documents(); if (progressRange == null) { progressRange = new ProgressRange(); } 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 = progressRange.Minimum; int progress = progressRange.Minimum; while (pageCounter < pdfPageCount) { 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 / pdfPageCount; if (currentFilePercentage > 50) { currentFilePercentage = 50; // we may be not the only one converting right now } progress = progressRange.Minimum + currentFilePercentage * 100 / progressRange.Range; if (progress > lastProgress) // can't use Not-Equal; temp files slowly deleted before next step { BroadcastProgress(organization, guid, progress); lastProgress = progress; } } } progress = progressRange.Minimum + progressRange.Range / 2 + ((pageCounter + 1) * progressRange.Range / pdfPageCount) / 2; if (progress > lastProgress) { BroadcastProgress(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; documents.Add(Document.Create(lastPageFileName, clientFileName + " {{LOCPAGE-" + (pageCounter).ToString(CultureInfo.InvariantCulture) + "-" + pdfPageCount.ToString(CultureInfo.InvariantCulture) + "}}", fileLength, guid, null, uploader)); // 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; documents.Add(Document.Create(lastPageFileName, clientFileName + " {{LOCPAGE-" + (pageCounter).ToString(CultureInfo.InvariantCulture) + "-" + pdfPageCount.ToString(CultureInfo.InvariantCulture) + "}}", fileLengthLastPage, guid, null, uploader)); // Set to readonly, lock out changes, permit all read Syscall.chmod(Document.StorageRoot + lastPageFileName, FilePermissions.S_IRUSR | FilePermissions.S_IRGRP | FilePermissions.S_IROTH); /* -- OLD CODE BELOW -- * process.WaitForExit(); * * int pageCounter = 0; // the first produced page will be zero * * // Create all document records * * * while (pageCounter < pdfPageCount) * { * string pageFileName = String.Format("{0}-{1:D4}.png", relativeFileName, pageCounter); * * if (File.Exists(Document.StorageRoot + pageFileName)) * { * long fileLength = new FileInfo(Document.StorageRoot + pageFileName).Length; * * documents.Add(Document.Create(pageFileName, * clientFileName + " " + (pageCounter + 1).ToString(CultureInfo.InvariantCulture) + "/" + pdfPageCount.ToString(CultureInfo.InvariantCulture), * fileLength, guid, null, uploader)); * * Syscall.chmod(Document.StorageRoot + pageFileName, * FilePermissions.S_IRUSR | FilePermissions.S_IRGRP | FilePermissions.S_IROTH); * * } * * pageCounter++; * } */ return(documents); }