def map(item): if item.deleted: return fs = fs_domain.AbstractFileSystem( fs_domain.GcsFileSystem(feconf.ENTITY_TYPE_EXPLORATION, item.id)) filepaths = fs.listdir('image') for filepath in filepaths: filename = filepath.split('/')[-1] if not re.match(constants.MATH_SVG_FILENAME_REGEX, filename): continue old_svg_image = fs.get(filepath) xmlns_attribute_is_present = ( html_validation_service.does_svg_tag_contains_xmlns_attribute( old_svg_image)) if not xmlns_attribute_is_present: yield (item.id, filename)
def validate_image_and_filename(raw_image, filename): """Validates the image data and its filename. Args: raw_image: str. The image content. filename: str. The filename for the image. Returns: str. The file format of the image. Raises: ValidationError. Image or filename supplied fails one of the validation checks. """ hundred_kb_in_bytes = 100 * 1024 if not raw_image: raise utils.ValidationError('No image supplied') if len(raw_image) > hundred_kb_in_bytes: raise utils.ValidationError('Image exceeds file size limit of 100 KB.') allowed_formats = ', '.join( list(feconf.ACCEPTED_IMAGE_FORMATS_AND_EXTENSIONS.keys())) if html_validation_service.is_parsable_as_xml(raw_image): file_format = 'svg' invalid_tags, invalid_attrs = ( html_validation_service.get_invalid_svg_tags_and_attrs(raw_image)) if invalid_tags or invalid_attrs: invalid_tags_message = ('tags: %s' % invalid_tags if invalid_tags else '') invalid_attrs_message = ('attributes: %s' % invalid_attrs if invalid_attrs else '') raise utils.ValidationError( 'Unsupported tags/attributes found in the SVG:\n%s\n%s' % (invalid_tags_message, invalid_attrs_message)) if not html_validation_service.does_svg_tag_contains_xmlns_attribute( raw_image): raise utils.ValidationError( 'The svg tag does not contains the \'xmlns\' attribute.') else: # Verify that the data is recognized as an image. file_format = imghdr.what(None, h=raw_image) if file_format not in feconf.ACCEPTED_IMAGE_FORMATS_AND_EXTENSIONS: raise utils.ValidationError('Image not recognized') # Verify that the file type matches the supplied extension. if not filename: raise utils.ValidationError('No filename supplied') if filename.rfind('.') == 0: raise utils.ValidationError('Invalid filename') if '/' in filename or '..' in filename: raise utils.ValidationError( 'Filenames should not include slashes (/) or consecutive ' 'dot characters.') if '.' not in filename: raise utils.ValidationError( 'Image filename with no extension: it should have ' 'one of the following extensions: %s.' % allowed_formats) dot_index = filename.rfind('.') extension = filename[dot_index + 1:].lower() if (extension not in feconf.ACCEPTED_IMAGE_FORMATS_AND_EXTENSIONS[file_format]): raise utils.ValidationError( 'Expected a filename ending in .%s, received %s' % (file_format, filename)) return file_format