Skip to content

Commit

Permalink
Fix uploaded files cache may have expired
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonami committed Jan 15, 2018
1 parent 00859d5 commit 494c90a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
5 changes: 4 additions & 1 deletion telethon/telegram_bare_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ def upload_file(self,
file,
part_size_kb=None,
file_name=None,
allow_cache=True,
progress_callback=None):
"""Uploads the specified file and returns a handle (an instance
of InputFile or InputFileBig, as required) which can be later used.
Expand Down Expand Up @@ -633,10 +634,12 @@ def upload_file(self,
file = stream.read()
hash_md5 = md5(file)
tuple_ = self.session.get_file(hash_md5.digest(), file_size)
if tuple_:
if tuple_ and allow_cache:
__log__.info('File was already cached, not uploading again')
return InputFile(name=file_name,
md5_checksum=tuple_[0], id=tuple_[2], parts=tuple_[3])
elif tuple_ and not allow_cache:
self.session.clear_file(hash_md5.digest(), file_size)
else:
hash_md5 = None

Expand Down
27 changes: 21 additions & 6 deletions telethon/telegram_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .errors import (
RPCError, UnauthorizedError, PhoneCodeEmptyError, PhoneCodeExpiredError,
PhoneCodeHashEmptyError, PhoneCodeInvalidError, LocationInvalidError,
SessionPasswordNeededError)
SessionPasswordNeededError, FilePartMissingError)
from .network import ConnectionMode
from .tl import TLObject
from .tl.custom import Draft, Dialog
Expand Down Expand Up @@ -813,6 +813,7 @@ def send_file(self, entity, file, caption='',
reply_to=None,
attributes=None,
thumb=None,
allow_cache=True,
**kwargs):
"""
Sends a file to the specified entity.
Expand Down Expand Up @@ -849,9 +850,13 @@ def send_file(self, entity, file, caption='',
Optional attributes that override the inferred ones, like
``DocumentAttributeFilename`` and so on.
thumb (:obj:`str` | :obj:`bytes` | :obj:`file`):
thumb (:obj:`str` | :obj:`bytes` | :obj:`file`, optional):
Optional thumbnail (for videos).
allow_cache (:obj:`bool`, optional):
Whether to allow using the cached version stored in the
database or not. Defaults to ``True`` to avoid reuploads.
Kwargs:
If "is_voice_note" in kwargs, despite its value, and the file is
sent as a document, it will be sent as a voice note.
Expand All @@ -868,7 +873,7 @@ def send_file(self, entity, file, caption='',
)

file_handle = self.upload_file(
file, progress_callback=progress_callback)
file, progress_callback=progress_callback, allow_cache=allow_cache)

if as_photo and not force_document:
media = InputMediaUploadedPhoto(file_handle, caption)
Expand Down Expand Up @@ -926,9 +931,19 @@ def send_file(self, entity, file, caption='',
media=media,
reply_to_msg_id=self._get_reply_to(reply_to)
)
result = self(request)

return self._get_response_message(request, result)
try:
return self._get_response_message(request, self(request))
except FilePartMissingError:
# After a while, cached files are invalidated and this
# error is raised. The file needs to be uploaded again.
if not allow_cache:
raise
return self.send_file(
entity, file, allow_cache=False,
caption=caption, force_document=force_document,
progress_callback=progress_callback, reply_to=reply_to,
attributes=attributes, thumb=thumb, **kwargs
)

def send_voice_note(self, entity, file, caption='', upload_progress=None,
reply_to=None):
Expand Down
8 changes: 8 additions & 0 deletions telethon/tl/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,11 @@ def cache_file(self, md5_digest, file_size, file_id, part_count):
(md5_digest, file_size, file_id, part_count)
)
self.save()

def clear_file(self, md5_digest, file_size):
with self._db_lock:
self._conn.execute(
'delete from sent_files where '
'md5_digest = ? and file_size = ?', (md5_digest, file_size)
)
self.save()

0 comments on commit 494c90a

Please sign in to comment.