Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add MIME type filter to ensure only supported image files are downloaded #1319

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions Classes/Controller/ToolboxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,37 @@ public function renderScoreTool()
}
}

/**
* List of common web image mimetypes
* The MIMETYPE attribute must specify the media type of the digital representation. All web-compatible formats as per RFC2046 are allowed.
*/
private const IMAGE_MIMETYPES = [
"image/jpeg",
"image/jpg",
"image/png",
"image/gif",
"image/bmp",
"image/tiff",
"image/x-tiff",
"image/webp",
"image/svg+xml",
"image/vnd.microsoft.icon",
"image/x-icon",
"image/heif",
"image/heic",
"image/vnd.adobe.photoshop",
"image/x-xbitmap",
"image/x-xpixmap",
"image/jp2",
"image/jpx",
"image/jpm",
"image/mj2",
"image/x-portable-anymap",
"image/x-portable-bitmap",
"image/x-portable-graymap",
"image/x-portable-pixmap"
];

/**
* Renders the image download tool
* Renders the image download tool (used in template)
Expand All @@ -291,14 +322,39 @@ private function renderImageDownloadTool(): void

$imageArray = [];
// Get left or single page download.
$imageArray[0] = $this->getImage($this->requestData['page']);
$image = $this->getImage($this->requestData['page']);
if ($this->filterImageFiles($image)) {
$imageArray[0] = $image;
}

if ($this->requestData['double'] == 1) {
$imageArray[1] = $this->getImage($this->requestData['page'] + 1);
$image = $this->getImage($this->requestData['page'] + 1);
if ($this->filterImageFiles($image)) {
$imageArray[1] = $image;
}
}

$this->view->assign('imageDownload', $imageArray);
}

/**
* Filters an image file based on its mimetype.
*
* This method checks if the provided image array contains a 'mimetype' key and
* verifies if the mimetype is one of the supported image types defined in the class constant IMAGE_MIMETYPES.
*
* @param mixed $image The image array to filter
*
* @return bool True if the image mimetype is supported, false otherwise
*/
private function filterImageFiles($image): bool
{
if (is_array($image) && isset($image['mimetype'])) {
return in_array($image['mimetype'], self::IMAGE_MIMETYPES);
}
return false;
}

/**
* Get file's URL and MIME type
*
Expand Down