Skip to content

Commit

Permalink
Issue #3: Split large files (> 1.5GB)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekmo committed Nov 7, 2020
1 parent 27fca8a commit 9a95c19
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions telegram_upload/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def send_files(self, entity, files, delete_on_success=False, print_file_id=False
if isinstance(file, File):
name = file_name = file.file_name
file_size = file.file_size
force_file = True
else:
file_name = os.path.basename(file)
file_size = os.path.getsize(file)
Expand Down
19 changes: 15 additions & 4 deletions telegram_upload/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


import mimetypes
from io import FileIO
from io import FileIO, SEEK_SET
from typing import Union

from hachoir.metadata.video import MP4Metadata
Expand Down Expand Up @@ -126,6 +126,9 @@ def __init__(self, file: Union[str, bytes, int], max_read_size: int, name: str):
def read(self, size: int = -1) -> bytes:
if size == -1:
size = self.remaining_size
if not self.remaining_size:
return b''
size = min(self.remaining_size, size)
self.remaining_size -= size
return super().read(size)

Expand All @@ -140,12 +143,20 @@ def file_name(self):
def file_size(self):
return self.max_read_size

def seek(self, offset: int, whence: int = SEEK_SET, split_seek: bool = False) -> int:
if not split_seek:
self.remaining_size += self.tell() - offset
return super().seek(offset, whence)


class SplitFiles(LargeFilesBase):
def process_large_file(self, file):
parts = math.ceil(os.path.getsize(file) / MAX_FILE_SIZE)
file_name = os.path.basename(file)
total_size = os.path.getsize(file)
parts = math.ceil(total_size / MAX_FILE_SIZE)
zfill = int(math.log10(10)) + 1
for part in range(parts):
splitted_file = SplitFile(file, MAX_FILE_SIZE, '{}.{}'.format(file, str(part).zfill(zfill)))
splitted_file.seek(MAX_FILE_SIZE * part)
size = total_size - (part * MAX_FILE_SIZE) if part >= parts - 1 else MAX_FILE_SIZE
splitted_file = SplitFile(file, size, '{}.{}'.format(file_name, str(part).zfill(zfill)))
splitted_file.seek(MAX_FILE_SIZE * part, split_seek=True)
yield splitted_file

0 comments on commit 9a95c19

Please sign in to comment.