From 6f865cc4bf8baeb2507848ba6a6bd0b571215ad8 Mon Sep 17 00:00:00 2001 From: Jacob Sanford Date: Mon, 17 Jun 2024 14:00:33 -0300 Subject: [PATCH] NBNP-448 Avoid using URIs for paths and files that may not exist --- .../src/Entity/SerialIssue.php | 16 ++++++--- .../src/Entity/SerialPage.php | 19 ++++++++-- .../src/Entity/SerialTitle.php | 36 +++++++++++++++++++ .../src/PageStorageMigration2024Helper.php | 17 ++++----- 4 files changed, 74 insertions(+), 14 deletions(-) diff --git a/custom/modules/digital_serial/modules/digital_serial_issue/src/Entity/SerialIssue.php b/custom/modules/digital_serial/modules/digital_serial_issue/src/Entity/SerialIssue.php index 69f72ebf..fccd0796 100644 --- a/custom/modules/digital_serial/modules/digital_serial_issue/src/Entity/SerialIssue.php +++ b/custom/modules/digital_serial/modules/digital_serial_issue/src/Entity/SerialIssue.php @@ -634,16 +634,24 @@ public function getChildPageImagePaths() { * {@inheritDoc} */ public function createStoragePath() { - $file_system = \Drupal::service('file_system'); - $issue_absolute_path = $file_system->realpath( - $this->getStorageUri() - ); + $title = $this->getParentTitle(); + $title->createStoragePath(); + $issue_absolute_path = $this->getStoragePath(); if (!file_exists($issue_absolute_path)) { mkdir($issue_absolute_path, 0755, TRUE); } return $issue_absolute_path; } + /** + * {@inheritDoc} + */ + public function getStoragePath() { + $title_id = $this->getParentTitleId(); + $issue_id = $this->id(); + return DRUPAL_ROOT . "/sites/default/files/serials/pages/$title_id/$issue_id"; + } + /** * {@inheritDoc} */ 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 6cead76b..e3de045e 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 @@ -486,6 +486,20 @@ public function getPagePermImageStorageUri() { return ''; } + /** + * {@inheritDoc} + */ + public function getPagePermImageStoragePath() { + $issue = $this->getParentIssue(); + $file = $this->getPageImage(); + + if (!empty($issue) && !empty($file)) { + return $issue->getStoragePath() . '/' . $file->getFilename(); + } + return ''; + } + + /** * {@inheritDoc} */ @@ -500,12 +514,13 @@ public function movePageImageToPermanentStorage($move_file = TRUE) { return; } $file_system = \Drupal::service('file_system'); - $perm_storage_absolute_file_location = $file_system->realpath($perm_storage_uri); + $issue = $this->getParentIssue(); + $issue->createStoragePath(); if ($move_file) { $file_system->move( $file_system->realpath($file->getFileUri()), - $perm_storage_absolute_file_location + $this->getPagePermImageStoragePath() ); } diff --git a/custom/modules/digital_serial/modules/digital_serial_title/src/Entity/SerialTitle.php b/custom/modules/digital_serial/modules/digital_serial_title/src/Entity/SerialTitle.php index 3da31bb1..637e5e15 100644 --- a/custom/modules/digital_serial/modules/digital_serial_title/src/Entity/SerialTitle.php +++ b/custom/modules/digital_serial/modules/digital_serial_title/src/Entity/SerialTitle.php @@ -330,4 +330,40 @@ public function updateDigitalHoldingRecord() { drupal_flush_all_caches(); } + /** + * {@inheritDoc} + */ + public function getStorageUri() { + $default_file_scheme = \Drupal::config('system.file')->get('default_scheme'); + $title_id = $this->id(); + return "$default_file_scheme://serials/pages/$title_id"; + } + + /** + * {@inheritDoc} + */ + public function getStoragePath() { + $title_id = $this->id(); + return DRUPAL_ROOT . "/sites/default/files/serials/pages/$title_id"; + } + + /** + * {@inheritDoc} + */ + public function createStoragePath() { + $title_absolute_path = $this->getStoragePath(); + if (!file_exists($title_absolute_path)) { + mkdir($title_absolute_path, 0755, TRUE); + } + return $title_absolute_path; + } + + /** + * {@inheritDoc} + */ + public function postSave(EntityStorageInterface $storage, $update = TRUE) { + $this->createStoragePath(); + parent::postSave($storage, $update); + } + } diff --git a/custom/modules/newspapers_core/src/PageStorageMigration2024Helper.php b/custom/modules/newspapers_core/src/PageStorageMigration2024Helper.php index 053581be..8f7e19cf 100644 --- a/custom/modules/newspapers_core/src/PageStorageMigration2024Helper.php +++ b/custom/modules/newspapers_core/src/PageStorageMigration2024Helper.php @@ -14,7 +14,7 @@ */ class PageStorageMigration2024Helper { - const BASE_STORAGE_PATH = '/app/html/sites/default/files/serials/pages'; + const BASE_PAGE_STORAGE_PATH = '/serials/pages'; // 18237 // rm -rf /app/html/sites/default/files/serials/pages/* @@ -37,8 +37,9 @@ public static function bulkCreateNewStoragePaths() { $progress = 0; foreach ($issue_ids as $issue_id) { // First, unlink the old storage location. - if (file_exists(self::BASE_STORAGE_PATH . "/$issue_id")) { - unlink(self::BASE_STORAGE_PATH . "/$issue_id"); + $old_storage_location = DRUPAL_ROOT . self::BASE_PAGE_STORAGE_PATH . "/$issue_id"; + if (file_exists($old_storage_location)) { + unlink($old_storage_location); } $issue = \Drupal::entityTypeManager() @@ -47,7 +48,7 @@ public static function bulkCreateNewStoragePaths() { $title_id = $issue->getParentTitleId(); $progress += 1; echo "Creating issue $progress/$count...\n"; - $issue_absolute_path = self::BASE_STORAGE_PATH . "/$title_id/$issue_id"; + $issue_absolute_path = DRUPAL_ROOT . self::BASE_PAGE_STORAGE_PATH . "/$title_id/$issue_id"; if (!file_exists($issue_absolute_path)) { mkdir($issue_absolute_path, 0755, TRUE); } @@ -105,8 +106,8 @@ public static function moveDziTileLocation($page, $issue) { $image_file_name = $file->getFilename(); $file_name = str_replace('.jpg', '.dzi', $image_file_name); - $old_page_absolute_file_location = self::BASE_STORAGE_PATH . "/$file_name"; - $new_page_absolute_file_location = self::BASE_STORAGE_PATH . "/$title_id/$issue_id/$file_name"; + $old_page_absolute_file_location = DRUPAL_ROOT . self::BASE_PAGE_STORAGE_PATH . "/$file_name"; + $new_page_absolute_file_location = DRUPAL_ROOT . self::BASE_PAGE_STORAGE_PATH . "/$title_id/$issue_id/$file_name"; $old_dzi_file = str_replace('.jpg', '.dzi', $old_page_absolute_file_location); $old_dzi_asset_path = str_replace('.dzi', '_files', $old_dzi_file); $new_dzi_file = str_replace('.jpg', '.dzi', $new_page_absolute_file_location); @@ -134,8 +135,8 @@ public static function movePdfFileLocation($page, $issue) { $issue_id = $issue->id(); $title_id = $issue->getParentTitleId(); $file_name = $file->getFilename(); - $old_page_absolute_file_location = self::BASE_STORAGE_PATH . "/$file_name"; - $new_page_absolute_file_location = self::BASE_STORAGE_PATH . "/$title_id/$issue_id/$file_name"; + $old_page_absolute_file_location = DRUPAL_ROOT . self::BASE_PAGE_STORAGE_PATH . "/$file_name"; + $new_page_absolute_file_location = DRUPAL_ROOT . self::BASE_PAGE_STORAGE_PATH . "/$title_id/$issue_id/$file_name"; $old_pdf_file_path = str_replace( '/pages/',