def test_convert_png_data_url_to_binary(self) -> None: image_data_url = '%s%s' % ( utils.PNG_DATA_URL_PREFIX, python_utils.url_quote(base64.b64encode(b'test123'))) # type: ignore[no-untyped-call] self.assertEqual( utils.convert_png_data_url_to_binary(image_data_url), b'test123')
def test_convert_png_data_url_to_binary_raises_if_prefix_is_missing(self): # type: () -> None image_data_url = python_utils.url_quote(base64.b64encode('test123')) # type: ignore[no-untyped-call] self.assertRaisesRegexp( # type: ignore[no-untyped-call] Exception, 'The given string does not represent a PNG data URL.', lambda: utils.convert_png_data_url_to_binary(image_data_url))
def map(model): # pylint: disable=too-many-return-statements if model.deleted: yield ('SUCCESS - DELETED', model.username) return if model.username is None: yield ('SUCCESS - NOT REGISTERED', model.username) return if model.profile_picture_data_url is None: yield ('FAILURE - MISSING PROFILE PICTURE', model.username) return try: profile_picture_binary = utils.convert_png_data_url_to_binary( model.profile_picture_data_url) except Exception: yield ('FAILURE - INVALID PROFILE PICTURE DATA URL', model.username) return if imghdr.what(None, h=profile_picture_binary) != 'png': yield ('FAILURE - PROFILE PICTURE NOT PNG', model.username) return try: # Load the image to retrieve dimensions for later verification. height, width = image_services.get_image_dimensions( profile_picture_binary) except Exception: yield ('FAILURE - CANNOT LOAD PROFILE PICTURE', model.username) return if ( height != user_services.GRAVATAR_SIZE_PX or width != user_services.GRAVATAR_SIZE_PX ): yield ( 'FAILURE - PROFILE PICTURE NON STANDARD DIMENSIONS - %s,%s' % ( height, width ), model.username ) return yield ('SUCCESS', model.username)
def get(self): """Handles GET requests.""" if not constants.ENABLE_ACCOUNT_EXPORT: raise self.PageNotFoundException # Retrieve user data. user_takeout_object = takeout_service.export_data_for_user( self.user_id) user_data = user_takeout_object.user_data user_images = user_takeout_object.user_images # Ensure that the exported data does not contain a user ID. user_data_json_string = json.dumps(user_data) if re.search(feconf.USER_ID_REGEX, user_data_json_string): logging.exception( '[TAKEOUT] User ID found in the JSON generated for user %s' % self.user_id) user_data_json_string = ( 'There was an error while exporting ' + 'data. Please contact %s to export your data.' % feconf.ADMIN_EMAIL_ADDRESS) user_images = [] # Create zip file. temp_file = io.BytesIO() with zipfile.ZipFile(temp_file, mode='w', compression=zipfile.ZIP_DEFLATED) as zfile: zfile.writestr('oppia_takeout_data.json', user_data_json_string) for image in user_images: decoded_png = utils.convert_png_data_url_to_binary( image.b64_image_data) zfile.writestr('images/' + image.image_export_path, decoded_png) # Render file for download. self.render_downloadable_file(temp_file, 'oppia_takeout_data.zip', 'text/plain')
def test_convert_png_data_url_to_binary_with_incorrect_prefix( self) -> None: with self.assertRaisesRegexp( # type: ignore[no-untyped-call] Exception, 'The given string does not represent a PNG data URL'): utils.convert_png_data_url_to_binary('data:image/jpg;base64,')