private Task <PreprocessingResult> ConvertSingleJp2ToPdf(PrepareForTransformationMessage message)
        {
            try
            {
                var tempFolder = GetTempFolder(message.RepositoryPackage);

                var metadataFile = Path.Combine(tempFolder, "header", "metadata.xml");
                var paket        = (PaketDIP)Paket.LoadFromFile(metadataFile);

                // Create pdf documents from scanned jpeg 2000 scans.
                scanProcessor.ConvertSingleJpeg2000ScansToPdfDocuments(paket, tempFolder);

                // Save the changed info to the metadata file
                ((Paket)paket).SaveToFile(metadataFile);

                // As we changed files we need to update the RepositoryPackage
                if (paket.Ablieferung.Bemerkung != "Metadata.xml das nicht zum Inhalt passt für Testsysteme")
                {
                    UpdateRepositoryPackage(message.RepositoryPackage, paket);
                }
                else
                {
                    UpdateRepositoryPackageFromDisk(message.RepositoryPackage, tempFolder);
                }


                return(Task.FromResult(new PreprocessingResult {
                    Success = true
                }));
            }
            catch (Exception ex)
            {
                var msg = "Unexpected error while converting single jpeg 2000 to pdf.";
                Log.Error(ex, msg);
                return(Task.FromResult(new PreprocessingResult {
                    Success = false, ErrorMessage = msg
                }));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Converts a package to a usage copy.
        /// </summary>
        /// <param name="id">ArchiveRecordId oder OrderItemId</param>
        /// <param name="assetType">The asset type.</param>
        /// <param name="fileName">Name of the package file to convert.</param>
        /// <param name="packageId">The id of the ordered package</param>
        /// <returns>PackageConversionResult.</returns>
        public async Task <PackageConversionResult> ConvertPackage(string id, AssetType assetType, bool protectWithPassword, string fileName,
                                                                   string packageId)
        {
            var retVal = new PackageConversionResult {
                Valid = true
            };
            var packageFileName = Path.Combine(Settings.Default.PickupPath, fileName);
            var fi = new FileInfo(packageFileName);

            // Make sure Gebrauchskopien have a packageId
            if (assetType == AssetType.Gebrauchskopie && string.IsNullOrEmpty(packageId))
            {
                throw new InvalidOperationException("Assets of type <Gebrauchskopie> require a packageId");
            }

            if (File.Exists(fi.FullName))
            {
                Log.Information("Found zip file {Name}. Starting to extract...", fi.Name);
                var tempFolder = Path.Combine(fi.DirectoryName, fi.Name.Remove(fi.Name.Length - fi.Extension.Length));
                try
                {
                    // Extract zip file to disk
                    ZipFile.ExtractToDirectory(packageFileName, tempFolder);

                    if (assetType == AssetType.Benutzungskopie)
                    {
                        ConvertAreldaMetadataXml(tempFolder);
                    }

                    var metadataFile = Path.Combine(tempFolder, "header", "metadata.xml");
                    var paket        = (PaketDIP)Paket.LoadFromFile(metadataFile);

                    // Create pdf documents from scanned jpeg 2000 scans.
                    scanProcessor.ConvertSingleJpeg2000ScansToPdfDocuments(paket, tempFolder,
                                                                           parameterHelper.GetSetting <ScansZusammenfassenSettings>());

                    // Get all the files from the subdirectory "content" in the extracted directory
                    var files = new DirectoryInfo(Path.Combine(tempFolder, "content")).GetFiles("*.*", SearchOption.AllDirectories);
                    foreach (var file in files)
                    {
                        Log.Information("Start extracting text for file: {file} for archive record or order id {id}", file, id);
                        var convertedFile = await ConvertFile(file, paket, tempFolder);

                        // Delete the original file, if the convertedFile exists and is not the same as the original file.
                        // In case of PDF the name of the original and converted file could be the same. --> PDF to PDF with OCR
                        if (!string.IsNullOrEmpty(convertedFile) && File.Exists(convertedFile) && convertedFile != file.FullName)
                        {
                            file.Delete();
                        }
                    }

                    paket.Generierungsdatum = DateTime.Today;
                    ((Paket)paket).SaveToFile(metadataFile);

                    AddReadmeFile(tempFolder);
                    AddDesignFiles(tempFolder);
                    CreateIndexHtml(tempFolder, packageId);

                    // Create zip file with the name of the archive
                    var finalZipFolder = Path.Combine(fi.DirectoryName, assetType.ToString(), id);
                    var finalZipFile   = finalZipFolder + ".zip";
                    CreateZipFile(finalZipFolder, finalZipFile, tempFolder, protectWithPassword, id);

                    retVal.FileName = finalZipFile;

                    // if we are here everything is groovy
                    Log.Information("Successfully processed (converted formats) zip file {Name}", fi.Name);
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "Unexpected exception while converting the package.");
                    retVal.Valid        = false;
                    retVal.ErrorMessage = $"Unexpected exception while converting the package.\nException:\n{ex}";
                    return(retVal);
                }
                finally
                {
                    // Delete the temp files
                    if (Directory.Exists(tempFolder))
                    {
                        Directory.Delete(tempFolder, true);
                    }
                }
            }
            else
            {
                Log.Warning("Unable to find the zip file {packageFileName} for conversion.", packageFileName);
                retVal.Valid        = false;
                retVal.ErrorMessage = $"Unable to find the zip file {packageFileName} for conversion.";
                return(retVal);
            }

            return(retVal);
        }