-
-
Notifications
You must be signed in to change notification settings - Fork 2
[fix] Fixed broken settings and issues with drf-yasg #9
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
Changes from all commits
f8a1a0e
6d54ec6
88782d8
ed8d45f
41c9599
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ | |
| from django.core.files.base import ContentFile | ||
| from django.utils.deconstruct import deconstructible | ||
|
|
||
| from .conf import DEFAULT_SETTINGS, get_setting | ||
| from .conf import get_setting, validate_settings | ||
| from .utils import FileManager, is_safe_path | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
@@ -23,11 +23,12 @@ class FileProcessorMixin: | |
|
|
||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
| validate_settings() | ||
| self.file_manager = FileManager(self) | ||
|
|
||
| def should_process_minification(self, path): | ||
| """Check if file should be minified.""" | ||
| if not get_setting("MINIFY_FILES", DEFAULT_SETTINGS["MINIFY_FILES"]): | ||
| if not get_setting("MINIFY_FILES"): | ||
| return False | ||
| if not self.file_manager.should_process(path): | ||
| return False | ||
|
|
@@ -39,11 +40,8 @@ def should_process_compression(self, path, allow_min=False): | |
| return False | ||
| if allow_min: | ||
| # When allowing min files, just check extension | ||
| ext = Path(path).suffix.lower() | ||
| supported = self.file_manager.supported_extensions | ||
| if isinstance(supported, dict): | ||
| supported = list(supported.keys()) | ||
| if ext.lstrip(".") not in supported: | ||
| ext = Path(path).suffix.lower().lstrip(".") | ||
| if ext not in self.file_manager.processable_extensions: | ||
| return False | ||
| return self.file_manager.is_compression_candidate(path) | ||
|
|
||
|
|
@@ -55,28 +53,18 @@ def minify_file_content(self, content, file_type): | |
| """Minify file content based on type.""" | ||
| if file_type == "css" and rcssmin: | ||
| try: | ||
| preserve_comments = get_setting( | ||
| "PRESERVE_COMMENTS", DEFAULT_SETTINGS["PRESERVE_COMMENTS"] | ||
| ) | ||
| if preserve_comments is None: | ||
| preserve_comments = True | ||
| return rcssmin.cssmin( | ||
| content, | ||
| keep_bang_comments=bool(preserve_comments), | ||
| keep_bang_comments=bool(get_setting("PRESERVE_COMMENTS")), | ||
| ) | ||
| except Exception as e: | ||
| logger.error(f"CSS minification failed for {file_type}: {e}") | ||
| return content | ||
| elif file_type == "js" and rjsmin: | ||
| try: | ||
| preserve_comments = get_setting( | ||
| "PRESERVE_COMMENTS", DEFAULT_SETTINGS["PRESERVE_COMMENTS"] | ||
| ) | ||
| if preserve_comments is None: | ||
| preserve_comments = True | ||
| return rjsmin.jsmin( | ||
| content, | ||
| keep_bang_comments=bool(preserve_comments), | ||
| keep_bang_comments=bool(get_setting("PRESERVE_COMMENTS")), | ||
| ) | ||
| except Exception as e: | ||
| logger.error(f"JS minification failed: {e}") | ||
|
|
@@ -89,13 +77,12 @@ class MinificationMixin(FileProcessorMixin): | |
|
|
||
| def process_minification(self, paths): | ||
| """Process minification for given paths.""" | ||
| if not get_setting("MINIFY_FILES", DEFAULT_SETTINGS["MINIFY_FILES"]): | ||
| if not get_setting("ENABLED"): | ||
| return {} | ||
| if not get_setting("MINIFY_FILES"): | ||
| return {} | ||
| minified_files = {} | ||
| max_files = ( | ||
| get_setting("MAX_FILES_PER_RUN", DEFAULT_SETTINGS["MAX_FILES_PER_RUN"]) | ||
| or 1000 | ||
| ) | ||
| max_files = get_setting("MAX_FILES_PER_RUN") | ||
| processed_count = 0 | ||
|
|
||
| for path in paths: | ||
|
|
@@ -114,6 +101,7 @@ def process_minification(self, paths): | |
| content = content.decode("utf-8") | ||
| except UnicodeDecodeError: | ||
| continue | ||
| processed_count += 1 | ||
| file_type = self._get_file_type(path) | ||
| minified_content = self.minify_file_content(content, file_type) | ||
| # Only save if minification reduced size | ||
|
|
@@ -131,10 +119,8 @@ def process_minification(self, paths): | |
| minified_path = str(parent / minified_filename) | ||
| else: | ||
| minified_path = minified_filename | ||
| # Save minified content | ||
| self._write_file_content(minified_path, minified_content) | ||
| minified_files[path] = minified_path | ||
| processed_count += 1 | ||
| except Exception as e: | ||
| logger.error(f"Failed to minify {path}: {e}") | ||
| continue | ||
|
|
@@ -146,16 +132,14 @@ class CompressionMixin(FileProcessorMixin): | |
|
|
||
| def process_compression(self, paths, allow_min=False): | ||
| """Process compression for given paths.""" | ||
| if not ( | ||
| get_setting("GZIP_COMPRESSION", DEFAULT_SETTINGS["GZIP_COMPRESSION"]) | ||
| or get_setting("BROTLI_COMPRESSION", DEFAULT_SETTINGS["BROTLI_COMPRESSION"]) | ||
| ): | ||
| if not get_setting("ENABLED"): | ||
| return {} | ||
| gzip_enabled = get_setting("GZIP_COMPRESSION") | ||
| brotli_enabled = get_setting("BROTLI_COMPRESSION") | ||
| if not (gzip_enabled or brotli_enabled): | ||
| return {} | ||
| compressed_files = {} | ||
| max_files = ( | ||
| get_setting("MAX_FILES_PER_RUN", DEFAULT_SETTINGS["MAX_FILES_PER_RUN"]) | ||
| or 1000 | ||
| ) | ||
| max_files = get_setting("MAX_FILES_PER_RUN") | ||
| processed_count = 0 | ||
|
|
||
| for path in paths: | ||
|
|
@@ -168,6 +152,7 @@ def process_compression(self, paths, allow_min=False): | |
| content = self._read_file_content(path) | ||
| if content is None: | ||
| continue | ||
| processed_count += 1 | ||
| # Get relative path for storage operations | ||
| # If path is absolute, convert to a relative path while preserving directory structure | ||
| if os.path.isabs(path): | ||
|
|
@@ -182,24 +167,19 @@ def process_compression(self, paths, allow_min=False): | |
| else: | ||
| relative_path = path | ||
| # Process Gzip compression | ||
| if get_setting( | ||
| "GZIP_COMPRESSION", DEFAULT_SETTINGS["GZIP_COMPRESSION"] | ||
| ): | ||
| if gzip_enabled: | ||
| gzipped_path = f"{relative_path}.gz" | ||
| gzipped_content = self.gzip_compress(content) | ||
| self._write_file_content( | ||
| gzipped_path, gzipped_content, is_text=False | ||
| ) | ||
| compressed_files.setdefault(path, []).append(gzipped_path) | ||
| # Process Brotli compression | ||
| if get_setting( | ||
| "BROTLI_COMPRESSION", DEFAULT_SETTINGS["BROTLI_COMPRESSION"] | ||
| ): | ||
| if brotli_enabled: | ||
| brotli_path = f"{relative_path}.br" | ||
| brotli_content = self.brotli_compress(content) | ||
| self._write_file_content(brotli_path, brotli_content, is_text=False) | ||
| compressed_files.setdefault(path, []).append(brotli_path) | ||
| processed_count += 1 | ||
| except Exception as e: | ||
| logger.error(f"Failed to compress {path}: {e}") | ||
| continue | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: Inconsistent Files that fail compression won't count toward MAX_FILES_PER_RUN. For consistent rate limiting, move
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be addressed |
||
|
|
@@ -210,9 +190,7 @@ def _read_file_content(self, path): | |
| if not is_safe_path(path): | ||
| logger.warning(f"Skipping unsafe path: {path}") | ||
| return None | ||
| max_size = ( | ||
| get_setting("MAX_FILE_SIZE", DEFAULT_SETTINGS["MAX_FILE_SIZE"]) or 10485760 | ||
| ) | ||
| max_size = get_setting("MAX_FILE_SIZE") | ||
| # Try storage methods first | ||
| if self.exists(path): | ||
| with self.open(path) as f: | ||
|
|
@@ -245,12 +223,7 @@ def _write_file_content(self, path, content, is_text=True): | |
| def gzip_compress(self, content): | ||
| """Compress content using gzip.""" | ||
| buffer = io.BytesIO() | ||
| level = ( | ||
| get_setting( | ||
| "COMPRESSION_LEVEL_GZIP", DEFAULT_SETTINGS["COMPRESSION_LEVEL_GZIP"] | ||
| ) | ||
| or 6 | ||
| ) | ||
| level = get_setting("COMPRESSION_LEVEL_GZIP") | ||
| # Clamp level to valid range (0-9) | ||
| level = max(0, min(9, level)) | ||
| with gzip.GzipFile(fileobj=buffer, mode="wb", compresslevel=level) as gz_file: | ||
|
|
@@ -261,12 +234,7 @@ def gzip_compress(self, content): | |
|
|
||
| def brotli_compress(self, content): | ||
| """Compress content using brotli.""" | ||
| level = ( | ||
| get_setting( | ||
| "COMPRESSION_LEVEL_BROTLI", DEFAULT_SETTINGS["COMPRESSION_LEVEL_BROTLI"] | ||
| ) | ||
| or 4 | ||
| ) | ||
| level = get_setting("COMPRESSION_LEVEL_BROTLI") | ||
| # Clamp level to valid range (0-11) | ||
| level = max(0, min(11, level)) | ||
| if isinstance(content, str): | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.