From 2980c23a6cc556e8d9f6b71abefc6c64480d40c7 Mon Sep 17 00:00:00 2001 From: Jacob Sanford Date: Wed, 5 Jun 2024 08:49:23 -0300 Subject: [PATCH] NBNP-448 Remove reliance on drupal filesystem for untracked --- .../src/Entity/SerialPage.php | 75 ++++++++++++++++++- .../newspapers_core/newspapers_core.module | 16 +--- 2 files changed, 74 insertions(+), 17 deletions(-) diff --git a/custom/modules/digital_serial/modules/digital_serial_page/src/Entity/SerialPage.php b/custom/modules/digital_serial/modules/digital_serial_page/src/Entity/SerialPage.php index 3f8070e6..b3ba9960 100644 --- a/custom/modules/digital_serial/modules/digital_serial_page/src/Entity/SerialPage.php +++ b/custom/modules/digital_serial/modules/digital_serial_page/src/Entity/SerialPage.php @@ -431,7 +431,8 @@ public function delete() { if (!empty($page_image)) { $page_image->delete(); } - _newspapers_core_delete_image_dzi($this->id()); + $this->deleteDziFiles(); + $this->deletePdfFile(); parent::delete(); } @@ -440,7 +441,8 @@ public function delete() { */ public function postSave(EntityStorageInterface $storage, $update = TRUE) { $this->movePageImageToPermanentStorage(TRUE); - _newspapers_core_delete_image_dzi($this->id()); + $this->deleteDziFiles(); + $this->deletePdfFile(); parent::postSave($storage, $update); } @@ -485,6 +487,33 @@ public function movePageImageToPermanentStorage($move_file = TRUE) { $file->save(); } + /** + * Removes the DZI files from disk for a page entity. + * + * The recursive remove is potentially dangerous but is mitigated by string + * checks. + */ + public function deleteDziFiles() { + $dzi_path = $this->getDziPath(); + if (!empty($dzi_path)) { + unlink($dzi_path); + $dzi_dir = str_replace('.dzi', '_files', $dzi_path); + if (is_dir($dzi_dir) && str_contains($dzi_dir, '_files')) { + _newspapers_core_rmdir_recursive($dzi_dir); + } + } + } + + /** + * Removes the PDF from disk for a page entity. + */ + public function deletePdfFile() { + $pdf_path = $this->getPdfPath(); + if (!empty($pdf_path)) { + unlink($pdf_path); + } + } + /** * {@inheritDoc} */ @@ -507,6 +536,27 @@ public function getPdfUri() { return ''; } + /** + * {@inheritDoc} + */ + public function getPdfPath() { + $issue = $this->getParentIssue(); + $issue_id = $issue->id(); + $file = $this->getPageImage(); + $pdf_filename = str_replace('.jpg', '.pdf', $file->getFilename()); + $pdf_file_schemas = [ + "/app/html/sites/default/files/serials/pages/pdf/$issue_id/$pdf_filename", + "/app/html/sites/default/files/serials/pages/$issue_id/$pdf_filename" + ]; + + foreach ($pdf_file_schemas as $pdf_file_schema) { + if (file_exists($pdf_file_schema)) { + return $pdf_file_schema; + } + } + return ''; + } + /** * {@inheritDoc} */ @@ -529,4 +579,25 @@ public function getDziUri() { return ''; } + /** + * {@inheritDoc} + */ + public function getDziPath() { + $issue = $this->getParentIssue(); + $issue_id = $issue->id(); + $file = $this->getPageImage(); + $dzi_filename = str_replace('.jpg', '.dzi', $file->getFilename()); + $dzi_file_schemas = [ + "/app/html/sites/default/files/serials/pages/$issue_id/$dzi_filename", + "/app/html/sites/default/files/serials/pages/$dzi_filename" + ]; + + foreach ($dzi_file_schemas as $dzi_file_schema) { + if (file_exists($dzi_file_schema)) { + return $dzi_file_schema; + } + } + return ''; + } + } diff --git a/custom/modules/newspapers_core/newspapers_core.module b/custom/modules/newspapers_core/newspapers_core.module index 415ec20b..407385aa 100644 --- a/custom/modules/newspapers_core/newspapers_core.module +++ b/custom/modules/newspapers_core/newspapers_core.module @@ -1889,21 +1889,7 @@ function _newspapers_core_delete_image_dzi($page_id) { ->getStorage('digital_serial_page') ->load($page_id); if (!empty($page)) { - $dzi_uri = $page->getDziUri(); - if (!empty($dzi_uri)) { - $file_system = \Drupal::service('file_system'); - $abs_dzi_path = $file_system->realpath($dzi_uri); - - if (!empty($abs_dzi_path)) { - if (file_exists($abs_dzi_path) && str_contains($abs_dzi_path, '.dzi')) { - unlink($abs_dzi_path); - } - $abs_dzi_dir = str_replace('.dzi', '_files', $abs_dzi_path); - if (is_dir($abs_dzi_dir) && str_contains($abs_dzi_dir, '_files')) { - _newspapers_core_rmdir_recursive($abs_dzi_dir); - } - } - } + $page->deleteDziFiles(); } }