Beispiel #1
0
        public Documents RasterizeMany(RasterizationTarget[] targets, string guid, Person uploader,
                                       Organization organization)
        {
            Documents result = new Documents();

            int pageCountTotal   = 0;
            int pageCountRunning = 0;


            foreach (RasterizationTarget target in targets)
            {
                pageCountTotal += GetPageCount(target.FullyQualifiedFileName);
            }


            foreach (RasterizationTarget target in targets)
            {
                int pdfPageCount = GetPageCount(target.FullyQualifiedFileName); // cached

                // Create progress range

                ProgressRange range = new ProgressRange
                {
                    Minimum = pageCountRunning * 100 / pageCountTotal,
                    Maximum = (pageCountRunning + pdfPageCount) * 100 / pageCountTotal
                };

                // Request one conversion

                Documents docs = RasterizeOne(target.FullyQualifiedFileName, target.ClientFileName, guid, uploader,
                                              organization, range);

                // Update pages converted

                pageCountRunning += pdfPageCount;

                // Finally, ask the backend to do the high-res conversions, but now we have the basic, fast ones

                if (docs.Count > 0)
                {
                    RasterizeDocumentHiresOrder backendOrder = new RasterizeDocumentHiresOrder(docs[0]);
                    backendOrder.Create();
                }
            }

            // Set progress to 100% to avoid rounding errors freezing the UI at 99%

            BroadcastProgress(organization, guid, 100);

            return(result);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        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);
        }