diff --git "a/app.py" "b/app.py" --- "a/app.py" +++ "b/app.py" @@ -1,1871 +1,1010 @@ -import json +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + import os -import logging -import threading -import time -from datetime import datetime -from flask import Flask, render_template_string, request, redirect, url_for, session, flash, send_file, jsonify, Response -from flask_caching import Cache -from huggingface_hub import HfApi, hf_hub_download, utils as hf_utils -from werkzeug.utils import secure_filename -import requests -from io import BytesIO -import uuid +from flask import Flask, request, Response, render_template_string, jsonify, redirect, url_for, make_response import hmac import hashlib -from urllib.parse import unquote, parse_qs +import json +from urllib.parse import unquote, parse_qs, quote +import time +from datetime import datetime +import logging +import threading +from huggingface_hub import HfApi, hf_hub_download, list_repo_files +from huggingface_hub.utils import RepositoryNotFoundError, EntryNotFoundError +import io # --- Configuration --- -app = Flask(__name__) -app.secret_key = os.getenv("FLASK_SECRET_KEY", "supersecretkey_folders_unique_tma") -BOT_TOKEN = "6750208873:AAE2hvPlJ99dBdhGa_Brre0IIpUdOvXxHt4" # Your Telegram Bot Token -DATA_FILE = 'cloudeng_data_tma.json' -REPO_ID = "Eluza133/Z1e1u" # Your Hugging Face Repo ID -HF_TOKEN_WRITE = os.getenv("HF_TOKEN") -HF_TOKEN_READ = os.getenv("HF_TOKEN_READ") or HF_TOKEN_WRITE -ADMIN_TELEGRAM_IDS_STR = os.getenv("ADMIN_TELEGRAM_IDS", "") # Comma-separated list of admin Telegram IDs -ADMIN_TELEGRAM_IDS = set(int(tid.strip()) for tid in ADMIN_TELEGRAM_IDS_STR.split(',') if tid.strip().isdigit()) -UPLOAD_FOLDER = 'uploads_tma' -os.makedirs(UPLOAD_FOLDER, exist_ok=True) - -cache = Cache(app, config={'CACHE_TYPE': 'simple'}) -logging.basicConfig(level=logging.INFO) +BOT_TOKEN = os.getenv("BOT_TOKEN", "6750208873:AAE2hvPlJ99dBdhGa_Brre0IIpUdOvXxHt4") +HOST = '0.0.0.0' +PORT = 7860 -# --- Helper Functions --- - -def find_node_by_id(filesystem, node_id): - if not filesystem or not isinstance(filesystem, dict): - return None, None - if filesystem.get('id') == node_id: - return filesystem, None - - queue = [(filesystem, None)] - while queue: - current_node, parent = queue.pop(0) - if current_node.get('type') == 'folder' and 'children' in current_node: - for i, child in enumerate(current_node.get('children', [])): - if not isinstance(child, dict): continue # Skip invalid children - if child.get('id') == node_id: - return child, current_node - if child.get('type') == 'folder': - queue.append((child, current_node)) - return None, None - -def add_node(filesystem, parent_id, node_data): - parent_node, _ = find_node_by_id(filesystem, parent_id) - if parent_node and parent_node.get('type') == 'folder': - if 'children' not in parent_node or not isinstance(parent_node['children'], list): - parent_node['children'] = [] - parent_node['children'].append(node_data) - return True - return False - -def remove_node(filesystem, node_id): - node_to_remove, parent_node = find_node_by_id(filesystem, node_id) - if node_to_remove and parent_node and 'children' in parent_node and isinstance(parent_node['children'], list): - parent_node['children'] = [child for child in parent_node['children'] if isinstance(child, dict) and child.get('id') != node_id] - return True - return False - -def get_node_path_string(filesystem, node_id): - path_list = [] - current_id = node_id - - while current_id: - node, parent = find_node_by_id(filesystem, current_id) - if not node: - break - if node.get('id') != 'root': - path_list.append(node.get('name', node.get('original_filename', ''))) - if not parent: - break - current_id = parent.get('id') if parent else None - - return " / ".join(reversed(path_list)) or "Root" - - -def initialize_user_filesystem(user_data): - # user_data is already specific to one user - if 'filesystem' not in user_data or not isinstance(user_data.get('filesystem'), dict): - user_data['filesystem'] = { - "type": "folder", - "id": "root", - "name": "root", - "children": [] - } - # Migration logic (optional, based on old structure if needed) - if 'files' in user_data and isinstance(user_data['files'], list): - telegram_id = user_data.get('telegram_id') # Assuming telegram_id is stored here - if telegram_id: - for old_file in user_data['files']: - file_id = old_file.get('id', uuid.uuid4().hex) - original_filename = old_file.get('filename', 'unknown_file') - name_part, ext_part = os.path.splitext(original_filename) - unique_suffix = uuid.uuid4().hex[:8] - unique_filename = f"{name_part}_{unique_suffix}{ext_part}" - hf_path = f"cloud_files/{str(telegram_id)}/root/{unique_filename}" # Use telegram_id - - file_node = { - 'type': 'file', - 'id': file_id, - 'original_filename': original_filename, - 'unique_filename': unique_filename, - 'path': hf_path, - 'file_type': get_file_type(original_filename), - 'upload_date': old_file.get('upload_date', datetime.now().strftime('%Y-%m-%d %H:%M:%S')) - } - add_node(user_data['filesystem'], 'root', file_node) - del user_data['files'] # Remove old structure - -@cache.memoize(timeout=300) -def load_data(): - try: - download_db_from_hf() - with open(DATA_FILE, 'r', encoding='utf-8') as file: - try: - data = json.load(file) - except json.JSONDecodeError: - logging.error(f"Error decoding JSON from {DATA_FILE}. Initializing empty database.") - return {'users': {}} - - if not isinstance(data, dict): - logging.warning("Data is not in dict format, initializing empty database") - return {'users': {}} - - # Ensure 'users' key exists and is a dictionary - if 'users' not in data or not isinstance(data['users'], dict): - logging.warning("Corrupted or missing 'users' structure, re-initializing.") - data['users'] = {} - - # Convert keys to integers (Telegram IDs) and initialize filesystem - converted_users = {} - for user_id_str, user_data in data['users'].items(): - try: - user_id_int = int(user_id_str) - if isinstance(user_data, dict): - initialize_user_filesystem(user_data) # Ensure filesystem exists - converted_users[user_id_int] = user_data - else: - logging.warning(f"Skipping invalid user data for key {user_id_str}") - except ValueError: - logging.warning(f"Skipping non-integer user ID key: {user_id_str}") - - data['users'] = converted_users - logging.info("Data successfully loaded and initialized") - return data - except FileNotFoundError: - logging.warning(f"{DATA_FILE} not found. Initializing empty database.") - return {'users': {}} - except Exception as e: - logging.error(f"Error loading data: {e}") - return {'users': {}} +# Hugging Face Settings +REPO_ID = "Eluza133/Z1e1u" +HF_UPLOAD_FOLDER = "uploads" # Base folder for user uploads within the HF repo +HF_TOKEN_WRITE = os.getenv("HF_TOKEN_WRITE") # Token with write access +HF_TOKEN_READ = os.getenv("HF_TOKEN_READ") # Token with read access (can be same as write) +app = Flask(__name__) +logging.basicConfig(level=logging.INFO) +app.secret_key = os.urandom(24) + +# --- Hugging Face API Initialization --- +hf_api = None +if HF_TOKEN_WRITE: + hf_api = HfApi(token=HF_TOKEN_WRITE) + logging.info("Hugging Face API initialized with WRITE token.") +elif HF_TOKEN_READ: + hf_api = HfApi(token=HF_TOKEN_READ) + logging.info("Hugging Face API initialized with READ token (Uploads disabled).") +else: + logging.warning("HF_TOKEN_WRITE and HF_TOKEN_READ not set. Hugging Face operations will fail.") + + +# --- Telegram Verification --- +def verify_telegram_data(init_data_str): + if not init_data_str: + logging.warning("Verification attempt with empty initData.") + return None, False, "Missing initData" -def save_data(data): - try: - # Ensure all user keys are strings before saving to JSON - string_key_users = {str(k): v for k, v in data.get('users', {}).items()} - data_to_save = {'users': string_key_users} - - with open(DATA_FILE, 'w', encoding='utf-8') as file: - json.dump(data_to_save, file, ensure_ascii=False, indent=4) - upload_db_to_hf() - cache.clear() - logging.info("Data saved and uploaded to HF") - except Exception as e: - logging.error(f"Error saving data: {e}") - raise - -def upload_db_to_hf(): - if not HF_TOKEN_WRITE: - logging.warning("HF_TOKEN_WRITE not set, skipping database upload.") - return - try: - api = HfApi() - api.upload_file( - path_or_fileobj=DATA_FILE, - path_in_repo=DATA_FILE, - repo_id=REPO_ID, - repo_type="dataset", - token=HF_TOKEN_WRITE, - commit_message=f"Backup {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}" - ) - logging.info("Database uploaded to Hugging Face") - except Exception as e: - logging.error(f"Error uploading database: {e}") - -def download_db_from_hf(): - if not HF_TOKEN_READ: - logging.warning("HF_TOKEN_READ not set, skipping database download.") - if not os.path.exists(DATA_FILE): - with open(DATA_FILE, 'w', encoding='utf-8') as f: - json.dump({'users': {}}, f) - return - try: - hf_hub_download( - repo_id=REPO_ID, - filename=DATA_FILE, - repo_type="dataset", - token=HF_TOKEN_READ, - local_dir=".", - local_dir_use_symlinks=False, - force_filename=DATA_FILE # Ensure correct filename - ) - logging.info("Database downloaded from Hugging Face") - except hf_utils.RepositoryNotFoundError: - logging.error(f"Repository {REPO_ID} not found.") - if not os.path.exists(DATA_FILE): - with open(DATA_FILE, 'w', encoding='utf-8') as f: - json.dump({'users': {}}, f) - except hf_utils.EntryNotFoundError: - logging.warning(f"{DATA_FILE} not found in repository {REPO_ID}. Initializing empty database.") - if not os.path.exists(DATA_FILE): - with open(DATA_FILE, 'w', encoding='utf-8') as f: - json.dump({'users': {}}, f) - except Exception as e: - logging.error(f"Error downloading database: {e}") - if not os.path.exists(DATA_FILE): - with open(DATA_FILE, 'w', encoding='utf-8') as f: - json.dump({'users': {}}, f) - -def periodic_backup(): - while True: - time.sleep(1800) # Backup every 30 minutes - logging.info("Starting periodic backup...") - try: - # Ensure data is loaded before saving (important if app restarts) - current_data = load_data() - save_data(current_data) - except Exception as e: - logging.error(f"Error during periodic backup: {e}") - - -def get_file_type(filename): - filename_lower = filename.lower() - if filename_lower.endswith(('.mp4', '.mov', '.avi', '.webm', '.mkv')): - return 'video' - elif filename_lower.endswith(('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.svg')): - return 'image' - elif filename_lower.endswith('.pdf'): - return 'pdf' - elif filename_lower.endswith('.txt'): - return 'text' - return 'other' - -def verify_telegram_auth(init_data_str, bot_token): try: parsed_data = parse_qs(init_data_str) - received_hash = parsed_data.get('hash', [None])[0] + received_hash = parsed_data.pop('hash', [None])[0] if not received_hash: - logging.warning("Hash missing in initData") - return None + logging.warning("Verification failed: Hash missing from initData.") + return None, False, "Hash missing" - data_check_string_parts = [] + data_check_list = [] for key, value in sorted(parsed_data.items()): - if key != 'hash': - # Values are lists from parse_qs, take the first element - data_check_string_parts.append(f"{key}={value[0]}") - - data_check_string = "\n".join(data_check_string_parts) + # Make sure values are handled correctly, especially if multiple exist (though unlikely for standard fields) + data_check_list.append(f"{key}={value[0]}") + data_check_string = "\n".join(data_check_list) - secret_key = hmac.new("WebAppData".encode(), bot_token.encode(), hashlib.sha256).digest() + secret_key = hmac.new("WebAppData".encode(), BOT_TOKEN.encode(), hashlib.sha256).digest() calculated_hash = hmac.new(secret_key, data_check_string.encode(), hashlib.sha256).hexdigest() if calculated_hash == received_hash: - user_data_json = parsed_data.get('user', [None])[0] - if user_data_json: + auth_date = int(parsed_data.get('auth_date', [0])[0]) + current_time = int(time.time()) + # Check if data is reasonably fresh (e.g., within 24 hours) + if current_time - auth_date > 86400: + logging.warning(f"Verification Warning: Telegram InitData is older than 24 hours (Auth Date: {auth_date}, Current: {current_time}). Allowing access.") + # return parsed_data, False, "Data expired" # Uncomment to enforce expiry + + user_data = None + if 'user' in parsed_data: try: - # Decode URL-encoded JSON string - user_data = json.loads(unquote(user_data_json)) - if 'id' in user_data: - logging.info(f"Telegram auth successful for user ID: {user_data['id']}") - return user_data - else: - logging.error("User ID missing in user data") - return None - except (json.JSONDecodeError, KeyError) as e: - logging.error(f"Error parsing user data from initData: {e}") - return None + user_json_str = unquote(parsed_data['user'][0]) + user_data = json.loads(user_json_str) + except Exception as e: + logging.error(f"Could not parse user JSON from initData: {e}") + return None, False, "User data parsing failed" else: - logging.error("User data missing in initData") - return None - else: - logging.warning(f"Hash mismatch. Calculated: {calculated_hash}, Received: {received_hash}") - return None + logging.warning("User data missing in parsed initData.") + return None, False, "User data missing" + + if not user_data or 'id' not in user_data: + logging.error("Verification failed: User ID missing after parsing.") + return None, False, "User ID missing" + logging.info(f"Verification successful for user ID: {user_data.get('id')}") + return user_data, True, "Verified" + else: + logging.warning(f"Verification failed: Hash mismatch. User: {parsed_data.get('user')}") + return None, False, "Invalid hash" except Exception as e: - logging.error(f"Exception during Telegram auth verification: {e}") - return None - -def is_admin(): - # Check if the logged-in user's Telegram ID is in the admin list - return 'telegram_id' in session and session['telegram_id'] in ADMIN_TELEGRAM_IDS - - -# --- HTML / CSS / JS --- - -BASE_STYLE = ''' -:root { - --primary: #ff4d6d; --secondary: #00ddeb; --accent: #8b5cf6; - --background-light: #f5f6fa; --background-dark: #1a1625; - --card-bg: rgba(255, 255, 255, 0.95); --card-bg-dark: rgba(40, 35, 60, 0.95); - --text-light: #2a1e5a; --text-dark: #e8e1ff; --shadow: 0 10px 30px rgba(0, 0, 0, 0.2); - --glass-bg: rgba(255, 255, 255, 0.15); --transition: all 0.3s ease; --delete-color: #ff4444; - --folder-color: #ffc107; - /* Telegram Theme Integration */ - --tg-theme-bg-color: var(--background-light); - --tg-theme-text-color: var(--text-light); - --tg-theme-hint-color: #aaa; - --tg-theme-link-color: var(--accent); - --tg-theme-button-color: var(--primary); - --tg-theme-button-text-color: #ffffff; - --tg-theme-secondary-bg-color: var(--card-bg); -} -html.dark { - --tg-theme-bg-color: var(--background-dark); - --tg-theme-text-color: var(--text-dark); - --tg-theme-hint-color: #777; - --tg-theme-link-color: var(--accent); - --tg-theme-button-color: var(--primary); - --tg-theme-button-text-color: #ffffff; - --tg-theme-secondary-bg-color: var(--card-bg-dark); -} - -* { margin: 0; padding: 0; box-sizing: border-box; } -body { - font-family: 'Inter', sans-serif; - background-color: var(--tg-theme-bg-color); - color: var(--tg-theme-text-color); - line-height: 1.6; - transition: background-color 0.3s ease, color 0.3s ease; -} -.container { margin: 10px auto; max-width: 1200px; padding: 15px; background: var(--tg-theme-secondary-bg-color); border-radius: 15px; box-shadow: var(--shadow); overflow-x: hidden; } -h1 { font-size: 1.8em; font-weight: 800; text-align: center; margin-bottom: 20px; background: linear-gradient(135deg, var(--primary), var(--accent)); -webkit-background-clip: text; color: transparent; } -h2 { font-size: 1.4em; margin-top: 25px; color: var(--tg-theme-text-color); } -h4 { font-size: 1.1em; margin-top: 15px; margin-bottom: 5px; color: var(--accent); } -ol, ul { margin-left: 20px; margin-bottom: 15px; } -li { margin-bottom: 5px; } -input, textarea { width: 100%; padding: 12px; margin: 10px 0; border: none; border-radius: 12px; background: var(--glass-bg); color: var(--tg-theme-text-color); font-size: 1em; box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.1); } -input:focus, textarea:focus { outline: none; box-shadow: 0 0 0 3px var(--primary); } -.btn { padding: 12px 24px; background: var(--tg-theme-button-color); color: var(--tg-theme-button-text-color); border: none; border-radius: 12px; cursor: pointer; font-size: 1em; font-weight: 600; transition: var(--transition); box-shadow: var(--shadow); display: inline-block; text-decoration: none; margin-top: 5px; margin-right: 5px; } -.btn:hover { transform: scale(1.03); filter: brightness(1.1); } -.download-btn { background: var(--secondary); color: white; } -.download-btn:hover { background: #00b8c5; } -.delete-btn { background: var(--delete-color); color: white; } -.delete-btn:hover { background: #cc3333; } -.folder-btn { background: var(--folder-color); color: white; } -.folder-btn:hover { background: #e6a000; } -.flash { color: var(--secondary); text-align: center; margin-bottom: 15px; padding: 10px; background: rgba(0, 221, 235, 0.1); border-radius: 10px; } -.flash.error { color: var(--delete-color); background: rgba(255, 68, 68, 0.1); } -.file-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 15px; margin-top: 20px; } -.user-list { margin-top: 20px; } -.user-item { padding: 15px; background: var(--tg-theme-secondary-bg-color); border-radius: 16px; margin-bottom: 10px; box-shadow: var(--shadow); transition: var(--transition); } -.user-item:hover { transform: translateY(-5px); } -.user-item a { color: var(--tg-theme-link-color); text-decoration: none; font-weight: 600; } -.user-item a:hover { filter: brightness(1.2); } -.item { background: var(--tg-theme-secondary-bg-color); padding: 10px; border-radius: 12px; box-shadow: var(--shadow); text-align: center; transition: var(--transition); display: flex; flex-direction: column; justify-content: space-between; } -.item:hover { transform: translateY(-3px); } -.item-preview { max-width: 100%; height: 100px; object-fit: cover; border-radius: 8px; margin-bottom: 8px; cursor: pointer; display: block; margin-left: auto; margin-right: auto;} -.item.folder .item-preview { object-fit: contain; font-size: 50px; color: var(--folder-color); line-height: 100px; } -.item p { font-size: 0.85em; margin: 3px 0; word-break: break-all; } -.item a { color: var(--tg-theme-link-color); text-decoration: none; } -.item a:hover { filter: brightness(1.2); } -.item-actions { margin-top: 8px; display: flex; flex-wrap: wrap; gap: 5px; justify-content: center; } -.item-actions .btn { font-size: 0.8em; padding: 5px 8px; } -.modal { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.85); z-index: 2000; justify-content: center; align-items: center; } -.modal-content { max-width: 95%; max-height: 95%; background: var(--tg-theme-secondary-bg-color); padding: 10px; border-radius: 15px; overflow: auto; position: relative; } -.modal img, .modal video, .modal iframe, .modal pre { max-width: 100%; max-height: 85vh; display: block; margin: auto; border-radius: 10px; } -.modal iframe { width: 90vw; height: 85vh; border: none; } -.modal pre { background: rgba(0,0,0,0.1); color: var(--tg-theme-text-color); padding: 15px; border-radius: 8px; white-space: pre-wrap; word-wrap: break-word; text-align: left; max-height: 85vh; overflow-y: auto;} -.modal-close-btn { position: absolute; top: 10px; right: 15px; font-size: 24px; color: var(--tg-theme-hint-color); cursor: pointer; background: rgba(0,0,0,0.3); border-radius: 50%; width: 25px; height: 25px; line-height: 25px; text-align: center; } -#progress-container { width: 100%; background: var(--glass-bg); border-radius: 10px; margin: 15px 0; display: none; position: relative; height: 20px; } -#progress-bar { width: 0%; height: 100%; background: var(--tg-theme-button-color); border-radius: 10px; transition: width 0.3s ease; } -#progress-text { position: absolute; width: 100%; text-align: center; line-height: 20px; color: var(--tg-theme-button-text-color); font-size: 0.9em; font-weight: bold; text-shadow: 1px 1px 1px rgba(0,0,0,0.5); } -.breadcrumbs { margin-bottom: 15px; font-size: 1em; color: var(--tg-theme-hint-color); } -.breadcrumbs a { color: var(--tg-theme-link-color); text-decoration: none; } -.breadcrumbs a:hover { text-decoration: underline; } -.breadcrumbs span { margin: 0 5px; } -.folder-actions { margin-top: 15px; margin-bottom: 10px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; } -.folder-actions input[type=text] { width: auto; flex-grow: 1; margin: 0; min-width: 150px; } -.folder-actions .btn { margin: 0; flex-shrink: 0;} -.auth-container { text-align: center; padding: 50px 20px; } -.auth-container p { margin-bottom: 20px; font-size: 1.1em; } -.spinner { border: 4px solid rgba(0, 0, 0, 0.1); width: 36px; height: 36px; border-radius: 50%; border-left-color: var(--tg-theme-button-color); animation: spin 1s ease infinite; margin: 20px auto; } -@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } -@media (max-width: 768px) { - .file-grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); } - .folder-actions { flex-direction: column; align-items: stretch; } - .folder-actions input[type=text] { width: 100%; } - .item-preview { height: 80px; } - .item.folder .item-preview { font-size: 40px; line-height: 80px; } - h1 { font-size: 1.6em; } - .btn { padding: 10px 20px; font-size: 0.9em; } - .item-actions .btn { padding: 4px 8px; font-size: 0.75em;} -} -''' - -INITIAL_AUTH_HTML = ''' -
-Инициализация и проверка авторизации...
- - -Пользователь: {{ user_info.get('first_name', 'Неизвестно') }} {{ user_info.get('last_name', '') }} (ID: {{ telegram_id }})
-{% with messages = get_flashed_messages(with_categories=true) %} - {% if messages %} - {% for category, message in messages %} -{{ item.name }}
-{{ item.original_filename | truncate(25, True) }}
-{{ item.upload_date }}
-Эта папка пуста.
{% endif %} -Зарегистрирован: {{ user.created_at }}
-Файлов: {{ user.file_count }}
- -Пользователей нет.
{% endfor %}{{ file.original_filename | truncate(30) }}
-В папке: {{ file.parent_path_str }}
-Загружен: {{ file.upload_date }}
-ID: {{ file.id }}
-Path: {{ file.path }}
-У п��льзователя нет файлов.
{% endfor %} -