Skip to content

Commit 39c27da

Browse files
committed
[REFACTOR] update filterFilesByMimeType for better custom dlf-mimetype use
1 parent d119b5a commit 39c27da

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

Classes/Common/Helper.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -952,14 +952,15 @@ private static function getLocalConfigurationByPath(string $path)
952952
*
953953
* @param mixed $file The file array to filter
954954
* @param array $allowedCategories The allowed MIME type categories to filter by (e.g., ['audio'], ['video'] or ['image', 'application'])
955-
* @param array|null $dlfMimeTypes Optional array of custom DLF mimetype keys to filter by. Default is null.
956-
* Pass null to use all available custom DLF mimetype keys
957-
* Pass array of keys to use only specific types - Accepted values: 'IIIF', 'IIP', 'ZOOMIFY', 'JPG'
955+
* @param null|bool|array $dlfMimeTypes Optional array of custom dlf mimetype keys to filter by. Default is null.
956+
* - null: use no custom dlf mimetypes
957+
* - true: use all custom dlf mimetypes
958+
* - array: use only specific types - Accepted values: 'IIIF', 'IIP', 'ZOOMIFY', 'JPG'
958959
* @param string $mimeTypeKey The key used to access the mimetype in the file array (default is 'mimetype')
959960
*
960961
* @return bool True if the file mimetype belongs to any of the allowed mimetypes or matches any custom dlf mimetypes, false otherwise
961962
*/
962-
public static function filterFilesByMimeType($file, array $allowedCategories, ?array $dlfMimeTypes = null, string $mimeTypeKey = 'mimetype'): bool
963+
public static function filterFilesByMimeType($file, array $allowedCategories, null|bool|array $dlfMimeTypes = null, string $mimeTypeKey = 'mimetype'): bool
963964
{
964965
if (empty($allowedCategories) && empty($dlfMimeTypes)) {
965966
return false;
@@ -988,9 +989,12 @@ function ($mimeType) use ($allowedCategories) {
988989
];
989990

990991
// Apply filtering to the custom dlf MIME type array
991-
$filteredDlfMimeTypes = $dlfMimeTypes === null
992-
? $dlfMimeTypeArray
993-
: array_intersect_key($dlfMimeTypeArray, array_flip($dlfMimeTypes));
992+
$filteredDlfMimeTypes = match(true) {
993+
$dlfMimeTypes === null => [],
994+
$dlfMimeTypes === true => $dlfMimeTypeArray,
995+
is_array($dlfMimeTypes) => array_intersect_key($dlfMimeTypeArray, array_flip($dlfMimeTypes)),
996+
default => []
997+
};
994998

995999
// Actual filtering to check if the file's MIME type is allowed
9961000
if (is_array($file) && isset($file[$mimeTypeKey])) {

Classes/Controller/PageViewController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ protected function getImage(int $page, MetsDocument $specificDoc = null): array
637637
// Get file info for the specific page and file group
638638
$file = $this->fetchFileInfo($page, $fileGrpImages, $specificDoc);
639639

640-
if ($file && Helper::filterFilesByMimeType($file, ['image'], null, 'mimeType')) {
640+
if ($file && Helper::filterFilesByMimeType($file, ['image'], true, 'mimeType')) {
641641
$image['url'] = $file['location'];
642642
$image['mimetype'] = $file['mimeType'];
643643

Classes/Controller/ToolboxController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,13 @@ private function renderImageDownloadTool(): void
283283
$imageArray = [];
284284
// Get left or single page download.
285285
$image = $this->getImage($page);
286-
if (Helper::filterFilesByMimeType($image, ['image'])) {
286+
if (Helper::filterFilesByMimeType($image, ['image'], true)) {
287287
$imageArray[0] = $image;
288288
}
289289

290290
if ($this->requestData['double'] == 1) {
291291
$image = $this->getImage($page + 1);
292-
if (Helper::filterFilesByMimeType($image, ['image'])) {
292+
if (Helper::filterFilesByMimeType($image, ['image'], true)) {
293293
$imageArray[1] = $image;
294294
}
295295
}

0 commit comments

Comments
 (0)