2021-11-20 14:40:07 +00:00
|
|
|
import hashlib
|
|
|
|
|
2021-11-25 09:40:25 +00:00
|
|
|
|
2021-11-20 14:40:07 +00:00
|
|
|
class Chunk:
|
2021-11-25 09:40:25 +00:00
|
|
|
"""A class defining a single chunk of a file to be uploaded"""
|
|
|
|
|
|
|
|
@staticmethod
|
2021-11-25 15:31:49 +00:00
|
|
|
def fromFile(fileobj, count: int, chunksize: int):
|
2021-11-25 09:40:25 +00:00
|
|
|
"""Create a new Chunk object from a File
|
|
|
|
|
|
|
|
Args:
|
|
|
|
fileobj (classes.file.File): The file object from local storage
|
|
|
|
count (int): Position of the current chunk in the list of total
|
|
|
|
chunks (first index: 0) or -1 to get the complete file
|
|
|
|
chunksize (int): Size of each chunk in bytes
|
|
|
|
|
|
|
|
Returns:
|
2021-11-25 15:31:49 +00:00
|
|
|
classes.chunk.Chunk: A Chunk object containing the portion of the
|
|
|
|
File object beginning at (count * chunksize) bytes and ending at
|
|
|
|
((count + 1) * chunksize - 1) bytes
|
2021-11-25 09:40:25 +00:00
|
|
|
"""
|
|
|
|
return fileobj.getChunk(count, chunksize)
|
|
|
|
|
|
|
|
def __init__(self, fileobj, count: int, data: bytes) -> None:
|
|
|
|
"""Initialize a new Chunk object
|
|
|
|
|
|
|
|
Args:
|
|
|
|
fileobj (classes.file.File): The file object from local storage
|
|
|
|
count (int): Position of the current chunk in the list of total
|
|
|
|
chunks (first index: 0) or -1 to get the complete file
|
|
|
|
data (bytes): Content of the chunk
|
|
|
|
"""
|
2021-11-20 14:40:07 +00:00
|
|
|
self.file = fileobj
|
|
|
|
self.count = count if count >= 0 else "complete"
|
|
|
|
self.data = data
|
|
|
|
|
2021-11-25 09:40:25 +00:00
|
|
|
def getTempName(self) -> str:
|
|
|
|
"""Get filename for this Chunk in the temp directory on the Vessel
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: Filename to use for this chunk in the Vessel tempdir
|
|
|
|
"""
|
2021-11-20 14:40:07 +00:00
|
|
|
return f"{self.file.uuid}_{self.count}.part"
|
|
|
|
|
2021-11-25 09:40:25 +00:00
|
|
|
def getHash(self) -> str:
|
|
|
|
"""Generate a hash for this Chunk
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: SHA256 hash of Chunk.data
|
|
|
|
"""
|
|
|
|
return hashlib.sha256(self.data).hexdigest()
|