Skip to content

Commit

Permalink
NBNP-448 Remove reliance on drupal filesystem for untracked
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobSanford committed Jun 5, 2024
1 parent 04497e1 commit 2980c23
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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}
*/
Expand All @@ -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}
*/
Expand All @@ -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 '';
}

}
16 changes: 1 addition & 15 deletions custom/modules/newspapers_core/newspapers_core.module
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down

0 comments on commit 2980c23

Please sign in to comment.