tus / tus-py-client

A Python client for the tus resumable upload protocol
https://tus.io/
MIT License
169 stars 45 forks source link

file_stream bug #60

Closed liuzm617 closed 2 years ago

liuzm617 commented 2 years ago

`

fs = open('path/to/file.ext') uploader = my_client.uploader(file_stream=fs, chunk_size=200)

class BaseUploader:
    self.file_stream = file_stream
    self.stop_at = self.get_file_size()
    def get_file_stream(self):

          if self.file_stream:
              self.file_stream.seek(0)
              return self.file_stream
          elif os.path.isfile(self.file_path):
              return open(self.file_path, 'rb')
          else:
              raise ValueError("invalid file {}".format(self.file_path))

    def get_file_size(self):

        with self.get_file_stream() as stream:
            stream.seek(0, os.SEEK_END)
            return stream.tell()

##Filestream is closed in get_file_size function , If you use it again, you will get an error: ValueError: I/O operation on closed file.**

uploader.upload()

honglei commented 2 years ago

Same problem occured in the master-version, while not occured in version 0.25 from pypi

with open(fullpath,'rb') as fs:
    fs.readable() #True
    my_client.set_headers({'Upload-Metadata': f'fileName {fileName}'})
    uploader: Uploader = my_client.uploader(file_stream=fs,chunk_size=64_000)
    fs.readable() #False
    #print_upload_info(uploader)
    uploader.upload(stop_at=200_000)
    print_upload_info(uploader)
honglei commented 2 years ago

use this version:

    @property
    def file_size(self):
        """
        Return size of the file.
        """
        stream = self.get_file_stream()
        stream.seek(0, os.SEEK_END)
        return stream.tell()
Acconut commented 2 years ago

Thanks for the report, @tzblic! Thanks for the patch, @honglei!