-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[refactor] File uploads #12322
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
Open
SchrodingersGat
wants to merge
10
commits into
inventree:master
Choose a base branch
from
SchrodingersGat:file-uploads
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[refactor] File uploads #12322
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e6473e1
Create a generic handler for renaming images
SchrodingersGat d748938
Refactor rename_attachment
SchrodingersGat 19bd6a5
rename_stock_item_test_result_attachment
SchrodingersGat 5c7fb05
Fix for helper function
SchrodingersGat 294758f
Refactor report uploads
SchrodingersGat 1f0fa35
Refactor image upload for part and company
SchrodingersGat cb19c50
Merge commit '69ae28d0dfd2cc5f4ac773b0022630b72061e504' into file-upl…
SchrodingersGat 1d92f75
Use proper django checking
SchrodingersGat ee0434d
Merge branch 'master' into file-uploads
SchrodingersGat c23d16f
Merge branch 'master' into file-uploads
SchrodingersGat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| """Common functions for handling media files in InvenTree.""" | ||
|
|
||
| from pathlib import Path | ||
| from typing import Optional | ||
|
|
||
| from django.core.exceptions import ValidationError | ||
|
|
||
|
|
||
| def rename_uploaded_file( | ||
| filename: str, | ||
| file_type: str, | ||
| model_type: Optional[str] = None, | ||
| model_id: Optional[int] = None, | ||
| ) -> str: | ||
| """A centralized function for handling file uploads in InvenTree. | ||
|
|
||
| All uploaded files will be stored in a consistent directory structure, | ||
| which allows for repeatable and predictable file storage. | ||
|
|
||
| Additionally, with a consistent file storage structure, | ||
| it is possible to implement a permissions system for accessing uploaded files. | ||
|
|
||
| Arguments: | ||
| filename: The name of the uploaded file. | ||
| file_type: The type of the file (e.g., 'attachment', 'test_result'). | ||
| model_type: The type of the model associated with the file (optional). | ||
| model_id: The ID of the model associated with the file (optional). | ||
| """ | ||
| filename = str(filename).strip() | ||
|
|
||
| if not filename: | ||
| raise ValidationError('Filename cannot be empty.') | ||
|
|
||
| if not file_type: | ||
| raise ValidationError('File type must be specified.') | ||
|
|
||
| # First, remove any illegal characters from the filename. | ||
| # Keep '.' so valid file extensions are preserved. | ||
| illegal_chars = '\'"\\\\/`~#|!@#$%^&*()[]{}<>?;:+=,' | ||
|
|
||
| for c in illegal_chars: | ||
| filename = filename.replace(c, '') | ||
|
|
||
| # Convert to a Path, ensure the filename is not attempting to traverse directories | ||
| file_path = Path(filename) | ||
|
|
||
| if file_path.is_absolute() or '..' in file_path.parts or len(file_path.parts) > 1: | ||
| raise ValidationError('Invalid filename: cannot contain directory traversal.') | ||
|
|
||
| # Construct an upload path based on the provided parts | ||
| parts = [] | ||
|
|
||
| # If provided, include the file type in the path | ||
| if model_type: | ||
| parts.append(str(model_type)) | ||
|
|
||
| # If provided, include the model ID in the path | ||
| if model_id: | ||
| parts.append(str(model_id)) | ||
|
|
||
| # Include the file type in the path | ||
| parts.append(str(file_type)) | ||
|
|
||
| # Finally, include the sanitized filename | ||
| parts.append(file_path.name) | ||
|
|
||
| # Join all parts to form the final upload path | ||
| return str(Path(*parts)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.