-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_worker.py
More file actions
executable file
·398 lines (342 loc) · 17.5 KB
/
Copy pathupload_worker.py
File metadata and controls
executable file
·398 lines (342 loc) · 17.5 KB
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
import threading
import time
import os
import logging
import asyncio
from pyrogram import Client
import database
import bot_handler
import user_handler
from models import db
from config import API_ID, API_HASH, TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID, USE_TG_OFFICIAL_THUMBNAIL
from pyrogram_clients import perform_pyrogram_upload
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("upload_worker")
# 鎖檔案路徑
LOCK_FILE = ".worker.lock"
class UploadWorker(threading.Thread):
def __init__(self, app):
super().__init__()
self.app = app
self.daemon = True
self._running = True
def run(self):
logger.info("[WORKER-FLOW] Service initialized and running.")
# --- 啟動時強力清理殘留鎖 ---
if os.path.exists(LOCK_FILE):
try:
os.remove(LOCK_FILE)
logger.info("[WORKER-FLOW] Stale lock file cleared on startup.")
except: pass
# --- 啟動時進行 Pyrogram 預熱 (Warmup) ---
self._warmup_pyrogram()
while self._running:
try:
# 1. 檢查並建立檔案鎖
if os.path.exists(LOCK_FILE):
if time.time() - os.path.getmtime(LOCK_FILE) > 600:
try: os.remove(LOCK_FILE)
except: pass
else:
time.sleep(5); continue
try:
with open(LOCK_FILE, "w") as f: f.write(str(os.getpid()))
except: pass
try:
with self.app.app_context():
self._process_tasks()
db.session.remove()
finally:
# 2. 釋放鎖 (增加安全檢查)
if os.path.exists(LOCK_FILE):
try: os.remove(LOCK_FILE)
except: pass
except Exception as e:
logger.error(f"[WORKER-FLOW] Loop error: {e}")
if os.path.exists(LOCK_FILE):
try: os.remove(LOCK_FILE)
except: pass
time.sleep(5)
def _warmup_pyrogram(self):
"""
在背景嘗試獲取目標群組資訊,預熱 Pyrogram 並偵測賬號狀態(包括 FloodWait)。
"""
from pyrogram.errors import FloodWait
if not TELEGRAM_CHAT_ID:
logger.warning("[WARMUP] TELEGRAM_CHAT_ID is not set, skipping warmup.")
return
logger.info(f"[WARMUP] Attempting to resolve TELEGRAM_CHAT_ID: {TELEGRAM_CHAT_ID}")
if not os.path.exists("sessions"): os.makedirs("sessions", exist_ok=True)
session_name = "sessions/shared_upload_session"
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
async def do_warmup():
c = Client(
session_name,
api_id=int(API_ID) if API_ID else None,
api_hash=API_HASH,
bot_token=TELEGRAM_BOT_TOKEN,
workdir="."
)
try:
await c.start()
target_chat = int(TELEGRAM_CHAT_ID) if str(TELEGRAM_CHAT_ID).startswith('-100') else TELEGRAM_CHAT_ID
chat = await c.get_chat(target_chat)
logger.info(f"[WARMUP] Successfully resolved chat: {chat.title} ({chat.id})")
logger.info("[HEALTH-CHECK] Account status: OK (No active restrictions detected)")
except FloodWait as e:
logger.error(f"[HEALTH-CHECK] Account is currently in FLOOD WAIT! Remaining: {e.value} seconds.")
except Exception as e:
logger.error(f"[WARMUP] Failed to resolve chat {TELEGRAM_CHAT_ID}: {e}")
finally:
if c.is_connected:
await c.stop()
loop.run_until_complete(do_warmup())
except Exception as e:
logger.error(f"[WARMUP] Fatal error during warmup: {e}")
finally:
loop.close()
def stop(self):
self._running = False
def _process_tasks(self):
all_queued = database.get_upload_tasks(status='queued', limit=10)
if not all_queued: return
logger.info(f"[WORKER-FLOW] Found {len(all_queued)} queued tasks. Locking now.")
all_ids = [t.id for t in all_queued]
database.bulk_update_upload_task_status(all_ids, 'uploading')
db.session.commit()
db.session.expire_all()
tasks_to_process = []
for tid in all_ids:
t = database.get_upload_task_by_id(tid)
if t: tasks_to_process.append(t)
processed_ids = []
# 0. 檢查超大檔案 (> 2000MB) 並分割
for task in tasks_to_process:
if task.file_size and task.file_size > 2000 * 1024 * 1024:
logger.info(f"[LARGE-FILE-FLOW] Task {task.id} is too large ({task.file_size} bytes). Splitting...")
self._split_large_file(task)
processed_ids.append(task.id)
continue
media_group_candidates = [t for t in tasks_to_process if t.api_used == 'bot_api' and t.id not in processed_ids]
# 1. 批次處理 (Media Group)
if len(media_group_candidates) >= 2:
batch = media_group_candidates[:10]
batch_ids = [t.id for t in batch]
logger.info(f"[BATCH-FLOW] Processing batch: {batch_ids}")
for t in batch: self._ensure_thumbnail(t)
self._process_media_group_batch(batch)
processed_ids.extend(batch_ids)
# 2. 單檔處理
for task in tasks_to_process:
if task.id in processed_ids: continue
if task.status != 'uploading': continue
logger.info(f"[SINGLE-FLOW] Processing Task {task.id}: {task.filename}")
self._ensure_thumbnail(task)
self._process_main_file_task(task)
def _split_large_file(self, task):
"""
將超大檔案分割成多個小於 2GB 的分片任務。
媒體檔案 (MP4等) 用 ffmpeg 動態時長切片,非媒體檔案用 7z 分卷壓縮。
"""
import subprocess
import shutil
import math
file_path = task.temp_file_path
if not os.path.exists(file_path):
database.update_upload_task_status(task.id, 'failed', error_message="File not found for splitting.")
db.session.commit(); return
is_media = task.mime_type.startswith(('video/', 'audio/'))
base_name, ext = os.path.splitext(task.filename)
split_dir = os.path.join("./temp_uploads", f"split_{task.id}")
os.makedirs(split_dir, exist_ok=True)
parts = []
try:
if is_media:
logger.info(f"[SPLIT-MEDIA] Analyzing duration for {task.filename}")
# 1. 使用 ffprobe 獲取總時長
probe_cmd = [
'ffprobe', '-v', 'error', '-show_entries', 'format=duration',
'-of', 'default=noprint_wrappers=1:nokey=1', file_path
]
try:
duration = float(subprocess.check_output(probe_cmd).decode('utf-8').strip())
# 2. 計算每份切片時間 (檔案大小 / 1.8GB = 份數; 時長 / 份數 = 每份秒數)
# 使用 1.8GB (1.8 * 1024 * 1024 * 1024 bytes) 作為安全閾值
safe_limit = 1.8 * 1024 * 1024 * 1024
num_parts = math.ceil(task.file_size / safe_limit)
segment_time = math.floor(duration / num_parts)
logger.info(f"[SPLIT-MEDIA] Duration: {duration}s, Size: {task.file_size}, Parts: {num_parts}, Segment Time: {segment_time}s")
except Exception as e:
logger.warning(f"[SPLIT-MEDIA] Failed to probe duration, fallback to 600s: {e}")
segment_time = 600 # 萬一獲取失敗,用保守的 10 分鐘
# 3. 執行動態切片
output_pattern = os.path.join(split_dir, f"{base_name}_part%03d{ext}")
cmd = [
'ffmpeg', '-i', file_path,
'-c', 'copy', '-map', '0',
'-f', 'segment',
'-segment_time', str(segment_time),
'-reset_timestamps', '1',
output_pattern
]
subprocess.run(cmd, check=True)
else:
logger.info(f"[SPLIT-NON-MEDIA] Using 7z for {task.filename}")
# 非媒體檔案:使用 7z 進行物理分卷壓縮,每卷 1900M
archive_path = os.path.join(split_dir, f"{task.filename}.7z")
cmd = [
'7z', 'a', '-v1900m', archive_path, file_path
]
subprocess.run(cmd, check=True)
# 掃描生成的分片檔案
for f in sorted(os.listdir(split_dir)):
if f.endswith(ext) or '.7z.' in f:
parts.append(os.path.join(split_dir, f))
if not parts:
raise Exception("Splitting failed, no parts generated.")
# 為每個分片創建新任務
for i, part_path in enumerate(parts):
part_size = os.path.getsize(part_path)
part_filename = os.path.basename(part_path)
api = 'pyrogram' if part_size >= 20 * 1024 * 1024 else 'bot_api'
database.add_upload_task_to_db(
filename=part_filename,
temp_file_path=part_path,
target_folder=task.target_folder,
file_size=part_size,
mime_type=task.mime_type if is_media else 'application/x-7z-compressed',
api_used=api,
status='queued',
priority=task.priority + 1
)
database.update_upload_task_status(task.id, 'completed', error_message=f"Split into {len(parts)} parts.")
if os.path.exists(file_path): os.remove(file_path)
logger.info(f"[SPLIT-FLOW] Task {task.id} split successfully into {len(parts)} parts.")
except Exception as e:
logger.error(f"[SPLIT-FLOW] Task {task.id} split failed: {e}")
database.update_upload_task_status(task.id, 'failed', error_message=f"Split failed: {str(e)}")
db.session.commit()
db.session.commit()
def _ensure_thumbnail(self, task):
if USE_TG_OFFICIAL_THUMBNAIL: return
if task.thumbnail_path and os.path.exists(task.thumbnail_path): return
if task.mime_type.startswith(('image/', 'video/')):
logger.info(f"[THUMB-FLOW] Custom mode: Generating thumbnail for Task {task.id}")
# --- 關鍵修復:隔離循環 ---
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
path = loop.run_until_complete(user_handler.generate_thumbnail(task.temp_file_path, task.mime_type))
if path:
task.thumbnail_path = path
db.session.add(task)
db.session.commit()
except Exception as e: logger.error(f"[THUMB-FLOW] Task {task.id} generation error: {e}")
finally:
loop.close()
def _cleanup_successful_task(self, task):
try:
database.update_upload_task_status(task.id, 'completed')
db.session.commit()
if task.temp_file_path and os.path.exists(task.temp_file_path):
try: os.remove(task.temp_file_path)
except: pass
if task.thumbnail_path and os.path.exists(task.thumbnail_path):
try: os.remove(task.thumbnail_path)
except: pass
logger.info(f"[WORKER-FLOW] Task {task.id} finalized.")
except Exception as e:
logger.error(f"[WORKER-FLOW] Task {task.id} cleanup error: {e}")
def _process_media_group_batch(self, tasks):
try:
extracted_data = bot_handler.send_media_group_to_telegram(tasks)
if not extracted_data: raise Exception("MediaGroup returned NO DATA.")
for data in extracted_data:
f_id, f_name, m_type, size, msg_id, t_id, orig_task = data
if not t_id and USE_TG_OFFICIAL_THUMBNAIL:
logger.info(f"[THUMB-FIX] Official thumbnail missing for {orig_task.filename}, triggering fallback.")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
temp_thumb = loop.run_until_complete(user_handler.generate_thumbnail(orig_task.temp_file_path, orig_task.mime_type))
if temp_thumb:
t_id = bot_handler.upload_thumbnail_only(temp_thumb)
if os.path.exists(temp_thumb): os.remove(temp_thumb)
except: pass
finally: loop.close()
chat_str = str(TELEGRAM_CHAT_ID).replace('-100', '')
database.add_file(f_name, f_id, orig_task.target_folder, size, m_type, t_id, f"https://t.me/c/{chat_str}/{msg_id}")
self._cleanup_successful_task(orig_task)
except Exception as e:
logger.error(f"[BATCH-FLOW] Execution failed: {e}")
t_ids = [t.id for t in tasks]
database.bulk_update_upload_task_status(t_ids, 'failed', error_message=str(e))
db.session.commit()
def _process_main_file_task(self, task):
try:
thumb = task.thumbnail_path if task.thumbnail_path and os.path.exists(task.thumbnail_path) else None
f_id = None; f_name = None; m_type = None; size = None; t_id = None; msg_id = None
if task.api_used == 'pyrogram':
# --- 關鍵修正:使用共享的持久 Session ---
if not os.path.exists("sessions"): os.makedirs("sessions", exist_ok=True)
session_name = "sessions/shared_upload_session"
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
async def run_pyro():
# 共享 Session 不需要每次刪除
c = Client(
session_name,
api_id=int(API_ID) if API_ID else None,
api_hash=API_HASH,
bot_token=TELEGRAM_BOT_TOKEN,
workdir="."
)
await c.start()
try:
target_chat = int(TELEGRAM_CHAT_ID) if str(TELEGRAM_CHAT_ID).startswith('-100') else TELEGRAM_CHAT_ID
res = await perform_pyrogram_upload(c, task.temp_file_path, task.filename, target_chat, task.mime_type, task.file_size, thumbnail_path=thumb, task_id=task.id)
return res
finally:
await c.stop()
f_id, f_name, m_type, size, t_id, msg_id = loop.run_until_complete(run_pyro())
finally:
loop.close()
else:
from werkzeug.datastructures import FileStorage
with open(task.temp_file_path, 'rb') as f:
fs = FileStorage(stream=f, filename=task.filename, content_type=task.mime_type)
f_id, f_name, m_type, size, msg_id, t_id = bot_handler.send_file_to_telegram(fs, thumbnail_path=thumb, file_size=task.file_size)
if f_id:
if not t_id and USE_TG_OFFICIAL_THUMBNAIL:
logger.info(f"[THUMB-FIX] Official thumbnail missing, triggering fallback.")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
temp_thumb = loop.run_until_complete(user_handler.generate_thumbnail(task.temp_file_path, task.mime_type))
if temp_thumb:
t_id = bot_handler.upload_thumbnail_only(temp_thumb)
if os.path.exists(temp_thumb): os.remove(temp_thumb)
except: pass
finally: loop.close()
chat_str = str(TELEGRAM_CHAT_ID).replace('-100', '')
database.add_file(f_name, f_id, task.target_folder, size, m_type, t_id, f"https://t.me/c/{chat_str}/{msg_id}")
self._cleanup_successful_task(task)
else:
database.update_upload_task_status(task.id, 'failed', error_message="Upload failed - No File ID.")
db.session.commit()
except Exception as e:
logger.error(f"[SINGLE-FLOW] Task {task.id} failed: {e}")
database.update_upload_task_status(task.id, 'failed', error_message=str(e))
db.session.commit()
upload_worker_thread = None
def start_upload_worker(app):
global upload_worker_thread
if upload_worker_thread is None or not upload_worker_thread.is_alive():
upload_worker_thread = UploadWorker(app)
upload_worker_thread.start()
def stop_upload_worker():
global upload_worker_thread
if upload_worker_thread is not None: upload_worker_thread.stop()