Skip to content

Commit

Permalink
Do not store the hypothetically produced mimetypes always
Browse files Browse the repository at this point in the history
  • Loading branch information
Donnype committed Sep 26, 2024
1 parent 3300690 commit 99134fc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion boefjes/boefjes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def boefje_output(
bytes_client.save_boefje_meta(boefje_meta)

if boefje_output.files:
mime_types = _default_mime_types(boefje_meta.boefje).union(plugin.produces)
mime_types = _default_mime_types(boefje_meta.boefje)
for file in boefje_output.files:
raw = base64.b64decode(file.content)
# when supported, also save file.name to Bytes
Expand Down
8 changes: 4 additions & 4 deletions boefjes/boefjes/job_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,11 @@ def handle(self, boefje_meta: BoefjeMeta) -> None:
boefje_meta.runnable_hash = plugin.runnable_hash
boefje_meta.environment = get_environment_settings(boefje_meta, plugin.boefje_schema)

mime_types = _default_mime_types(boefje_meta.boefje).union(plugin.produces)

logger.info("Starting boefje %s[%s]", boefje_meta.boefje.id, str(boefje_meta.id))

boefje_meta.started_at = datetime.now(timezone.utc)

boefje_results: list[tuple[set, bytes | str]]
boefje_results: list[tuple[set, bytes | str]] = []

try:
boefje_results = self.job_runner.run(boefje_meta, boefje_meta.environment)
Expand All @@ -152,7 +150,9 @@ def handle(self, boefje_meta: BoefjeMeta) -> None:
)
else:
valid_mimetypes.add(mimetype)
raw_file_id = self.bytes_client.save_raw(boefje_meta.id, output, mime_types.union(valid_mimetypes))
raw_file_id = self.bytes_client.save_raw(
boefje_meta.id, output, _default_mime_types(boefje_meta.boefje).union(valid_mimetypes)
)
logger.info(
"Saved raw file %s for boefje %s[%s]", raw_file_id, boefje_meta.boefje.id, boefje_meta.id
)
Expand Down
9 changes: 8 additions & 1 deletion bytes/tests/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from httpx import HTTPError, HTTPStatusError

from bytes.api.models import BoefjeOutput
from bytes.models import BoefjeMeta, NormalizerMeta
from bytes.models import BoefjeMeta, NormalizerMeta, RawDataMeta
from bytes.repositories.meta_repository import BoefjeMetaFilter, NormalizerMetaFilter, RawDataFilter

BYTES_API_CLIENT_VERSION = "0.2"
Expand Down Expand Up @@ -164,6 +164,13 @@ def get_raw(self, raw_id: UUID) -> bytes:

return response.content

@retry_with_login
def get_raw_meta(self, raw_id: UUID) -> RawDataMeta:
response = self.client.get(f"/bytes/raw/{raw_id}/meta")
self._verify_response(response)

return RawDataMeta.model_validate(response.json())

@retry_with_login
def get_raws(self, query_filter: RawDataFilter) -> dict[str, str]:
params = query_filter.model_dump(exclude_none=True)
Expand Down
17 changes: 15 additions & 2 deletions bytes/tests/integration/test_bytes_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,18 @@ def test_raw_mimes(bytes_api_client: BytesAPIClient) -> None:

raw = b"test 123456"
second_raw = b"second test 200"
bytes_api_client.save_raw(boefje_meta.id, raw, mime_types)
bytes_api_client.save_raw(boefje_meta.id, second_raw, second_mime_types)
first_id = bytes_api_client.save_raw(boefje_meta.id, raw, mime_types)
second_id = bytes_api_client.save_raw(boefje_meta.id, second_raw, second_mime_types)

first_meta = bytes_api_client.get_raw_meta(first_id)
second_meta = bytes_api_client.get_raw_meta(second_id)

assert first_meta.id != second_meta.id
assert {x.value for x in first_meta.mime_types} == set(mime_types)
assert {x.value for x in second_meta.mime_types} == set(second_mime_types)

assert bytes_api_client.get_raw(first_id) == raw
assert bytes_api_client.get_raw(second_id) == second_raw

retrieved_raws = bytes_api_client.get_raws(
RawDataFilter(
Expand Down Expand Up @@ -386,3 +396,6 @@ def test_save_multiple_raw_files(bytes_api_client: BytesAPIClient) -> None:

assert bytes_api_client.get_raw(ids["first"]) == first_raw
assert bytes_api_client.get_raw(ids["second"]) == second_raw

assert bytes_api_client.get_raw_meta(ids["first"]).mime_types == set()
assert {x.value for x in bytes_api_client.get_raw_meta(ids["second"]).mime_types} == {"mime", "type"}

0 comments on commit 99134fc

Please sign in to comment.