Skip to content

Commit

Permalink
Fix naming issue of files, when executing a tool
Browse files Browse the repository at this point in the history
  • Loading branch information
heisner-tillman committed Jan 26, 2024
1 parent fc2d966 commit c00b7f4
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/galaxy/webapps/galaxy/services/tools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import re
import shutil
import tempfile
from json import dumps
Expand Down Expand Up @@ -77,6 +78,31 @@ def create_temp_file(self, trans, files) -> Dict[str, FilesPayload]:
)
return files_payload

def create_temp_file_execute(self, trans, files) -> Dict[str, FilesPayload]:
# TODO - could access headers from request maybe better to do that
# header_content = request.headers["content-type"]
files_payload = {}
for i, upload_file in enumerate(files):
with tempfile.NamedTemporaryFile(
dir=trans.app.config.new_file_path, prefix="upload_file_data_", delete=False
) as dest:
shutil.copyfileobj(upload_file.file, dest) # type: ignore[misc] # https://github.com/python/mypy/issues/15031
upload_file.file.close()

# try to grab the name from the header
try:
header_items = upload_file.headers.items()[0]
name_search = re.search(r'name="([^"]+)"', header_items[1])
if name_search:
name = name_search.group(1)
else:
raise Exception("No name found in header")
# if we can't find the name in the header use the index
except Exception:
name = f"files_{i}|file_data"
files_payload[name] = FilesPayload(filename=upload_file.filename, local_filename=dest.name)
return files_payload

def create_fetch(
self,
trans: ProvidesHistoryContext,
Expand Down Expand Up @@ -129,7 +155,7 @@ def execute(

create_payload = payload.model_dump()
if files:
create_payload.update(self.create_temp_file(trans, files))
create_payload.update(self.create_temp_file_execute(trans, files))
return self.create(trans, create_payload)

def create(self, trans: ProvidesHistoryContext, payload) -> ToolResponse:
Expand Down

0 comments on commit c00b7f4

Please sign in to comment.