From c3e2bd8971a621c56351e4a32272b9b53ed7c625 Mon Sep 17 00:00:00 2001 From: Cyril Vazquez Date: Tue, 7 Jul 2020 12:16:56 +0200 Subject: [PATCH] Keep container tmpdir and rmdir when done or error --- .../Controller/archiveEntryTrait.php | 96 ++++++++++++------- 1 file changed, 62 insertions(+), 34 deletions(-) diff --git a/src/bundle/recordsManagement/Controller/archiveEntryTrait.php b/src/bundle/recordsManagement/Controller/archiveEntryTrait.php index 8e68dafcd..11bbbd178 100755 --- a/src/bundle/recordsManagement/Controller/archiveEntryTrait.php +++ b/src/bundle/recordsManagement/Controller/archiveEntryTrait.php @@ -88,37 +88,50 @@ trait archiveEntryTrait $archive->depositDate = \laabs::newTimestamp(); if ($zipContainer) { + $zipResource = $archive->digitalResources[0]; $archive = $this->processZipContainer($archive); } else { $this->receiveAttachments($archive); } - // Verify if deposit archive has proper hash - $this->checkintegrity($archive); + try { + // Verify if deposit archive has proper hash + $this->checkintegrity($archive); + + // Load archival profile, service level if specified + // Instantiate description controller + $this->useReferences($archive, 'deposit'); + + // Complete management metadata from profile and service level + $this->completeMetadata($archive); - // Load archival profile, service level if specified - // Instantiate description controller - $this->useReferences($archive, 'deposit'); + $this->useReferences($archive, 'deposit'); - // Complete management metadata from profile and service level - $this->completeMetadata($archive); + // Validate archive metadata and resources + $this->validateCompliance($archive); - $this->useReferences($archive, 'deposit'); + // Check format conversion + $this->convertArchive($archive); - // Validate archive metadata and resources - $this->validateCompliance($archive); + // Generate PDI + package + $this->generateAIP($archive); - // Check format conversion - $this->convertArchive($archive); + // Deposit + $this->deposit($archive); - // Generate PDI + package - $this->generateAIP($archive); + // Send certificate + $this->sendResponse($archive); + } catch (\Exception $exception) { + if ($zipContainer) { + $this->deleteZipContainer($archive, $zipResource); + } - // Deposit - $this->deposit($archive); + throw $exception; + } - // Send certificate - $this->sendResponse($archive); + if ($zipContainer) { + $this->deleteZipContainer($archive, $zipResource); + } return $archive->archiveId; } @@ -167,23 +180,41 @@ trait archiveEntryTrait */ public function processZipContainer($archive) { - $zip = $archive->digitalResources[0]; + $zipResource = $archive->digitalResources[0]; - $zipDirectory = $this->extractZip($zip); + $zipDirectory = $this->extractZip($zipResource); + $zipResource->tmpdir = $zipDirectory; $archive->digitalResources = []; - $cleanZipDirectory = array_diff(scandir($zipDirectory), array('..', '.')); - $directory = $zipDirectory.DIRECTORY_SEPARATOR.reset($cleanZipDirectory); + $cleanZipDirectory = array_diff(scandir($zipDirectory.DIRECTORY_SEPARATOR.'contents'), array('..', '.')); + // Root must be a directory + $archiveDirectory = realpath($zipDirectory.DIRECTORY_SEPARATOR.'contents'.DIRECTORY_SEPARATOR.reset($cleanZipDirectory)); + if (!is_dir($archiveDirectory)) { + $this->deleteZipContainer($archive, $zipResource); - if (!is_dir($directory)) { throw new \core\Exception("The container file is non-compliant"); } - $this->extractDir($directory, $archive); + try { + $this->extractDir($archiveDirectory, $archive); + } catch (\Exception $exception) { + $this->deleteZipContainer($archive, $zipResource); + + throw $exception; + } return $archive; } + protected function deleteZipContainer($archive, $zipResource) + { + unset($archive->contents); + unset($archive->digitalResources); + + gc_collect_cycles(); + \laabs\rmdir($zipResource->tmpdir, true); + } + /** * Extract zip * @@ -193,26 +224,23 @@ trait archiveEntryTrait */ private function extractZip($zip) { - $packageDir = \laabs\tempdir().DIRECTORY_SEPARATOR."MaarchRM".DIRECTORY_SEPARATOR; + $packageDir = \laabs\tempdir(); if (!is_dir($packageDir)) { mkdir($packageDir, 0777, true); } - $name = \laabs::newId(); - $zipfile = $packageDir.$name.".zip"; - - if (!is_dir($packageDir.$name)) { - mkdir($packageDir.$name, 0777, true); + $zipfile = $packageDir.DIRECTORY_SEPARATOR."container.zip"; + $zipdir = $packageDir.DIRECTORY_SEPARATOR."contents"; + if (!is_dir($zipdir)) { + mkdir($zipdir, 0777, true); } file_put_contents($zipfile, base64_decode($zip->getContents())); - $this->zip->extract($zipfile, $packageDir. $name, false, null, "x"); - - unset($zipfile); + $this->zip->extract($zipfile, $zipdir, false, null, "x"); - return $packageDir.$name; + return $packageDir; } /** -- GitLab