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

Add support for a list of allowed mime-types instead of only one #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gabsoftware
Copy link

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.

Copy link
Owner

@migueldemoura migueldemoura left a 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";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
!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" )));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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" )));
Copy link
Owner

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' ]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'screenshotMimeTypes' => [ 'image/png', 'image/jpeg' ]
'screenshotMimeTypes' => ['image/png', 'image/jpeg']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants