Skip to content

Commit

Permalink
WIP files: implemenet Remotetransfer
Browse files Browse the repository at this point in the history
  • Loading branch information
slint committed Mar 20, 2024
1 parent ea4b1b1 commit 378d6d9
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions invenio_records_resources/services/files/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,47 @@ def set_file_content(self, record, file, file_key, stream, content_length):
super().set_file_content(record, file, file_key, stream, content_length)


class RemoteTransfer(BaseTransfer):
"""Remote transfer."""

def __init__(self, **kwargs):
"""Constructor."""
super().__init__(TransferType.REMOTE, **kwargs)

def init_file(self, record, file_metadata):
"""Initialize a file."""
uri = file_metadata.pop("uri", None)
if not uri:
raise Exception("`uri` is required for remote files.")

file = record.files.create(
key=file_metadata.pop("key"),
obj={
"file": {
"uri": uri,
"storage_class": "R",
# TODO: We should accept size and checksum, so that we could verify
# it below on commit.
"size": None,
"checksum": None,
}
},
data=file_metadata,
)

return file

def set_file_content(self, *args, **kwargs):
"""Do nothing since it's a remote file."""
pass

def commit_file(self, record, file_key):
"""Commit a file."""
# TODO: Try to verify (or populate) the size and checksum in a
# lightweight fashion (e.g. via a HEAD request to the remote)
record.files.commit(file_key)


class FetchTransfer(BaseTransfer):
"""Fetch transfer."""

Expand Down Expand Up @@ -171,6 +212,8 @@ def get_transfer(cls, file_type, **kwargs):
"""Get transfer type."""
if file_type == TransferType.FETCH:
return FetchTransfer(**kwargs)
elif TransferType.REMOTE:
return RemoteTransfer(**kwargs)
else: # default to local
return LocalTransfer(**kwargs)

Expand Down

0 comments on commit 378d6d9

Please sign in to comment.