*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects
Commit cc983a00 authored by Moser, Maximilian's avatar Moser, Maximilian
Browse files

Unify logic for creating download files

* the file paths are now based on the container id and file name
* if no base directory is specified, it is based on a random temporary
  directory
* the path for the base directory is based on `tempfile.tempdir`, which
  can be influenced e.g. through the $TMPDIR environment variable
parent d316c743
No related branches found
No related tags found
1 merge request!8v0.3.1
...@@ -26,7 +26,7 @@ from dbrepo.api.dto import ( ...@@ -26,7 +26,7 @@ from dbrepo.api.dto import (
from dbrepo.RestClient import RestClient from dbrepo.RestClient import RestClient
from pandas import concat from pandas import concat
from ..utils import url_regex from ..utils import create_download_file, url_regex
from .base import BaseWrapper from .base import BaseWrapper
db_tbl_regex = re.compile(r"(/api)?/database/([0-9]+)(/table/([0-9]+)(/.*)?)?") db_tbl_regex = re.compile(r"(/api)?/database/([0-9]+)(/table/([0-9]+)(/.*)?)?")
...@@ -290,6 +290,8 @@ class DBRepo(BaseWrapper): ...@@ -290,6 +290,8 @@ class DBRepo(BaseWrapper):
# write the dataframe to a CSV file, with the columns in the same order # write the dataframe to a CSV file, with the columns in the same order
# as they were uploaded, and without index column # as they were uploaded, and without index column
name = f"{table_name}.csv" file_path = create_download_file(
data.to_csv(name, index=False, columns=[c.name for c in table.columns]) database_id, f"{table_name.rstrip('.csv')}.csv"
return name )
data.to_csv(file_path, index=False, columns=[c.name for c in table.columns])
return file_path
...@@ -9,7 +9,6 @@ import getpass ...@@ -9,7 +9,6 @@ import getpass
import os.path import os.path
import pathlib import pathlib
import re import re
import tempfile
import urllib.parse import urllib.parse
from typing import Iterable, Optional, Tuple from typing import Iterable, Optional, Tuple
...@@ -18,7 +17,7 @@ from inveniordm_py.files.metadata import FilesListMetadata, OutgoingStream ...@@ -18,7 +17,7 @@ from inveniordm_py.files.metadata import FilesListMetadata, OutgoingStream
from inveniordm_py.records.metadata import DraftMetadata from inveniordm_py.records.metadata import DraftMetadata
from inveniordm_py.records.resources import Draft from inveniordm_py.records.resources import Draft
from ..utils import doi_regex, url_regex from ..utils import create_download_file, doi_regex, url_regex
from .base import BaseWrapper from .base import BaseWrapper
recid_regex = re.compile(r"^.*(/api)?/records/(.*)$") recid_regex = re.compile(r"^.*(/api)?/records/(.*)$")
...@@ -180,8 +179,8 @@ class InvenioRDM(BaseWrapper): ...@@ -180,8 +179,8 @@ class InvenioRDM(BaseWrapper):
record.get() record.get()
response = record.files(file_name).download() response = record.files(file_name).download()
with tempfile.NamedTemporaryFile(delete=False) as downloaded_file: file_name = create_download_file(record_pid, file_name)
with open(file_name, "wb") as downloaded_file:
for chunk in response.iter_content(chunk_size=256): for chunk in response.iter_content(chunk_size=256):
downloaded_file.write(chunk) downloaded_file.write(chunk)
......
...@@ -5,7 +5,29 @@ ...@@ -5,7 +5,29 @@
"""Utility functions.""" """Utility functions."""
import pathlib
import re import re
import tempfile
from typing import Optional
doi_regex = re.compile(r"^((https?://)?doi.org/)?(10\.\d+)/(.*)$") doi_regex = re.compile(r"^((https?://)?doi.org/)?(10\.\d+)/(.*)$")
url_regex = re.compile(r"^((.*?)://)?(.*)$") url_regex = re.compile(r"^((.*?)://)?(.*)$")
_tempdir = tempfile.mkdtemp()
"""Randomly named temporary directory, fixed for the duration of the run.
The outcome can be influenced by setting ``tempfile.tempdir`` before first use.
"""
def create_download_file(
container_id: str | int, file_name: str, dir: Optional[str] = None
) -> str:
"""Create a file for storing downloaded content."""
cid = str(container_id)
dir = pathlib.Path(dir or _tempdir) / cid
dir.mkdir(mode=0o700, parents=True, exist_ok=True)
file_path = dir / file_name
file_path.touch(0o700)
return str(file_path)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment