commit
763c3690dd
5 changed files with 145 additions and 0 deletions
@ -0,0 +1,7 @@ |
|||||||
|
name: 'HOCR Media Sanitizer' |
||||||
|
type: module |
||||||
|
description: Updates HOCR media' |
||||||
|
package: Custom |
||||||
|
core_version_requirement: ^10 || ^11 |
||||||
|
dependencies: |
||||||
|
- islandora:islandora |
||||||
@ -0,0 +1,15 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
/** |
||||||
|
* @file |
||||||
|
* hocr_sanitizer.module |
||||||
|
*/ |
||||||
|
|
||||||
|
/** |
||||||
|
* Implements hook_help(). |
||||||
|
*/ |
||||||
|
function hocr_sanitizer_help($route_name, $route_match) { |
||||||
|
if ($route_name == 'help.page.hocr_sanitizer') { |
||||||
|
return t('The HOCR Sanitizer module batch-renames managed files ending in _HOCR.txt to .html and updates their MIME type to text/html.'); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,7 @@ |
|||||||
|
hocr_sanitizer.admin: |
||||||
|
path: '/admin/config/media/hocr-sanitizer' |
||||||
|
defaults: |
||||||
|
_form: '\Drupal\hocr_sanitizer\Form\HocrSanitizerForm' |
||||||
|
_title: 'HOCR Sanitizer' |
||||||
|
requirements: |
||||||
|
_permission: 'administer site configuration' |
||||||
@ -0,0 +1,59 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\hocr_sanitizer\Form; |
||||||
|
|
||||||
|
use Drupal\Core\Form\FormBase; |
||||||
|
use Drupal\Core\Form\FormStateInterface; |
||||||
|
use Drupal\Core\Batch\BatchBuilder; |
||||||
|
use Drupal\hocr_sanitizer\Service\HocrSanitizerBatch; |
||||||
|
|
||||||
|
class HocrSanitizerForm extends FormBase { |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function getFormId() { |
||||||
|
return 'hocr_sanitizer_admin_form'; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function buildForm(array $form, FormStateInterface $form_state) { |
||||||
|
$form['description'] = [ |
||||||
|
'#markup' => $this->t('This will rename all files ending in <code>_HOCR.txt</code> to <code>.html</code> and update their MIME type to <code>text/html</code>.'), |
||||||
|
]; |
||||||
|
|
||||||
|
$form['submit'] = [ |
||||||
|
'#type' => 'submit', |
||||||
|
'#value' => $this->t('Run HOCR Sanitizer Batch'), |
||||||
|
]; |
||||||
|
|
||||||
|
return $form; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function submitForm(array &$form, FormStateInterface $form_state) { |
||||||
|
$query = \Drupal::entityQuery('file') |
||||||
|
->condition('uri', '%_HOCR.txt', 'LIKE') |
||||||
|
->accessCheck(FALSE) |
||||||
|
->condition('filemime', 'text/plain'); |
||||||
|
$fids = $query->execute(); |
||||||
|
|
||||||
|
$batch_builder = (new BatchBuilder()) |
||||||
|
->setTitle($this->t('Renaming HOCR text files to HTML...')) |
||||||
|
->setInitMessage($this->t('Starting HOCR rename...')) |
||||||
|
->setProgressMessage($this->t('Renaming file @current of @total...')) |
||||||
|
->setErrorMessage($this->t('An error occurred during file renaming.')); |
||||||
|
|
||||||
|
foreach ($fids as $fid) { |
||||||
|
$batch_builder->addOperation([HocrSanitizerBatch::class, 'renameFile'], [$fid]); |
||||||
|
} |
||||||
|
|
||||||
|
$batch_builder->setFinishCallback([HocrSanitizerBatch::class, 'batchFinished']); |
||||||
|
|
||||||
|
batch_set($batch_builder->toArray()); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,57 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\hocr_sanitizer\Service; |
||||||
|
|
||||||
|
use Drupal\file\Entity\File; |
||||||
|
use Drupal\Core\File\FileExists; |
||||||
|
|
||||||
|
class HocrSanitizerBatch { |
||||||
|
|
||||||
|
/** |
||||||
|
* Batch operation: rename one HOCR file. |
||||||
|
*/ |
||||||
|
public static function renameFile($fid, array &$context) { |
||||||
|
$file = File::load($fid); |
||||||
|
if (!$file) { |
||||||
|
$context['results']['errors'][] = "File with fid $fid not found."; |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
$old_uri = $file->getFileUri(); |
||||||
|
$dirname = dirname($old_uri); |
||||||
|
$old_filename = basename($old_uri); |
||||||
|
|
||||||
|
if (!preg_match('/_HOCR\.txt$/i', $old_filename)) { |
||||||
|
return; // Skip if no match. |
||||||
|
} |
||||||
|
|
||||||
|
$new_filename = preg_replace('/_HOCR\.txt$/i', '_HOCR.html', $old_filename); |
||||||
|
$new_uri = $dirname . '/' . $new_filename; |
||||||
|
$file_system = \Drupal::service('file_system'); |
||||||
|
|
||||||
|
try { |
||||||
|
$file_system->move($old_uri, $new_uri, FileExists::Rename); |
||||||
|
$file->setFileUri($new_uri); |
||||||
|
$file->setMimeType('text/vnd.hocr+html'); |
||||||
|
$file->setFilename($new_filename); |
||||||
|
$file->save(); |
||||||
|
|
||||||
|
$context['results']['renamed'][] = $new_filename; |
||||||
|
} |
||||||
|
catch (\Exception $e) { |
||||||
|
$context['results']['errors'][] = "Failed to rename $old_filename: " . $e->getMessage(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Batch finished callback. |
||||||
|
*/ |
||||||
|
public static function batchFinished($success, $results, $operations) { |
||||||
|
if (!empty($results['renamed'])) { |
||||||
|
\Drupal::messenger()->addStatus(t('Renamed @count files.', ['@count' => count($results['renamed'])])); |
||||||
|
} |
||||||
|
if (!empty($results['errors'])) { |
||||||
|
\Drupal::messenger()->addWarning(t('Some errors occurred: @count issues.', ['@count' => count($results['errors'])])); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue