Skip to content

Commit cff0dc9

Browse files
Secure files extending file name with uuid.
1 parent ea5dce1 commit cff0dc9

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Generated by Django 4.2.15 on 2024-09-03 09:37
2+
3+
from django.db import migrations, models
4+
import flash_update.models
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('flash_update', '0012_auto_20230410_0720'),
11+
]
12+
13+
operations = [
14+
migrations.AlterField(
15+
model_name='flashgraphicmap',
16+
name='file',
17+
field=models.FileField(upload_to=flash_update.models.flash_map_upload_to, verbose_name='file'),
18+
),
19+
]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Generated by Django 4.2.15 on 2024-09-03 09:42
2+
3+
from django.db import migrations, models
4+
import flash_update.models
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('flash_update', '0013_alter_flashgraphicmap_file'),
11+
]
12+
13+
operations = [
14+
migrations.AlterField(
15+
model_name='flashupdate',
16+
name='extracted_file',
17+
field=models.FileField(blank=True, null=True, upload_to=flash_update.models.flash_extracted_file_upload_to, verbose_name='extracted file'),
18+
),
19+
]

flash_update/models.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# import os
2+
# from uuid import uuid4
13
import reversion
24
from django.conf import settings
35
from django.contrib.auth.models import Group
@@ -6,6 +8,7 @@
68
from django.utils.translation import gettext_lazy as _
79
from tinymce.models import HTMLField
810

11+
from main.utils import custom_upload_to
912
from api.models import (
1013
ActionCategory,
1114
ActionOrg,
@@ -15,10 +18,15 @@
1518
District,
1619
)
1720

21+
def flash_map_upload_to(instance, filename):
22+
return custom_upload_to('flash_update/images/')(instance, filename)
23+
24+
def flash_extracted_file_upload_to(instance, filename):
25+
return custom_upload_to('flash_update/pdf/')(instance, filename)
1826

1927
@reversion.register()
2028
class FlashGraphicMap(models.Model):
21-
file = models.FileField(verbose_name=_("file"), upload_to="flash_update/images/")
29+
file = models.FileField(verbose_name=_("file"), upload_to=flash_map_upload_to)
2230
caption = models.CharField(max_length=225, blank=True, null=True)
2331
created_by = models.ForeignKey(
2432
settings.AUTH_USER_MODEL,
@@ -116,7 +124,7 @@ class FlashShareWith(models.TextChoices):
116124
verbose_name=_("share with"),
117125
)
118126
references = models.ManyToManyField(FlashReferences, blank=True, verbose_name=_("references"))
119-
extracted_file = models.FileField(verbose_name=_("extracted file"), upload_to="flash_update/pdf/", blank=True, null=True)
127+
extracted_file = models.FileField(verbose_name=_("extracted file"), upload_to=flash_extracted_file_upload_to, blank=True, null=True)
120128
extracted_at = models.DateTimeField(verbose_name=_("extracted at"), blank=True, null=True)
121129

122130
class Meta:

main/utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import os
12
import datetime
23
import json
34
import typing
5+
from uuid import uuid4
46
from collections import defaultdict
57
from tempfile import NamedTemporaryFile, _TemporaryFileWrapper
68

@@ -15,6 +17,20 @@
1517
from reversion.revisions import _get_options
1618

1719

20+
def custom_upload_to(directory):
21+
"""
22+
Rename file name with adding uuid
23+
"""
24+
def upload_to(instance, filename):
25+
# Get the file extension
26+
extension = filename.split('.')[-1]
27+
old_file_name = filename.split('.')[0]
28+
# Create a unique filename using uuid4
29+
new_filename = f"{old_file_name}-{uuid4().hex}.{extension}"
30+
# Return the new file path
31+
return os.path.join(directory, new_filename)
32+
return upload_to
33+
1834
def is_tableau(request):
1935
"""Checking the request for the 'tableau' parameter
2036
(used mostly for switching to the *TableauSerializers)

0 commit comments

Comments
 (0)