119 lines
3.3 KiB
Python
119 lines
3.3 KiB
Python
import os
|
|
|
|
from flask import Blueprint, jsonify, request, current_app, redirect
|
|
from werkzeug.utils import secure_filename
|
|
|
|
from app.extensions import db
|
|
from app.models.session import Session
|
|
from paper.errors import NeedPasswordException
|
|
from paper.parser import PaperParser
|
|
|
|
sessions = Blueprint("sessions", __name__, url_prefix="/sessions")
|
|
|
|
ALLOWED_EXTENSIONS = ['zip']
|
|
|
|
def allowed_file(filename):
|
|
return '.' in filename and \
|
|
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
|
|
async def create_session_form():
|
|
data = request.json
|
|
data["name"] = data["name"].replace("/", "").replace("\\", "")
|
|
|
|
session = Session.query.filter_by(name=data.get("name")).first()
|
|
|
|
if not session:
|
|
session = Session(
|
|
name=data.get("name"),
|
|
authorized=False
|
|
)
|
|
|
|
db.session.add(session)
|
|
|
|
response = {"status": "ok"}
|
|
|
|
async with PaperParser(data.get("name")) as parser:
|
|
try:
|
|
result = await parser.sign_in(**data)
|
|
|
|
if hasattr(result, "phone_code_hash"):
|
|
response = {
|
|
"status": "error",
|
|
"message": "Need secret code from telegram",
|
|
"action": "code",
|
|
"phone_hash": result.phone_code_hash,
|
|
}
|
|
else:
|
|
session.authorized = True
|
|
|
|
except NeedPasswordException:
|
|
response = {
|
|
"status": "error",
|
|
"message": "Need 2FA Password",
|
|
"action": "password",
|
|
}
|
|
|
|
except Exception as e:
|
|
response = {
|
|
"status": "exception",
|
|
"message": str(e),
|
|
}
|
|
|
|
finally:
|
|
db.session.commit()
|
|
|
|
return jsonify(response)
|
|
|
|
def create_session_from_file():
|
|
archive = request.files['archive']
|
|
|
|
if archive.filename == '':
|
|
return 'Not Found', 404
|
|
|
|
if not allowed_file(archive.filename):
|
|
return 'File Extension Not Allowed', 406
|
|
|
|
session_upload_path = os.path.join(current_app.config['UPLOAD_DIR'], "sessions")
|
|
|
|
if not os.path.exists(session_upload_path):
|
|
os.makedirs(session_upload_path)
|
|
|
|
filename = secure_filename(archive.filename)
|
|
file_path = os.path.join(session_upload_path, filename)
|
|
|
|
archive.save(file_path)
|
|
|
|
return 'Ok', 200
|
|
# return redirect('/')
|
|
|
|
@sessions.route("/", methods=["POST"])
|
|
async def create_session():
|
|
files = request.files
|
|
if not 'archive' in files:
|
|
return await create_session_form()
|
|
else:
|
|
return create_session_from_file()
|
|
|
|
@sessions.route("/", methods=["GET"])
|
|
def get_sessions(**kwargs):
|
|
return Session.query.filter_by(authorized=True).all()
|
|
|
|
@sessions.route("/<int:id>", methods=["DELETE"])
|
|
async def remove_session(id: int):
|
|
session: Session = Session.query.get_or_404(id)
|
|
|
|
if session.has_depending_tasks():
|
|
return 'Есть зависимые задачи', 409
|
|
|
|
async with PaperParser(session.name) as parser:
|
|
try:
|
|
await parser.client.log_out()
|
|
|
|
db.session.delete(session)
|
|
db.session.commit()
|
|
|
|
except Exception as e:
|
|
return str(e), 500
|
|
|
|
return 'ok', 200
|
|
|