Skip to content

Commit

Permalink
NBNP-448 Avoid using URIs for paths and files that may not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobSanford committed Jun 18, 2024
1 parent 9251623 commit 6f865cc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand All @@ -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()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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/*
Expand All @@ -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()
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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/',
Expand Down

0 comments on commit 6f865cc

Please sign in to comment.