Skip to content

Commit

Permalink
Merge pull request #34 from eodcgmbh/upload-still-broken
Browse files Browse the repository at this point in the history
if files were not used in the put request, check the request body
  • Loading branch information
SerRichard authored Nov 21, 2024
2 parents 3cc6dd9 + 96811fb commit 378e04d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
27 changes: 19 additions & 8 deletions openeo_argoworkflows/api/openeo_argoworkflows_api/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

fs = fsspec.filesystem(protocol="file")


@dataclass
class ByteRange:
start: int
Expand Down Expand Up @@ -238,21 +239,31 @@ async def upload_file(
upload_dest = space.files_directory / path

form_data = await request.form()
for file in form_data.values():

if len(form_data) == 0:
form_data = None
request_body = await request.body()

with open(upload_dest, "wb") as f:
while contents := file.file.read(1024 * 1024):
f.write(contents)
f.write(request_body)

else:
for file in form_data.values():

with open(upload_dest, "wb") as f:
while contents := file.file.read(1024 * 1024):
f.write(contents)

size_bytes = fs.stat(upload_dest)["size"]
# Formatted for RFC3339
modified_time = datetime.datetime.now(datetime.UTC).strftime("%Y-%m-%dT%H:%M:%S.%f")[:-4] + "Z"

except Exception:
raise HTTPException(
status_code=501,
status_code=500,
detail="The server encountered an error trying to upload the file.",
)

size_bytes = fs.stat(upload_dest)["size"]
# Formatted for RFC3339
modified_time = datetime.datetime.now(datetime.UTC).strftime("%Y-%m-%dT%H:%M:%S.%f")[:-4] + "Z"

return Response(
status_code=200,
content=json.dumps({
Expand Down
2 changes: 1 addition & 1 deletion openeo_argoworkflows/api/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openeo-argoworkflows-api"
version = "2024.11.8"
version = "2024.11.9"
description = ""
authors = ["sean <sean.hoyal@external.eodc.eu>"]
readme = "README.md"
Expand Down
13 changes: 12 additions & 1 deletion openeo_argoworkflows/api/tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def test_file_list(user_validate, a_mock_user, mock_settings):
@patch("openeo_fastapi.client.auth.Authenticator.validate")
def test_file_upload(user_validate, a_mock_user, mock_settings):

fs = fsspec.filesystem(protocol="file")

user_validate.return_value = a_mock_user

create(create_object=a_mock_user)
Expand All @@ -114,11 +116,20 @@ def test_file_upload(user_validate, a_mock_user, mock_settings):

expected_file = mock_settings.OPENEO_WORKSPACE_ROOT / f"{str(a_mock_user.user_id)}/FILES/fake-process-graph.json"

fs = fsspec.filesystem(protocol="file")
assert fs.exists(expected_file)

with open(original_file, "rb") as f:
resp = app.put(f"{mock_settings.OPENEO_PREFIX}/files/fake-process-graph-2.json", headers=headers, data=f)

assert resp.status_code == 200
assert resp.json()["path"] == "fake-process-graph-2.json"

expected_file = mock_settings.OPENEO_WORKSPACE_ROOT / f"{str(a_mock_user.user_id)}/FILES/fake-process-graph-2.json"

assert fs.exists(expected_file)



@patch("openeo_fastapi.client.auth.Authenticator.validate")
def test_file_delete(user_validate, a_mock_user, mock_settings):

Expand Down

0 comments on commit 378e04d

Please sign in to comment.