Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP files: implement RemoteTransfer #567

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading