Skip to content

Commit

Permalink
Object File Uploads: Add validations and download functionality (#10183)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maffooch authored May 13, 2024
1 parent dd1aefc commit 3cff053
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
19 changes: 19 additions & 0 deletions dojo/api_v2/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import os
import re
from datetime import datetime
from typing import List
Expand Down Expand Up @@ -797,6 +798,24 @@ class Meta:
model = FileUpload
fields = "__all__"

def validate(self, data):
if file := data.get("file"):
ext = os.path.splitext(file.name)[1] # [0] returns path+filename
valid_extensions = settings.FILE_UPLOAD_TYPES
if ext.lower() not in valid_extensions:
if accepted_extensions := f"{', '.join(valid_extensions)}":
msg = (
"Unsupported extension. Supported extensions are as "
f"follows: {accepted_extensions}"
)
else:
msg = (
"File uploads are prohibited due to the list of acceptable "
"file extensions being empty"
)
raise ValidationError(msg)
return data


class RawFileSerializer(serializers.ModelSerializer):
file = serializers.FileField(required=True)
Expand Down
13 changes: 11 additions & 2 deletions dojo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,13 +850,22 @@ def clean(self):
# Don't bother validating the formset unless each form is valid on its own
return
for form in self.forms:
print(dir(form))
file = form.cleaned_data.get('file', None)
if file:
ext = os.path.splitext(file.name)[1] # [0] returns path+filename
valid_extensions = settings.FILE_UPLOAD_TYPES
if ext.lower() not in valid_extensions:
form.add_error('file', 'Unsupported file extension.')
if accepted_extensions := f"{', '.join(valid_extensions)}":
msg = (
"Unsupported extension. Supported extensions are as "
f"follows: {accepted_extensions}"
)
else:
msg = (
"File uploads are prohibited due to the list of acceptable "
"file extensions being empty"
)
form.add_error('file', msg)


ManageFileFormSet = modelformset_factory(FileUpload, extra=3, max_num=10, fields=['title', 'file'], can_delete=True, formset=BaseManageFileFormSet)
Expand Down
2 changes: 1 addition & 1 deletion dojo/templates/dojo/view_eng.html
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ <h4>Files<span class="pull-right">
<div class="col-md-2" style="text-align: center">
<div class="row">
{% url 'access_file' fid=file.id oid=eng.id obj_type='Engagement' as image_url %}
<a href="{{ image_url }}" target="_blank">
<a href="{{ image_url }}" target="_blank" download>
{% if file|get_thumbnail %}
<img src="{{ image_url }}" alt="thumbnail" style="width:150px">
{% else %}
Expand Down
2 changes: 1 addition & 1 deletion dojo/templates/dojo/view_finding.html
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ <h4>Files<span class="pull-right">
<div class="col-md-2" style="text-align: center">
<div class="row">
{% url 'access_file' fid=file.id oid=finding.id obj_type='Finding' as image_url %}
<a href="{{ image_url }}" target="_blank">
<a href="{{ image_url }}" target="_blank" download>
{% if file|get_thumbnail %}
<img src="{{ image_url }}" alt="thumbnail" style="width:150px">
{% else %}
Expand Down
2 changes: 1 addition & 1 deletion dojo/templates/dojo/view_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,7 @@ <h4>
<div class="col-md-2" style="text-align: center">
<div class="row">
{% url 'access_file' fid=file.id oid=test.id obj_type='Test' as image_url %}
<a href="{{ image_url }}" target="_blank">
<a href="{{ image_url }}" target="_blank" download>
{% if file|get_thumbnail %}
<img src="{{ image_url }}" alt="thumbnail" style="width:150px">
{% else %}
Expand Down

0 comments on commit 3cff053

Please sign in to comment.