예제 #1
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="package">The package to convert</param>
        /// <returns>PackageConversionResult.</returns>
        public async Task <PackageConversionResult> ConvertPackage(string id, AssetType assetType, bool protectWithPassword, RepositoryPackage package)
        {
            var retVal = new PackageConversionResult {
                Valid = true
            };
            var packageFileName = Path.Combine(Settings.Default.PickupPath, package.PackageFileName);
            var fi = new FileInfo(packageFileName);

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

            if (File.Exists(fi.FullName))
            {
                Log.Information("Found zip file {Name}. File is already unzipped.", fi.Name);
                var tempFolder = Path.Combine(fi.DirectoryName ?? throw new InvalidOperationException(), fi.Name.Remove(fi.Name.Length - fi.Extension.Length));
                try
                {
                    var metadataFile = Path.Combine(tempFolder, "header", "metadata.xml");
                    var paket        = (PaketDIP)Paket.LoadFromFile(metadataFile);

                    var contentFolder = Path.Combine(tempFolder, "content");
                    var context       = new JobContext {
                        ArchiveRecordId = package.ArchiveRecordId, PackageId = package.PackageId
                    };
                    await ConvertFiles(id, package.Files, paket, tempFolder, contentFolder, context);
                    await ConvertFolders(id, package.Folders, paket, tempFolder, contentFolder, context);

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

                    AddReadmeFile(tempFolder);
                    AddDesignFiles(tempFolder);
                    CreateIndexHtml(tempFolder, package.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);
        }
예제 #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);
        }