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

Disallow media extensions unregistered with IANA #3954

Merged
merged 6 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions scrapy/pipelines/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
import functools
import hashlib
import mimetypes
import os
import os.path
import time
Expand All @@ -14,6 +15,7 @@
from collections import defaultdict
import six


try:
from cStringIO import StringIO as BytesIO
except ImportError:
Expand Down Expand Up @@ -460,4 +462,11 @@ def item_completed(self, results, item, info):
def file_path(self, request, response=None, info=None):
media_guid = hashlib.sha1(to_bytes(request.url)).hexdigest()
media_ext = os.path.splitext(request.url)[1]
# Handles empty and wild extensions by trying to guess the
# mime type then extension or default to empty string otherwise
if media_ext not in mimetypes.types_map.keys():
OmarFarrag marked this conversation as resolved.
Show resolved Hide resolved
media_ext = ''
media_type = mimetypes.guess_type(request.url)[0]
if media_type:
media_ext = mimetypes.guess_extension(media_type)
OmarFarrag marked this conversation as resolved.
Show resolved Hide resolved
return 'full/%s%s' % (media_guid, media_ext)
6 changes: 6 additions & 0 deletions tests/test_pipeline_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ def test_file_path(self):
response=Response("http://www.dorma.co.uk/images/product_details/2532"),
info=object()),
'full/244e0dd7d96a3b7b01f54eded250c9e272577aa1')
self.assertEqual(file_path(Request("http://www.dfsonline.co.uk/get_prod_image.php?img=status_0907_mdm.jpg.bohaha")),
'full/76c00cef2ef669ae65052661f68d451162829507')
self.assertEqual(file_path(Request("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAR0AAACxCAMAAADOHZloAAACClBMVEX/\
//+F0tzCwMK76ZKQ21AMqr7oAAC96JvD5aWM2kvZ78J0N7fmAAC46Y4Ap7y")),
'full/178059cbeba2e34120a67f2dc1afc3ecc09b61cb.png')


def test_fs_store(self):
assert isinstance(self.pipeline.store, FSFilesStore)
Expand Down