def _inject_content_type(self, params): if not self.parameters['guess_mime_type']: return filename = self.src # Add a content type param if we can guess the type. guessed_type = guess_content_type(filename) if guessed_type is not None: params['ContentType'] = guessed_type
def _inject_content_type(self, params, filename): # Add a content type param if we can guess the type. try: guessed_type = guess_content_type(filename) if guessed_type is not None: params['ContentType'] = guessed_type # This catches a bug in the mimetype libary where some MIME types # specifically on windows machines cause a UnicodeDecodeError # because the MIME type in the Windows registery has an encoding # that cannot be properly encoded using the default system encoding. # https://bugs.python.org/issue9291 # # So instead of hard failing, just log the issue and fall back to the # default guessed content type of None. except UnicodeDecodeError: LOGGER.debug( 'Unable to guess content type for %s due to ' 'UnicodeDecodeError: ', filename, exc_info=True )
def _inject_content_type(self, params, filename): # Add a content type param if we can guess the type. try: guessed_type = guess_content_type(filename) if guessed_type is not None: params['ContentType'] = guessed_type # This catches a bug in the mimetype libary where some MIME types # specifically on windows machines cause a UnicodeDecodeError # because the MIME type in the Windows registery has an encoding # that cannot be properly encoded using the default system encoding. # https://bugs.python.org/issue9291 # # So instead of hard failing, just log the issue and fall back to the # default guessed content type of None. except UnicodeDecodeError: LOGGER.debug( 'Unable to guess content type for %s due to ' 'UnicodeDecodeError: ', filename, exc_info=True)
def on_queued(self, future, **kwargs): guessed_type = utils.guess_content_type(self._get_filename(future)) if guessed_type is not None: future.meta.call_args.extra_args['ContentType'] = guessed_type
def test_guess_content_type_with_unicode_error_returns_no_match(self): with mock.patch("mimetypes.guess_type") as guess_type_patch: # This should throw a UnicodeDecodeError. guess_type_patch.side_effect = lambda x: b"\xe2".decode("ascii") self.assertEqual(guess_content_type("foo.txt"), None)
def test_guess_content_type_with_no_valid_matches(self): self.assertEqual(guess_content_type("no-extension"), None)
def test_guess_content_type(self): self.assertEqual(guess_content_type("foo.txt"), "text/plain")
def test_guess_content_type_with_unicode_error_returns_no_match(self): with mock.patch('mimetypes.guess_type') as guess_type_patch: # This should throw a UnicodeDecodeError. guess_type_patch.side_effect = lambda x: b'\xe2'.decode('ascii') self.assertEqual(guess_content_type('foo.txt'), None)
def test_guess_content_type_with_no_valid_matches(self): self.assertEqual(guess_content_type('no-extension'), None)
def test_guess_content_type(self): self.assertEqual(guess_content_type('foo.txt'), 'text/plain')
def _inject_content_type(self, params, filename): # Add a content type param if we can guess the type. guessed_type = guess_content_type(filename) if guessed_type is not None: params['ContentType'] = guessed_type