Skip to content

Commit

Permalink
Allow JSON uploads re #10798
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed May 6, 2024
1 parent e33a342 commit 35b4f1b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 25 deletions.
8 changes: 5 additions & 3 deletions arches/app/etl_modules/base_import_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,12 @@ def read(self, request):
validator = FileValidator()
if len(validator.validate_file_type(content)) > 0:
return {
"status": 400,
"success": False,
"title": _("Invalid excel file/zip specified"),
"message": _("Upload a valid excel file"),
"data": FileValidationError(
title=_("Invalid excel file/zip specified"),
message=_("Upload a valid excel file"),
code=400,
)
}
if content.name.split(".")[-1].lower() == "zip":
with zipfile.ZipFile(content, "r") as zip_ref:
Expand Down
9 changes: 5 additions & 4 deletions arches/app/etl_modules/jsonld_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ def read(self, request):
}
}
validator = FileValidator()
if validator.validate_file_type(content):
if (file_type_errors := validator.validate_file_type(content)):
return {
"status": 400,
"success": False,
"title": _("Invalid Uploaded File"),
"message": _("Upload a valid zip file"),
"data": FileValidationError(
message=", ".join(file_type_errors),
code=400,
)
}

with zipfile.ZipFile(content, "r") as zip_ref:
Expand Down
35 changes: 19 additions & 16 deletions arches/app/utils/file_validator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io
import json
import logging
import zipfile
from django.conf import settings
Expand All @@ -14,15 +15,17 @@ def __init__(self):

def test_unknown_filetypes(self, file, extension=None):
errors = []
if extension in settings.FILE_TYPES:
if extension == "xlsx":
match extension:
case "DS_Store":
self.logger.log(logging.WARN, "DS_Store file encountered, proceeding with caution.")
case _ if extension not in settings.FILE_TYPES:
errors.append(f"File type is not permitted: {extension}")
case "xlsx":
try:
load_workbook(io.BytesIO(file))
except (InvalidFileException, zipfile.BadZipFile):
error = "Invalid xlsx workbook"
self.logger.log(logging.ERROR, error)
errors.append(error)
elif extension == "csv":
errors.append("Invalid xlsx workbook")
case "csv":
try:
datareader = csv.reader(file.decode("utf-8").splitlines(), delimiter=",")
length = None
Expand All @@ -32,17 +35,17 @@ def test_unknown_filetypes(self, file, extension=None):
elif length is None:
length = len(row)
except csv.Error:
error = "Invalid csv file"
self.logger.log(logging.ERROR, error)
errors.append(error)
else:
error = "Cannot validate file"
self.logger.log(logging.ERROR, error)
errors.append(error)
else:
error = "File type is not permitted"
errors.append("Invalid csv file")
case "json":
try:
json.load(io.BytesIO(file))
except json.decoder.JSONDecodeError:
errors.append("Invalid json file")
case _:
errors.append("Cannot validate file")

for error in errors:
self.logger.log(logging.ERROR, error)
errors.append(error)

return errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ SEARCH_COMPONENT_LOCATIONS.append('{{ project_name }}.search_components')
LOCALE_PATHS.append(os.path.join(APP_ROOT, 'locale'))

FILE_TYPE_CHECKING = False
FILE_TYPES = ["bmp", "gif", "jpg", "jpeg", "pdf", "png", "psd", "rtf", "tif", "tiff", "xlsx", "csv", "zip"]
FILE_TYPES = ["bmp", "gif", "jpg", "jpeg", "json", "pdf", "png", "psd", "rtf", "tif", "tiff", "xlsx", "csv", "zip"]
FILENAME_GENERATOR = "arches.app.utils.storage_filename_generator.generate_filename"
UPLOADED_FILES_DIR = "uploadedfiles"

Expand Down
2 changes: 1 addition & 1 deletion arches/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@
]

FILE_TYPE_CHECKING = False
FILE_TYPES = ["bmp", "gif", "jpg", "jpeg", "pdf", "png", "psd", "rtf", "tif", "tiff", "xlsx", "csv", "zip"]
FILE_TYPES = ["bmp", "gif", "jpg", "jpeg", "json", "pdf", "png", "psd", "rtf", "tif", "tiff", "xlsx", "csv", "zip"]
FILENAME_GENERATOR = "arches.app.utils.storage_filename_generator.generate_filename"
UPLOADED_FILES_DIR = "uploadedfiles"

Expand Down
3 changes: 3 additions & 0 deletions releases/7.6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Arches 7.6.0 Release Notes

- Plugins now support the configuration boolean `is_standalone`. Standalone plugins do not appear in the sidebar, and do not display the sidebar or application header.

- 10862 JSON-LD bulk import module

### Performance Improvements
- 10453 Reduce queries for related objects when indexing resources

Expand Down Expand Up @@ -202,6 +204,7 @@ Minor incompatibilities:
},
}
```
3. Add `"json"` to `FILE_TYPES` if you wish to use the JSON-LD bulk import module.
9. Run `python manage.py updateproject`
Expand Down

0 comments on commit 35b4f1b

Please sign in to comment.