-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom SecureFileField class in FileField.
- Loading branch information
1 parent
bd12e9c
commit 47c0f1e
Showing
10 changed files
with
54 additions
and
78 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
19 changes: 0 additions & 19 deletions
19
flash_update/migrations/0013_alter_flashgraphicmap_file.py
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
flash_update/migrations/0014_alter_flashupdate_extracted_file.py
This file was deleted.
Oops, something went wrong.
This file contains 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 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,30 @@ | ||
import datetime | ||
import posixpath | ||
from uuid import uuid4 | ||
|
||
from django.core.files.utils import validate_file_name | ||
from django.db.models.fields.files import FileField | ||
|
||
|
||
class SecureFileField(FileField): | ||
def generate_filename(self, instance, filename): | ||
""" | ||
Apply (if callable) or prepend (if a string) upload_to to the filename, | ||
then delegate further processing of the name to the storage backend. | ||
Until the storage layer, all file paths are expected to be Unix style | ||
(with forward slashes). | ||
Add random uuid in the file name. | ||
""" | ||
extension = filename.split(".")[-1] | ||
old_file_name = filename.split(".")[0] | ||
# Create a unique filename using uuid4 | ||
filename = f"{old_file_name}-{uuid4().hex}.{extension}" | ||
|
||
if callable(self.upload_to): | ||
filename = self.upload_to(instance, filename) | ||
else: | ||
dirname = datetime.datetime.now().strftime(str(self.upload_to)) | ||
filename = posixpath.join(dirname, filename) | ||
filename = validate_file_name(filename, allow_relative_path=True) | ||
|
||
return self.storage.generate_filename(filename) |
This file contains 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 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