2019-06-19 14:04:35 +00:00
|
|
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
|
|
|
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
|
|
|
|
#
|
|
|
|
# This file is part of Pyrogram.
|
|
|
|
#
|
|
|
|
# Pyrogram is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Pyrogram is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import base64
|
|
|
|
import json
|
|
|
|
import logging
|
2019-09-08 09:58:34 +00:00
|
|
|
import os
|
2019-06-19 14:04:35 +00:00
|
|
|
import sqlite3
|
|
|
|
from pathlib import Path
|
|
|
|
|
2019-09-14 17:56:07 +00:00
|
|
|
from .sqlite_storage import SQLiteStorage
|
2019-06-19 14:04:35 +00:00
|
|
|
|
2019-09-08 17:24:06 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2019-06-19 14:04:35 +00:00
|
|
|
|
2019-09-14 17:56:07 +00:00
|
|
|
class FileStorage(SQLiteStorage):
|
2019-06-19 14:04:35 +00:00
|
|
|
FILE_EXTENSION = ".session"
|
|
|
|
|
|
|
|
def __init__(self, name: str, workdir: Path):
|
|
|
|
super().__init__(name)
|
|
|
|
|
|
|
|
self.database = workdir / (self.name + self.FILE_EXTENSION)
|
|
|
|
|
2019-06-20 01:31:37 +00:00
|
|
|
def migrate_from_json(self, session_json: dict):
|
2019-06-19 14:04:35 +00:00
|
|
|
self.open()
|
|
|
|
|
2019-09-14 17:56:07 +00:00
|
|
|
self.dc_id(session_json["dc_id"])
|
|
|
|
self.test_mode(session_json["test_mode"])
|
|
|
|
self.auth_key(base64.b64decode("".join(session_json["auth_key"])))
|
|
|
|
self.user_id(session_json["user_id"])
|
|
|
|
self.date(session_json.get("date", 0))
|
|
|
|
self.is_bot(session_json.get("is_bot", False))
|
2019-06-19 14:04:35 +00:00
|
|
|
|
2019-06-20 01:31:37 +00:00
|
|
|
peers_by_id = session_json.get("peers_by_id", {})
|
|
|
|
peers_by_phone = session_json.get("peers_by_phone", {})
|
2019-06-19 14:04:35 +00:00
|
|
|
|
|
|
|
peers = {}
|
|
|
|
|
|
|
|
for k, v in peers_by_id.items():
|
|
|
|
if v is None:
|
|
|
|
type_ = "group"
|
|
|
|
elif k.startswith("-100"):
|
|
|
|
type_ = "channel"
|
|
|
|
else:
|
|
|
|
type_ = "user"
|
|
|
|
|
|
|
|
peers[int(k)] = [int(k), int(v) if v is not None else None, type_, None, None]
|
|
|
|
|
|
|
|
for k, v in peers_by_phone.items():
|
|
|
|
peers[v][4] = k
|
|
|
|
|
|
|
|
# noinspection PyTypeChecker
|
|
|
|
self.update_peers(peers.values())
|
|
|
|
|
2019-09-14 18:35:59 +00:00
|
|
|
def update(self):
|
|
|
|
version = self.version()
|
|
|
|
|
|
|
|
if version == 1:
|
|
|
|
with self.lock, self.conn:
|
|
|
|
self.conn.execute("DELETE FROM peers")
|
|
|
|
|
|
|
|
version += 1
|
|
|
|
|
|
|
|
self.version(version)
|
|
|
|
|
2019-06-19 14:04:35 +00:00
|
|
|
def open(self):
|
2019-06-20 01:31:37 +00:00
|
|
|
path = self.database
|
|
|
|
file_exists = path.is_file()
|
|
|
|
|
|
|
|
if file_exists:
|
|
|
|
try:
|
2019-06-20 12:15:02 +00:00
|
|
|
with open(str(path), encoding="utf-8") as f:
|
2019-06-20 01:31:37 +00:00
|
|
|
session_json = json.load(f)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
2019-09-08 17:24:06 +00:00
|
|
|
log.warning("JSON session storage detected! Converting it into an SQLite session storage...")
|
2019-06-20 01:31:37 +00:00
|
|
|
|
|
|
|
path.rename(path.name + ".OLD")
|
|
|
|
|
2019-09-08 17:24:06 +00:00
|
|
|
log.warning('The old session file has been renamed to "{}.OLD"'.format(path.name))
|
2019-06-20 01:31:37 +00:00
|
|
|
|
|
|
|
self.migrate_from_json(session_json)
|
|
|
|
|
2019-09-08 17:24:06 +00:00
|
|
|
log.warning("Done! The session has been successfully converted from JSON to SQLite storage")
|
2019-06-20 01:31:37 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
if Path(path.name + ".OLD").is_file():
|
2019-09-08 17:24:06 +00:00
|
|
|
log.warning('Old session file detected: "{}.OLD". You can remove this file now'.format(path.name))
|
2019-06-19 14:04:35 +00:00
|
|
|
|
2019-09-14 17:56:07 +00:00
|
|
|
self.conn = sqlite3.connect(str(path), timeout=1, check_same_thread=False)
|
2019-06-19 14:04:35 +00:00
|
|
|
|
2019-06-20 01:31:37 +00:00
|
|
|
if not file_exists:
|
|
|
|
self.create()
|
2019-09-14 18:35:59 +00:00
|
|
|
else:
|
|
|
|
self.update()
|
2019-06-19 14:04:35 +00:00
|
|
|
|
2019-06-20 01:31:37 +00:00
|
|
|
with self.conn:
|
2019-09-09 13:45:19 +00:00
|
|
|
try: # Python 3.6.0 (exactly this version) is bugged and won't successfully execute the vacuum
|
|
|
|
self.conn.execute("VACUUM")
|
|
|
|
except sqlite3.OperationalError:
|
|
|
|
pass
|
2019-09-08 09:58:34 +00:00
|
|
|
|
2019-09-14 17:22:08 +00:00
|
|
|
def delete(self):
|
2019-09-08 09:58:34 +00:00
|
|
|
os.remove(self.database)
|