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

Check uploaded SVG files validity #3359

Open
marcantoinedupre opened this issue Dec 15, 2022 · 0 comments
Open

Check uploaded SVG files validity #3359

marcantoinedupre opened this issue Dec 15, 2022 · 0 comments

Comments

@marcantoinedupre
Copy link
Contributor

We have seen a lot of errors in the past few months due to "bad" SVG files which caused errors deep during our media management commands/tasks. For instance on executing the admin sync_mobile command:

IsADirectoryError: [Errno 21] Is a directory: '/opt/geotrek-admin'
  File "urllib/request.py", line 1526, in open_local_file
    return addinfourl(open(localfile, 'rb'), headers, origurl)

URLError: <urlopen error [Errno 21] Is a directory: '/opt/geotrek-admin'>
  File "manage.py", line 20, in <module>
    execute_from_command_line(sys.argv)
  File "django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "geotrek/api/management/commands/sync_mobile.py", line 519, in handle
    self.sync()
  File "geotrek/api/management/commands/sync_mobile.py", line 431, in sync
    self.sync_medias()
  File "geotrek/api/management/commands/sync_mobile.py", line 284, in sync_medias
    self.sync_global_media()
  File "geotrek/api/management/commands/sync_mobile.py", line 359, in sync_global_media
    self.sync_pictograms(trekking_models.Practice, directory=url_media_nolang, zipfile=self.zipfile_settings,
  File "geotrek/api/management/commands/sync_mobile.py", line 193, in sync_pictograms
    cairosvg.svg2png(url=obj.pictogram.path, write_to=dst)
  File "__init__.py", line 55, in svg2png
    return surface.PNGSurface.convert(
  File "cairosvg/surface.py", line 131, in convert
    instance = cls(
  File "cairosvg/surface.py", line 223, in __init__
    self.draw(tree)
  File "cairosvg/surface.py", line 469, in draw
    self.draw(child)
  File "cairosvg/surface.py", line 469, in draw
    self.draw(child)
  File "cairosvg/surface.py", line 469, in draw
    self.draw(child)
  File "cairosvg/surface.py", line 401, in draw
    TAGS[node.tag](self, node)
  File "cairosvg/image.py", line 28, in image
    image_bytes = node.fetch_url(url, 'image/*')
  File "cairosvg/parser.py", line 243, in fetch_url
    return read_url(url, self.url_fetcher, resource_type)
  File "cairosvg/url.py", line 143, in read_url
    return url_fetcher(url, resource_type)
  File "cairosvg/url.py", line 84, in fetch
    return urlopen(Request(url, headers=HTTP_HEADERS)).read()
  File "urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "urllib/request.py", line 542, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "urllib/request.py", line 502, in _call_chain
    result = func(*args)
  File "urllib/request.py", line 1489, in file_open
    return self.open_local_file(req)
  File "urllib/request.py", line 1528, in open_local_file
    raise URLError(exp)

Here it failed at cairosvg.svg2png(url=obj.pictogram.path, write_to=dst) because cairosvg tries to reach for an external resource defined in the svg file. For instance it could be an image like so:

<g id="_x31_VIvLo.tif">
	<image style="overflow:visible;" width="136" height="111" id="Layer_0_1_" transform="matrix(0.2108 0 0 0.2108 1.6667 4.3015></image>
</g>

This trouble goes unnoticed at first because SVG editor/viewer applications may silently ignore this kind of missing resource issue.

It would be good to check SVG files at updload time for compatibility with Geotrek usages. It would save time for everyone, users and admin staff. We could warn the user that the svg file is invalid or try to auto-correct it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Backlog
Development

No branches or pull requests

2 participants