-
Notifications
You must be signed in to change notification settings - Fork 18
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
Add support for a list of allowed mime-types instead of only one #50
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a bunch for the PR, @gabsoftware! Just left a few comments, let me know what you think.
I'll tag afterwards and add a note about the change of screenshotMimeType
-> screenshotMimeTypes
@@ -20,21 +20,22 @@ | |||
exit(); | |||
} | |||
|
|||
$mimetype = isset($screenshot['tmp_name']) ? mime_content_type($screenshot['tmp_name']) : "error"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$mimetype = isset($screenshot['tmp_name']) ? mime_content_type($screenshot['tmp_name']) : "error"; | |
$mimetype = isset($screenshot['tmp_name']) ? mime_content_type($screenshot['tmp_name']) : 'error'; |
if ( | ||
// Make sure it is a successful single file upload | ||
!isset($screenshot['error']) || | ||
is_array($screenshot['error']) || | ||
$screenshot['error'] !== UPLOAD_ERR_OK || | ||
// Verify size and mime type | ||
$screenshot['size'] > $config['maxScreenshotSize'] || | ||
mime_content_type($screenshot['tmp_name']) !== $config['screenshotMimeType'] | ||
!in_array( $mimetype, $config['screenshotMimeTypes'], true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!in_array( $mimetype, $config['screenshotMimeTypes'], true) | |
!in_array($mimetype, $config['screenshotMimeTypes'], true) |
) { | ||
http_response_code(422); | ||
exit(); | ||
} | ||
|
||
// Generate screenshot path | ||
do {} while (file_exists($filename = bin2hex(random_bytes(12)) . '.png')); | ||
do {} while (file_exists($filename = bin2hex(random_bytes(12)) . '.' . ($mimetype == "image/png" ? "png" : "jpg" ))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do {} while (file_exists($filename = bin2hex(random_bytes(12)) . '.' . ($mimetype == "image/png" ? "png" : "jpg" ))); | |
do {} while (file_exists($filename = bin2hex(random_bytes(12)) . '.' . ($mimetype == 'image/png' ? 'png' : 'jpg' ))); |
) { | ||
http_response_code(422); | ||
exit(); | ||
} | ||
|
||
// Generate screenshot path | ||
do {} while (file_exists($filename = bin2hex(random_bytes(12)) . '.png')); | ||
do {} while (file_exists($filename = bin2hex(random_bytes(12)) . '.' . ($mimetype == "image/png" ? "png" : "jpg" ))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If someone adds other mimetypes besides these 2, I guess we can't really have this assumption of using .jpg
here.
@@ -4,5 +4,5 @@ return [ | |||
'secretBcrypt' => '', | |||
'saveDirName' => '/data/', | |||
'maxScreenshotSize' => 2 * 1048576, | |||
'screenshotMimeType' => 'image/png' | |||
'screenshotMimeTypes' => [ 'image/png', 'image/jpeg' ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'screenshotMimeTypes' => [ 'image/png', 'image/jpeg' ] | |
'screenshotMimeTypes' => ['image/png', 'image/jpeg'] |
Applications such as ShareX will save to PNG below a certain file size (usually 2 MB) then to JPEG, so it would be useful to allow a list of mime-types instead of only one.
This pull-request enables that.
By default it allows image/png and image/jpeg.