|
3 | 3 | import codecs
|
4 | 4 | import json
|
5 | 5 | import os
|
| 6 | +import logging |
6 | 7 |
|
| 8 | +from django.conf import settings as django_settings |
7 | 9 | from django.contrib.staticfiles.storage import staticfiles_storage
|
| 10 | +from django.core.exceptions import SuspiciousFileOperation |
8 | 11 |
|
9 | 12 | from pipeline.conf import settings
|
10 | 13 | from pipeline.compressors import SubProcessCompressor
|
11 | 14 | from pipeline.utils import source_map_re, relurl
|
12 | 15 |
|
13 | 16 |
|
| 17 | +logger = logging.getLogger('pipeline') |
| 18 | + |
| 19 | + |
14 | 20 | class CleanCSSCompressor(SubProcessCompressor):
|
15 | 21 |
|
16 | 22 | def compress_css(self, css):
|
@@ -56,10 +62,25 @@ def compress_css_with_source_map(self, paths, output_filename):
|
56 | 62 | # Grab urls from staticfiles storage (in case filenames are hashed)
|
57 | 63 | source_map_data = json.loads(source_map)
|
58 | 64 | for i, source in enumerate(source_map_data['sources']):
|
59 |
| - source_abs_path = os.path.join(output_dir, source) |
| 65 | + source_abs_path = os.path.abspath(os.path.join(output_dir, source)) |
60 | 66 | source_rel_path = os.path.relpath(
|
61 | 67 | source_abs_path, staticfiles_storage.base_location)
|
62 |
| - source_url = staticfiles_storage.url(source_rel_path) |
| 68 | + source_url = None |
| 69 | + try: |
| 70 | + source_url = staticfiles_storage.url(source_rel_path) |
| 71 | + except SuspiciousFileOperation: |
| 72 | + for static_dir in django_settings.STATICFILES_DIRS: |
| 73 | + if not isinstance(static_dir, tuple): |
| 74 | + continue |
| 75 | + prefix, path = static_dir |
| 76 | + if not source_abs_path.startswith(path): |
| 77 | + continue |
| 78 | + source_rel_path = os.path.relpath(source_abs_path, path) |
| 79 | + source_url = staticfiles_storage.url( |
| 80 | + os.path.join(prefix, source_rel_path)) |
| 81 | + if not source_url: |
| 82 | + logger.exception("Could not relativize source map file") |
| 83 | + continue |
63 | 84 | source_map_data['sources'][i] = relurl(source_url, output_url)
|
64 | 85 | source_map = json.dumps(source_map_data)
|
65 | 86 |
|
|
0 commit comments