Spaces:
Paused
Paused
File size: 24,029 Bytes
aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df 3dd7b99 aa342df |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 |
import os
import time
import threading
import requests
import logging
import secrets
from functools import wraps
from datetime import datetime
from flask import Flask, render_template_string, request, jsonify, session, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
# ================= 配置区域 =================
DEFAULT_DB_URI = 'postgresql://postgres:password@192.168.1.10:5432/alist_sync'
DB_URI = os.environ.get('DB_URI', DEFAULT_DB_URI)
# 兼容性修复
if DB_URI and DB_URI.startswith("postgres://"):
DB_URI = DB_URI.replace("postgres://", "postgresql://", 1)
PORT = int(os.environ.get('PORT', 5000))
# ===========================================
app = Flask(__name__)
app.secret_key = os.environ.get('SECRET_KEY', secrets.token_hex(16))
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {'pool_pre_ping': True}
db = SQLAlchemy(app)
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
logger = logging.getLogger(__name__)
# --- 数据库模型 ---
class AppConfig(db.Model):
__tablename__ = 'config'
id = db.Column(db.Integer, primary_key=True)
url = db.Column(db.String(255), default="http://localhost:5244")
username = db.Column(db.String(255), default="admin")
password = db.Column(db.String(255), default="")
path1 = db.Column(db.String(500), default="")
path2 = db.Column(db.String(500), default="")
interval = db.Column(db.Integer, default=60)
auto_sync = db.Column(db.Boolean, default=False)
web_password = db.Column(db.String(255), default="123456")
class AppLog(db.Model):
__tablename__ = 'logs'
id = db.Column(db.Integer, primary_key=True)
timestamp = db.Column(db.DateTime, default=datetime.now)
message = db.Column(db.Text)
# --- 装饰器与逻辑 ---
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if 'logged_in' not in session:
return redirect(url_for('login'))
return f(*args, **kwargs)
return decorated_function
def add_log(msg):
print(f"[Log] {msg}")
try:
with app.app_context():
log = AppLog(message=str(msg), timestamp=datetime.now())
db.session.add(log)
db.session.commit()
# 自动清理日志
last = db.session.query(db.func.max(AppLog.id)).scalar()
if last and last > 1000:
AppLog.query.filter(AppLog.id < (last - 500)).delete()
db.session.commit()
except:
pass
def get_token(base_url, username, password):
try:
res = requests.post(f"{base_url.rstrip('/')}/api/auth/login",
json={'username': username, 'password': password}, timeout=10)
if res.status_code == 200:
return res.json().get('data', {}).get('token')
except Exception as e:
add_log(f"登录失败: {e}")
return None
def list_files(base_url, token, path):
try:
res = requests.post(f"{base_url.rstrip('/')}/api/fs/list",
headers={'Authorization': token},
json={"path": path, "password": "", "page": 1, "per_page": 0, "refresh": True}, timeout=30)
if res.status_code == 200:
return res.json().get('data', {}).get('content', [])
except Exception as e:
add_log(f"获取列表失败: {e}")
return []
def copy_files_api(base_url, token, src_dir, dst_dir, file_names):
try:
res = requests.post(f"{base_url.rstrip('/')}/api/fs/copy",
headers={'Authorization': token},
json={"src_dir": src_dir, "dst_dir": dst_dir, "names": file_names}, timeout=30)
return res.json()
except:
return {"code": 500}
# --- 后台任务 ---
def background_worker():
while True:
try:
with app.app_context():
cfg = AppConfig.query.first()
if not cfg:
db.session.add(AppConfig(web_password="123456"))
db.session.commit()
continue
if cfg.auto_sync:
interval = max(1, cfg.interval)
token = get_token(cfg.url, cfg.username, cfg.password)
if token:
src = list_files(cfg.url, token, cfg.path1)
dst = list_files(cfg.url, token, cfg.path2)
if src:
dst_names = {f['name'] for f in dst}
missing = [i['name'] for i in src if i['name'] not in dst_names]
if missing:
add_log(f"自动同步: 发现 {len(missing)} 个文件,开始复制")
for i in range(0, len(missing), 20):
copy_files_api(cfg.url, token, cfg.path1, cfg.path2, missing[i:i+20])
time.sleep(interval * 60)
else:
time.sleep(10)
except Exception as e:
print(f"后台错误: {e}")
time.sleep(60)
# ==========================================
# Material 3 风格前端模板
# ==========================================
# 通用头部,包含 Material Web 组件加载
HEAD_COMMON = """
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Alist Sync Panel</title>
<!-- Roboto Font & Icons -->
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0" rel="stylesheet">
<!-- Material Web Components (ESM) -->
<script type="importmap">
{
"imports": {
"@material/web/": "https://esm.run/@material/web/"
}
}
</script>
<script type="module">
import '@material/web/all.js';
import {styles as typescaleStyles} from '@material/web/typography/md-typescale-styles.js';
document.adoptedStyleSheets.push(typescaleStyles.styleSheet);
</script>
<style>
:root {
/* M3 Color Tokens (Baseline Purple) */
--md-sys-color-primary: #6750A4;
--md-sys-color-on-primary: #FFFFFF;
--md-sys-color-primary-container: #EADDFF;
--md-sys-color-on-primary-container: #21005D;
--md-sys-color-secondary: #625B71;
--md-sys-color-surface: #FEF7FF;
--md-sys-color-surface-container: #F3EDF7;
--md-sys-color-on-surface: #1D1B20;
--md-sys-color-outline: #79747E;
--md-sys-color-error: #B3261E;
--md-ref-typeface-plain: 'Roboto', sans-serif;
}
body {
font-family: var(--md-ref-typeface-plain);
background-color: var(--md-sys-color-surface);
color: var(--md-sys-color-on-surface);
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
height: 100vh;
overflow: hidden;
}
/* Utilities */
.d-flex { display: flex; }
.flex-column { flex-direction: column; }
.align-center { align-items: center; }
.justify-center { justify_content: center; }
.gap-1 { gap: 8px; }
.gap-2 { gap: 16px; }
.w-100 { width: 100%; }
.mt-2 { margin-top: 16px; }
/* Card Style using CSS (Lightweight) */
.m3-card {
background: var(--md-sys-color-surface-container);
border-radius: 16px;
padding: 24px;
box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);
}
/* Scrollbar */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #ccc; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #aaa; }
</style>
</head>
"""
LOGIN_HTML = HEAD_COMMON + """
<body>
<div style="display:flex; justify-content:center; align-items:center; height:100%; width:100%;">
<div class="m3-card" style="width: 100%; max-width: 400px; display:flex; flex-direction:column; gap:24px;">
<div style="text-align: center;">
<span class="material-symbols-outlined" style="font-size: 48px; color: var(--md-sys-color-primary);">sync_lock</span>
<h1 class="md-typescale-headline-small" style="margin: 8px 0;">Alist Sync</h1>
<p class="md-typescale-body-medium" style="color: var(--md-sys-color-secondary); margin:0;">请输入面板访问密码</p>
</div>
{% if error %}
<div style="color: var(--md-sys-color-error); background: #f9dedc; padding: 10px; border-radius: 8px; font-size: 14px; display:flex; align-items:center; gap:8px;">
<span class="material-symbols-outlined" style="font-size:18px;">error</span> {{ error }}
</div>
{% endif %}
<form method="post" style="display:flex; flex-direction:column; gap:16px;">
<md-outlined-text-field
label="Password"
type="password"
name="password"
required
style="width: 100%;">
<md-icon slot="leading-icon">key</md-icon>
</md-outlined-text-field>
<md-filled-button type="submit" style="width: 100%;">
登 录
</md-filled-button>
</form>
<div style="text-align:center;">
<span class="md-typescale-label-small" style="color: var(--md-sys-color-outline);">Default: 123456</span>
</div>
</div>
</div>
</body>
</html>
"""
INDEX_HTML = HEAD_COMMON + """
<body style="overflow-y: auto;">
<!-- App Bar -->
<header style="background: var(--md-sys-color-surface-container); padding: 16px 24px; display:flex; justify-content:space-between; align-items:center; position:sticky; top:0; z-index:1000; box-shadow: 0 2px 4px rgba(0,0,0,0.05);">
<div class="d-flex align-center gap-2">
<span class="material-symbols-outlined" style="color: var(--md-sys-color-primary); font-size: 28px;">cloud_sync</span>
<span class="md-typescale-title-large">Alist Sync Panel</span>
</div>
<div>
<md-text-button href="/logout" onclick="window.location.href='/logout'">
<md-icon slot="icon">logout</md-icon>
退出
</md-text-button>
</div>
</header>
<main style="padding: 24px; max-width: 1200px; margin: 0 auto; width: 100%; box-sizing: border-box; display: grid; grid-template-columns: repeat(auto-fit, minmax(350px, 1fr)); gap: 24px;">
<!-- Config Card -->
<div class="m3-card" style="grid-column: span 2;">
<div class="d-flex align-center gap-2" style="margin-bottom: 24px;">
<md-icon style="color: var(--md-sys-color-primary);">settings</md-icon>
<span class="md-typescale-title-medium">同步配置</span>
</div>
<form id="configForm" style="display: grid; grid-template-columns: 1fr 1fr; gap: 16px;">
<!-- Full Width -->
<md-outlined-text-field label="Alist URL" id="url" style="grid-column: span 2;">
<md-icon slot="leading-icon">link</md-icon>
</md-outlined-text-field>
<!-- Half Width -->
<md-outlined-text-field label="Username" id="username">
<md-icon slot="leading-icon">person</md-icon>
</md-outlined-text-field>
<md-outlined-text-field label="Password" id="password" type="password">
<md-icon slot="leading-icon">lock</md-icon>
</md-outlined-text-field>
<div style="grid-column: span 2; height: 1px; background: var(--md-sys-color-outline); opacity: 0.2; margin: 8px 0;"></div>
<md-outlined-text-field label="Source Path (源路径)" id="path1" placeholder="/阿里云盘/Video">
<md-icon slot="leading-icon">folder_open</md-icon>
</md-outlined-text-field>
<md-outlined-text-field label="Target Path (目标路径)" id="path2" placeholder="/本地存储/Video">
<md-icon slot="leading-icon">save_alt</md-icon>
</md-outlined-text-field>
<div class="d-flex align-center gap-2" style="grid-column: span 2; margin-top: 16px; background: rgba(0,0,0,0.03); padding: 16px; border-radius: 12px; justify-content: space-between;">
<div class="d-flex align-center gap-2">
<md-outlined-text-field label="间隔 (分钟)" id="interval" type="number" style="width: 120px;" value="60"></md-outlined-text-field>
<label style="display:flex; align-items:center; gap:12px; cursor:pointer;">
<md-switch id="auto_sync" icons></md-switch>
<span class="md-typescale-body-large">开启后台自动同步</span>
</label>
</div>
<md-filled-button type="button" id="saveBtn">
<md-icon slot="icon">save</md-icon>
保存并应用
</md-filled-button>
</div>
</form>
</div>
<!-- Status & Security Card -->
<div class="d-flex flex-column gap-2">
<!-- Status -->
<div class="m3-card" style="flex: 1;">
<div class="d-flex align-center gap-2" style="margin-bottom: 16px;">
<md-icon style="color: var(--md-sys-color-primary);">analytics</md-icon>
<span class="md-typescale-title-medium">运行状态</span>
</div>
<div style="display:flex; flex-direction:column; align-items:center; justify-content:center; height: 120px;">
<div id="status-indicator" style="display:flex; align-items:center; gap:8px; margin-bottom:8px;">
<md-icon id="status-icon" style="font-size: 48px;">stop_circle</md-icon>
</div>
<span id="status-text" class="md-typescale-headline-small">已停止</span>
</div>
</div>
<!-- Security -->
<div class="m3-card">
<div class="d-flex align-center gap-2" style="margin-bottom: 16px;">
<md-icon style="color: var(--md-sys-color-primary);">security</md-icon>
<span class="md-typescale-title-medium">修改面板密码</span>
</div>
<div class="d-flex gap-2">
<md-outlined-text-field label="新密码" id="new_pass" type="password" style="flex:1;"></md-outlined-text-field>
<md-tonal-button id="changePassBtn">
修改
</md-tonal-button>
</div>
</div>
</div>
<!-- Logs Card -->
<div class="m3-card" style="grid-column: span 2; display: flex; flex-direction: column; height: 500px;">
<div class="d-flex align-center justify-between" style="margin-bottom: 16px; justify-content: space-between;">
<div class="d-flex align-center gap-2">
<md-icon style="color: var(--md-sys-color-primary);">terminal</md-icon>
<span class="md-typescale-title-medium">系统日志</span>
</div>
<md-icon-button id="refreshLogBtn">
<md-icon>refresh</md-icon>
</md-icon-button>
</div>
<div id="logContainer" style="flex: 1; background: #1e1e1e; border-radius: 8px; padding: 16px; overflow-y: auto; font-family: 'Roboto Mono', monospace; font-size: 13px; color: #00e676;">
加载中...
</div>
</div>
</main>
<!-- Toast Notification -->
<div id="toast" style="position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--md-sys-color-on-surface); color: var(--md-sys-color-surface); padding: 12px 24px; border-radius: 24px; opacity: 0; transition: opacity 0.3s; pointer-events: none; z-index: 2000; box-shadow: 0 4px 8px rgba(0,0,0,0.2);">
Notification
</div>
<script>
// Helper for Toast
function showToast(msg) {
const t = document.getElementById('toast');
t.textContent = msg;
t.style.opacity = '1';
setTimeout(() => t.style.opacity = '0', 3000);
}
// API Helpers
async function fetchAPI(url, method='GET', data=null) {
const opts = { method: method, headers: {'Content-Type': 'application/json'} };
if (data) opts.body = JSON.stringify(data);
const res = await fetch(url, opts);
return res.json();
}
// Load Data
async function loadConfig() {
try {
const data = await fetchAPI('/api/config');
// Populate Fields
document.getElementById('url').value = data.url;
document.getElementById('username').value = data.username;
document.getElementById('password').value = data.password;
document.getElementById('path1').value = data.path1;
document.getElementById('path2').value = data.path2;
document.getElementById('interval').value = data.interval;
document.getElementById('auto_sync').selected = data.auto_sync;
updateStatusUI(data.auto_sync);
} catch(e) {
console.error(e);
}
}
function updateStatusUI(isRunning) {
const icon = document.getElementById('status-icon');
const text = document.getElementById('status-text');
if (isRunning) {
icon.textContent = 'sync';
icon.style.color = '#00c853'; // Green
icon.style.animation = 'spin 2s linear infinite';
text.textContent = '后台运行中';
text.style.color = '#00c853';
} else {
icon.textContent = 'pause_circle';
icon.style.color = '#b00020'; // Red
icon.style.animation = 'none';
text.textContent = '已停止';
text.style.color = '#b00020';
}
}
// Events
document.getElementById('saveBtn').addEventListener('click', async () => {
const payload = {
url: document.getElementById('url').value,
username: document.getElementById('username').value,
password: document.getElementById('password').value,
path1: document.getElementById('path1').value,
path2: document.getElementById('path2').value,
interval: parseInt(document.getElementById('interval').value),
auto_sync: document.getElementById('auto_sync').selected
};
await fetchAPI('/api/config', 'POST', payload);
showToast('配置已保存并应用');
updateStatusUI(payload.auto_sync);
loadLogs();
});
document.getElementById('changePassBtn').addEventListener('click', async () => {
const pass = document.getElementById('new_pass').value;
if(!pass) return showToast('密码不能为空');
const res = await fetch('/api/change_pass', {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'password=' + encodeURIComponent(pass)
});
const json = await res.json();
showToast(json.msg);
document.getElementById('new_pass').value = '';
});
async function loadLogs() {
const logs = await fetchAPI('/api/logs');
const container = document.getElementById('logContainer');
container.innerHTML = logs.map(l =>
`<div style="margin-bottom:4px; border-bottom: 1px solid #333; padding-bottom: 2px;">
<span style="color:#888;">[${l.timestamp}]</span> ${l.message}
</div>`
).join('');
}
document.getElementById('refreshLogBtn').addEventListener('click', loadLogs);
// Add CSS Animation for spinner
const styleSheet = document.createElement("style");
styleSheet.innerText = `@keyframes spin { 100% { transform: rotate(360deg); } }`;
document.head.appendChild(styleSheet);
// Init
loadConfig();
loadLogs();
setInterval(loadLogs, 10000);
</script>
</body>
</html>
"""
# --- 路由逻辑 (保持不变) ---
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
input_pass = request.form.get('password')
with app.app_context():
cfg = AppConfig.query.first()
real_pass = cfg.web_password if cfg else "123456"
if input_pass == real_pass:
session['logged_in'] = True
return redirect(url_for('index'))
else:
error = "密码错误 (默认为 123456)"
return render_template_string(LOGIN_HTML, error=error)
@app.route('/logout')
def logout():
session.pop('logged_in', None)
return redirect(url_for('login'))
@app.route('/')
@login_required
def index():
return render_template_string(INDEX_HTML)
@app.route('/api/config', methods=['GET', 'POST'])
@login_required
def api_config():
cfg = AppConfig.query.first()
if not cfg:
cfg = AppConfig(web_password="123456")
db.session.add(cfg)
db.session.commit()
if request.method == 'POST':
data = request.json
cfg.url = data.get('url')
cfg.username = data.get('username')
cfg.password = data.get('password')
cfg.path1 = data.get('path1')
cfg.path2 = data.get('path2')
cfg.interval = data.get('interval')
cfg.auto_sync = data.get('auto_sync')
db.session.commit()
return jsonify({'success': True})
return jsonify({
'url': cfg.url, 'username': cfg.username, 'password': cfg.password,
'path1': cfg.path1, 'path2': cfg.path2,
'interval': cfg.interval, 'auto_sync': cfg.auto_sync
})
@app.route('/api/change_pass', methods=['POST'])
@login_required
def api_change_pass():
new_pass = request.form.get('password')
if new_pass:
cfg = AppConfig.query.first()
cfg.web_password = new_pass
db.session.commit()
return jsonify({'msg': '面板密码已修改'})
return jsonify({'msg': '无效密码'})
@app.route('/api/logs')
@login_required
def api_logs():
logs = AppLog.query.order_by(AppLog.timestamp.desc()).limit(100).all()
return jsonify([{'timestamp': l.timestamp.strftime("%H:%M:%S"), 'message': l.message} for l in logs])
if __name__ == '__main__':
with app.app_context():
try:
db.create_all()
except: pass
t = threading.Thread(target=background_worker, daemon=True)
t.start()
app.run(host='0.0.0.0', port=PORT, debug=False, use_reloader=False) |