Ejemplo n.º 1
0
        /// <summary>
        /// The convert if not exist.
        /// </summary>
        /// <param name="file">
        /// The file.
        /// </param>
        /// <param name="ms">
        /// The memory stream.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool ConvertIfNotExist(Domain.Entities.File file, byte[] ms = null)
        {
            var failedFolder     = (string)System.IO.Path.Combine(FileModel.FileStoragePhysicalPath(this.settings), (string)this.settings.FailedPDFsFolder);
            var connectionString = (string)this.settings.ConnectionString;

            return(this.converter.ConvertIfNotExist(new FileDTO(file), failedFolder, connectionString, ms));
        }
Ejemplo n.º 2
0
        public bool ConvertIfNotExist(FileDTO fileDTO, string failedFolder, string connectionString, byte[] ms = null)
        {
            try
            {
                var fileStoragePhysicalPath = (string)FileModel.FileStoragePhysicalPath(this._settings);
                EnsureDirectoryExist(fileStoragePhysicalPath);
                var fileId        = fileDTO.fileId;
                int numberOfPages = fileDTO.numberOfPages;
                var workingDir    = Path.Combine(fileStoragePhysicalPath, fileId.ToString());
                EnsureDirectoryExist(workingDir);
                var pdfFile      = Path.Combine(workingDir, "document.pdf");
                var swfFile      = Path.Combine(workingDir, "document.swf");
                var swfPagedFile = Path.Combine(workingDir, "%.swf");

                if (!System.IO.File.Exists(pdfFile) && ms != null)
                {
                    System.IO.File.WriteAllBytes(pdfFile, ms);
                }

                if (!System.IO.File.Exists(pdfFile))
                {
                    return(false);
                }

                if (System.IO.File.Exists(swfFile) && new FileInfo(pdfFile).LastWriteTime < new FileInfo(swfFile).LastWriteTime)
                {
                    return(true);
                }

                SafelyDeleteSwfFile(swfFile);

                string baseArgs = ((string)_settings.SWFToolsCommandPattern).Replace("{pdfFile}", pdfFile);

                var logger = _logger;

                Task.Factory.StartNew(
                    () =>
                {
                    UpdateFileWithStatus(connectionString, new Guid(fileId), UploadFileStatus.Rendering, logger);
                    try
                    {
                        _pdfProcessor.CheckIfDocumentWasScannedAndFixIfNecessary(pdfFile);
                    }
                    catch (Exception ex)
                    {
                        CopyFileToFailedFolder(connectionString, failedFolder, pdfFile, new Guid(fileId), logger, "_rendering");
                    }

                    UpdateFileWithStatus(connectionString, new Guid(fileId), UploadFileStatus.Converting, logger);
                    bool renderAsBitmap  = false;
                    bool overWriteSource = false;
                    bool bigPageSucceded = this.Convert(baseArgs.Replace("{swfFile}", swfFile), pdfFile, ref renderAsBitmap, ref overWriteSource, () => System.IO.File.Exists(swfFile), (fail) => this.CopyFileToFailedFolder(connectionString, failedFolder, pdfFile, new Guid(fileId), logger, "_converting", UploadFileStatus.ConvertingFailed, fail));

                    if (bigPageSucceded)
                    {
                        // convert page files
                        UpdateFileWithStatus(connectionString, new Guid(fileId), UploadFileStatus.Converted, logger);

                        Convert(baseArgs.Replace("{swfFile}", swfPagedFile), pdfFile, ref renderAsBitmap, ref overWriteSource, () => this.CheckIfSwfPagedFilesExist(swfPagedFile, numberOfPages), (fail) => this.CopyFileToFailedFolder(connectionString, failedFolder, pdfFile, new Guid(fileId), logger, "_pages", UploadFileStatus.ConvertingPagesFailed, fail));
                    }
                });

                return(true);
            }
            catch (Exception ex)
            {
                _logger.Error("Converting error", ex);
                return(false);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// The convert if not exist.
        /// </summary>
        /// <param name="fileDTO">
        /// The file DTO.
        /// </param>
        /// <param name="ms">
        /// The memory stream.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool ConvertIfNotExist(FileDTO fileDTO, string failedFolder, string connectionString, byte[] ms = null)
        {
            try
            {
                var fileStoragePhysicalPath = (string)FileModel.FileStoragePhysicalPath(this.settings);
                var permSWFPattern          = (string)this.settings.PermSWFPattern;
                var permSWFPagePattern      = (string)this.settings.PermSWFPagePattern;
                this.EnsureDirectoryExist(fileStoragePhysicalPath);
                var fileId        = fileDTO.fileId;
                int numberOfPages = fileDTO.numberOfPages;
                var workingDir    = Path.Combine(fileStoragePhysicalPath, fileId.ToString());
                this.EnsureDirectoryExist(workingDir);
                var  pdfFile        = Path.Combine(workingDir, "document.pdf");
                var  swfFile        = Path.Combine(workingDir, "document.swf");
                var  swfPagedFile   = Path.Combine(workingDir, "%.swf");
                bool isDebugEnabled = this.logger.IsDebugEnabled;

                if (!System.IO.File.Exists(pdfFile) && ms != null)
                {
                    System.IO.File.WriteAllBytes(pdfFile, ms);
                }

                if (!System.IO.File.Exists(pdfFile))
                {
                    return(false);
                }

                if (System.IO.File.Exists(swfFile) && new FileInfo(pdfFile).LastWriteTime < new FileInfo(swfFile).LastWriteTime)
                {
                    return(true);
                }

                this.SafelyDeleteSwfFile(swfFile);

                string baseArgs = ((string)this.settings.SWFToolsCommandPattern).Replace("{pdfFile}", pdfFile);

                var logger = this.logger;

                Task.Factory.StartNew(
                    () =>
                {
                    var s = new Stopwatch();
                    if (isDebugEnabled)
                    {
                        logger.Debug("Save SWF pages in background");
                        s.Start();
                    }

                    this.UpdateFileWithStatus(connectionString, fileId, UploadFileStatus.Rendering, logger);
                    try
                    {
                        pdfProcessor.CheckIfDocumentWasScannedAndFixIfNecessary(pdfFile);
                    }
                    catch (Exception ex)
                    {
                        this.CopyFileToFailedFolder(connectionString, failedFolder, pdfFile, fileId, logger, "_rendering");
                    }


                    if (isDebugEnabled)
                    {
                        logger.DebugFormat("Process images PDF took {0}s", s.ElapsedMilliseconds / 1000.0);
                        s.Restart();
                    }

                    this.UpdateFileWithStatus(connectionString, fileId, UploadFileStatus.Converting, logger);
                    bool renderAsBitmap  = false;
                    bool overWriteSource = false;
                    bool bigPageSucceded = this.Convert(baseArgs.Replace("{swfFile}", swfFile), pdfFile, ref renderAsBitmap, ref overWriteSource, () => System.IO.File.Exists(swfFile), (fail) => this.CopyFileToFailedFolder(connectionString, failedFolder, pdfFile, fileId, logger, "_converting", UploadFileStatus.ConvertingFailed, fail));
                    if (isDebugEnabled)
                    {
                        logger.DebugFormat("Convert SWF took {0}s", s.ElapsedMilliseconds / 1000.0);
                        s.Restart();
                    }

                    if (bigPageSucceded)
                    {
                        // convert page files
                        this.UpdateFileWithStatus(connectionString, fileId, UploadFileStatus.Converted, logger);

                        this.Convert(baseArgs.Replace("{swfFile}", swfPagedFile), pdfFile, ref renderAsBitmap, ref overWriteSource, () => this.CheckIfSwfPagedFilesExist(swfPagedFile, numberOfPages), (fail) => this.CopyFileToFailedFolder(connectionString, failedFolder, pdfFile, fileId, logger, "_pages", UploadFileStatus.ConvertingPagesFailed, fail));
                        if (isDebugEnabled)
                        {
                            logger.DebugFormat("Convert SWF pages took {0}s", s.ElapsedMilliseconds / 1000.0);
                        }
                    }
                });

                return(true);
            }
            catch (Exception ex)
            {
                this.logger.Error("Converting error", ex);
                return(false);
            }
        }