Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion dvuploader/directupload.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ async def _upload_singlepart(
"headers": headers,
"url": ticket["url"],
"content": upload_bytes(
file=file.handler, # type: ignore
file=file.get_handler(), # type: ignore
progress=progress,
pbar=pbar,
hash_func=file.checksum._hash_fun,
Expand Down
24 changes: 18 additions & 6 deletions dvuploader/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ def extract_file_name(self):

if self.handler is None:
self._validate_filepath(self.filepath)
self.handler = open(self.filepath, "rb")
self._size = os.path.getsize(self.filepath)
else:
self._size = len(self.handler.read())
Expand All @@ -95,6 +94,15 @@ def extract_file_name(self):

return self

def get_handler(self) -> IO:
"""
Opens the file and initializes the file handler.
"""
if self.handler is not None:
return self.handler

return open(self.filepath, "rb")

@staticmethod
def _validate_filepath(path):
"""
Expand Down Expand Up @@ -126,7 +134,6 @@ def apply_checksum(self):

self.checksum.apply_checksum()


def update_checksum_chunked(self, blocksize=2**20):
"""Updates the checksum with data read from a file-like object in chunks.

Expand All @@ -139,12 +146,13 @@ def update_checksum_chunked(self, blocksize=2**20):
Note:
This method resets the file position to the start after reading.
"""
assert self.handler is not None, "File handler is not initialized."
assert self.checksum is not None, "Checksum is not initialized."
assert self.checksum._hash_fun is not None, "Checksum hash function is not set."

handler = self.get_handler()

while True:
buf = self.handler.read(blocksize)
buf = handler.read(blocksize)
Comment thread
JR-1991 marked this conversation as resolved.

if not isinstance(buf, bytes):
buf = buf.encode()
Expand All @@ -153,8 +161,12 @@ def update_checksum_chunked(self, blocksize=2**20):
break
self.checksum._hash_fun.update(buf)

self.handler.seek(0)

if self.handler is not None: # type: ignore
# In case of passed handler, we need to seek the handler to the start after reading.
self.handler.seek(0)
else:
# Path-based handlers will be opened just-in-time, so we can close it.
handler.close()
Comment thread
JR-1991 marked this conversation as resolved.

def __del__(self):
if self.handler is not None:
Expand Down
19 changes: 13 additions & 6 deletions dvuploader/nativeupload.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ async def native_upload(
}

files_new = [file for file in files if not file.to_replace]
files_new_metadata = [file for file in files if file.to_replace and file._unchanged_data]
files_replace = [file for file in files if file.to_replace and not file._unchanged_data]
files_new_metadata = [
file for file in files if file.to_replace and file._unchanged_data
]
files_replace = [
file for file in files if file.to_replace and not file._unchanged_data
]

# These are not in a package but need a metadtata update, ensure even for zips
for file in files_new_metadata:
Expand All @@ -114,7 +118,7 @@ async def native_upload(
file.file_name, # type: ignore
total=file._size,
),
file
file,
)
for file in files_replace
]
Expand Down Expand Up @@ -269,11 +273,12 @@ async def _single_native_upload(
)

json_data = _get_json_data(file)
handler = file.get_handler()

files = {
"file": (
file.file_name,
file.handler,
handler,
file.mimeType,
),
"jsonData": (
Expand Down Expand Up @@ -374,8 +379,10 @@ async def _update_metadata(
if _tab_extension(dv_path) in file_mapping:
file_id = file_mapping[_tab_extension(dv_path)]
elif (
file.file_name and _is_zip(file.file_name)
and not file._is_inside_zip and not file._enforce_metadata_update
file.file_name
and _is_zip(file.file_name)
and not file._is_inside_zip
and not file._enforce_metadata_update
):
# When the file is a zip package it will be unpacked and thus
# the expected file name of the zip will not be in the
Expand Down
2 changes: 1 addition & 1 deletion dvuploader/packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def zip_files(
with zipfile.ZipFile(path, "w") as zip_file:
for file in files:
zip_file.writestr(
data=file.handler.read(), # type: ignore
data=file.get_handler().read(), # type: ignore
zinfo_or_arcname=_create_arcname(file),
)
file._is_inside_zip = True
Expand Down