Skip to content

Commit

Permalink
Changed default path for database and files to use md5 of phone or token
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akhmetov committed Apr 5, 2019
1 parent 56f4f26 commit 46db8ac
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog

[unreleased]

- **Incompatible** default path for files is changed. Now the library uses an md5 hash of the phone number or bot token instead of just a phone number.
It should not be noticeable for most cases, but if you rely on locally saved files or database, you need to pass the ``files_directory`` parameter to the ``telegram.client.Telegram``.
- Fixed problem with randomly raised "Database encryption key is needed" errors during login process. (#12)
- Fixed `stop` method execution. (#8)
- Added ``examples/bot_login.py`` example.
Expand Down
9 changes: 8 additions & 1 deletion telegram/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import hashlib
import time
import queue
import signal
Expand Down Expand Up @@ -75,7 +76,13 @@ def __init__(
self._database_encryption_key = database_encryption_key

if not files_directory:
files_directory = f'/tmp/.tdlib_files/{self.phone}/'
hasher = hashlib.md5()
hasher.update(
(self.phone or self.bot_token).encode('utf-8') # type: ignore
)
directory_name = hasher.hexdigest()
files_directory = f'/tmp/.tdlib_files/{directory_name}/'

self.files_directory = files_directory

self._authorized = False
Expand Down
5 changes: 3 additions & 2 deletions tests/test_telegram_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ def test_get_chat_history(self, telegram):

def test_set_initial_params(self, telegram):
async_result = telegram._set_initial_params()
phone_md5 = '69560384b84c896952ef20352fbce705'

exp_data = {
'@type': 'setTdlibParameters',
Expand All @@ -247,9 +248,9 @@ def test_set_initial_params(self, telegram):
'system_version': 'unknown',
'application_version': VERSION,
'system_language_code': 'en',
'database_directory': f'/tmp/.tdlib_files/{PHONE}/database',
'database_directory': f'/tmp/.tdlib_files/{phone_md5}/database',
'use_message_database': True,
'files_directory': f'/tmp/.tdlib_files/{PHONE}/files',
'files_directory': f'/tmp/.tdlib_files/{phone_md5}/files',
},
'@extra': {'request_id': 'updateAuthorizationState'},
}
Expand Down

0 comments on commit 46db8ac

Please sign in to comment.