Fix FileLike object to implement file-like interface

- Add seek(), tell(), read() methods to FileLike class
- Add content_type attribute for validation
- Fixes 'FileLike object has no attribute seek' error in chunked uploads
parent 4adc33c8
...@@ -989,6 +989,38 @@ def finalize_upload(): ...@@ -989,6 +989,38 @@ def finalize_upload():
self.path = path self.path = path
self.filename = filename self.filename = filename
self.name = filename self.name = filename
self.content_type = 'application/zip' # Default for ZIP files
self._file = None
self._pos = 0
def __enter__(self):
self._file = open(self.path, 'rb')
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if self._file:
self._file.close()
def read(self, size=-1):
if self._file is None:
self._file = open(self.path, 'rb')
if size == -1:
data = self._file.read()
else:
data = self._file.read(size)
self._pos += len(data)
return data
def seek(self, pos, whence=0):
if self._file is None:
self._file = open(self.path, 'rb')
self._file.seek(pos, whence)
self._pos = self._file.tell()
def tell(self):
if self._file is None:
self._file = open(self.path, 'rb')
return self._file.tell()
def save(self, dst): def save(self, dst):
shutil.move(self.path, dst) shutil.move(self.path, dst)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment