From f2fadb456cb2e953087c9b51ebbc5ca6e64cebf7 Mon Sep 17 00:00:00 2001 From: j3nsch Date: Wed, 30 Oct 2024 16:17:39 +0100 Subject: [PATCH] #1251 Moved PackageHandler to opus4-import --- modules/sword/models/PackageHandler.php | 204 ------------------------ 1 file changed, 204 deletions(-) delete mode 100644 modules/sword/models/PackageHandler.php diff --git a/modules/sword/models/PackageHandler.php b/modules/sword/models/PackageHandler.php deleted file mode 100644 index 25281ee09..000000000 --- a/modules/sword/models/PackageHandler.php +++ /dev/null @@ -1,204 +0,0 @@ -setPackageType($contentType); - } - - /** - * @param AdditionalEnrichments $additionalEnrichments - */ - public function setAdditionalEnrichments($additionalEnrichments) - { - $this->additionalEnrichments = $additionalEnrichments; - } - - /** - * @param string $contentType - * @throws Exception - */ - private function setPackageType($contentType) - { - if ($contentType === null || $contentType === false) { - throw new Exception('Content-Type header is required'); - } - - switch ($contentType) { - case 'application/zip': - $this->packageType = self::PACKAGE_TYPE_ZIP; - break; - case 'application/tar': - case 'application/x-tar': - $this->packageType = self::PACKAGE_TYPE_TAR; - break; - default: - throw new Exception('Content-Type ' . $contentType . ' is currently not supported'); - } - } - - /** - * Verarbeitet die mit dem SWORD-Request übergebene Paketdatei. - * - * @param string $payload der Inhalt der Paketdatei - * @return ImportStatusDocument|null - */ - public function handlePackage($payload) - { - $packageReader = $this->getPackageReader(); - if ($packageReader === null) { - // TODO improve error handling - return null; - } - - $tmpDirName = null; - try { - $tmpDirName = $this->createTmpDir($payload); - $this->savePackage($payload, $tmpDirName); - - $statusDoc = $packageReader->readPackage($tmpDirName); - } finally { - // TODO copy file before cleanup if error occured - if ($tmpDirName !== null) { - $this->cleanupTmpDir($tmpDirName); - } - } - return $statusDoc; - } - - /** - * Entfernt das zuvor erzeugte temporäre Verzeichnis für die Extraktion des Paketinhalts. - * Das Verzeichnis enthält Dateien und ein Unterverzeichnis. Daher ist ein rekursives Löschen - * erforderlich. - * - * @param string $tmpDirName - */ - private function cleanupTmpDir($tmpDirName) - { - $it = new RecursiveDirectoryIterator($tmpDirName, RecursiveDirectoryIterator::SKIP_DOTS); - $files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST); - foreach ($files as $file) { - if ($file->isDir()) { - rmdir($file->getRealPath()); - } else { - unlink($file->getRealPath()); - } - } - rmdir($tmpDirName); - } - - /** - * Liefert in Abhängigkeit vom zu verarbeitenden Pakettyp ein passendes Objekt zum Einlesen des Pakets zurück. - * Liefert null zurück, wenn der Pakettyp nicht verarbeitet werden kann. - * - * @return AbstractPackageReader - * - * TODO make types configurable and remove explicit TAR/ZIP declarations in this class (use factory class?) - */ - private function getPackageReader() - { - $packageReader = null; - switch ($this->packageType) { - case self::PACKAGE_TYPE_ZIP: - $packageReader = new ZipPackageReader(); - break; - case self::PACKAGE_TYPE_TAR: - $packageReader = new TarPackageReader(); - break; - default: - break; - } - $packageReader->setAdditionalEnrichments($this->additionalEnrichments); - return $packageReader; - } - - /** - * Speichert die übergebene Payload als Datei im übergebenen Verzeichnis ab. - * - * @param string $payload - * @param string $tmpDir - * - * TODO save package into import folder (no longer temporary file) - */ - private function savePackage($payload, $tmpDir) - { - $tmpFileName = $tmpDir . DIRECTORY_SEPARATOR . 'package.' . $this->packageType; - file_put_contents($tmpFileName, $payload); - } - - /** - * Erzeugt ein temporäres Verzeichnis, in dem die mit dem SWORD-Request übergebene Datei zwischengespeichert werden - * kann. Die Methode gibt den absoluten Pfad des Verzeichnisses zurück. - * - * @param string $payload der Inhalt des SWORD-Packages - * @return string absoluter Pfad des temporären Ablageverzeichnisses - * @throws Application_Exception - */ - private function createTmpDir($payload) - { - $baseDirName = Application_Configuration::getInstance()->getTempPath() - . DIRECTORY_SEPARATOR . md5($payload) . '-' . time() . '-' . rand(10000, 99999); - $suffix = 0; - $dirName = "$baseDirName-$suffix"; - while (is_readable($dirName)) { - // add another suffix to make file name unique (even if collision events are not very likely) - $suffix++; - $dirName = "$baseDirName-$suffix"; - } - mkdir($dirName); - return $dirName; - } -}