Skip to content

Commit

Permalink
Merge pull request #1214 from eclipsotic/tmp_upload_file_removal
Browse files Browse the repository at this point in the history
Remove unnecessary saving of uploaded file to tmp dir
  • Loading branch information
jstucke authored Apr 26, 2024
2 parents 404dc3a + 7ba8e19 commit 2cd82be
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 32 deletions.
29 changes: 1 addition & 28 deletions src/helperFunctions/task_conversion.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
from __future__ import annotations

import logging
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING, Any

from markupsafe import escape

import config
from helperFunctions.uid import create_uid
from objects.firmware import Firmware

if TYPE_CHECKING:
from flask import Request
from werkzeug.datastructures import FileStorage

OPTIONAL_FIELDS = ['tags', 'device_part']
DROPDOWN_FIELDS = ['device_class', 'vendor', 'device_name', 'device_part']
Expand Down Expand Up @@ -41,14 +36,13 @@ def get_file_name_and_binary_from_request(request: Request) -> tuple[str, bytes]
Retrieves the file name and content from the flask request object.
:param request: The flask request object.
:param config: The FACT configuration.
:return: A Tuple containing the file name and the file content.
"""
try:
file_name = escape(request.files['file'].filename)
except AttributeError:
file_name = 'no name'
file_binary = _get_uploaded_file_binary(request.files['file'])
file_binary = request.files['file'].read() if request.files['file'] else None
return file_name, file_binary


Expand Down Expand Up @@ -141,27 +135,6 @@ def _get_uid_of_analysis_task(analysis_task: dict) -> str | None:
return None


def _get_uploaded_file_binary(request_file: FileStorage) -> bytes | None:
"""
Retrieves the binary from the request file storage and returns it as byte string. May return `None` if no
binary was found or an exception occurred.
:param request_file: A file contained in the flask request object.
:param config: The FACT configuration.
:return: The binary as byte string or `None` if no binary was found.
"""
if not request_file:
return None
with TemporaryDirectory(prefix='fact_upload_', dir=config.common.temp_dir_path) as tmp_dir:
tmp_file_path = Path(tmp_dir) / 'upload.bin'
try:
request_file.save(str(tmp_file_path))
return tmp_file_path.read_bytes()
except OSError:
logging.error('Encountered error when trying to read uploaded file:', exc_info=True)
return None


def check_for_errors(analysis_task: dict) -> dict[str, str]:
"""
Check an analysis task for missing fields and return a dict with error messages (that are intended to be displayed
Expand Down
4 changes: 0 additions & 4 deletions src/test/unit/helperFunctions/test_task_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from helperFunctions.task_conversion import (
_get_tag_list,
_get_uid_of_analysis_task,
_get_uploaded_file_binary,
check_for_errors,
convert_analysis_task_to_fw_obj,
)
Expand Down Expand Up @@ -41,9 +40,6 @@ def test_check_for_errors(self):
assert len(result) == 1, 'number of invalid fields not correct'
assert result['b'] == 'Please specify the b'

def test_get_uploaded_file_binary_error(self):
assert _get_uploaded_file_binary(None) is None, 'missing upload file should lead to None'

def test_get_uid_of_analysis_task(self):
analysis_task = {'binary': b'this is a test'}
assert (
Expand Down

0 comments on commit 2cd82be

Please sign in to comment.