File size: 3,681 Bytes
5dc13d9
 
 
 
 
 
 
c877088
5dc13d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c877088
5dc13d9
 
c877088
5dc13d9
 
c877088
5dc13d9
 
 
c877088
5dc13d9
 
 
c877088
 
5dc13d9
 
 
c877088
5dc13d9
c877088
5dc13d9
c877088
5dc13d9
c877088
5dc13d9
 
c877088
5dc13d9
c877088
5dc13d9
 
 
 
 
 
c877088
 
5dc13d9
c877088
 
 
 
5dc13d9
 
c877088
 
 
 
 
 
5dc13d9
c877088
 
 
5dc13d9
c877088
 
 
 
5dc13d9
c877088
 
 
5dc13d9
c877088
 
 
5dc13d9
 
c877088
 
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
import os
import time
import shutil
import zipfile
import gradio as gr
from fastapi import FastAPI

# --- CẤU HÌNH ---
BASE_UPLOAD_DIR = "bins"
EXPIRATION_HOURS = 24

if not os.path.exists(BASE_UPLOAD_DIR):
    os.makedirs(BASE_UPLOAD_DIR)

def clean_old_bins():
    now = time.time()
    cutoff = now - (EXPIRATION_HOURS * 3600)
    if os.path.exists(BASE_UPLOAD_DIR):
        for bin_id in os.listdir(BASE_UPLOAD_DIR):
            bin_path = os.path.join(BASE_UPLOAD_DIR, bin_id)
            if os.path.getmtime(bin_path) < cutoff:
                shutil.rmtree(bin_path)

# --- LOGIC XỬ LÝ ---
def upload_to_bin(bin_id, files, progress=gr.Progress()):
    clean_old_bins()
    if not bin_id or not files: return "⚠️ Thiếu mã Bin hoặc File!", None
    bin_id = bin_id.strip()
    bin_path = os.path.join(BASE_UPLOAD_DIR, bin_id)
    if not os.path.exists(bin_path): os.makedirs(bin_path)
    
    uploaded_list = []
    for i, file in enumerate(files):
        progress((i + 1) / len(files), desc=f"Đang xử lý {i+1}/{len(files)}...")
        dest_path = os.path.join(bin_path, os.path.basename(file.name))
        shutil.copy(file.name, dest_path)
        uploaded_list.append(dest_path)
    os.utime(bin_path, None)
    return f"✅ Đã tải lên Bin: {bin_id}", uploaded_list

def get_bin_files(bin_id):
    clean_old_bins()
    if not bin_id: return "⚠️ Nhập mã Bin!", None
    bin_path = os.path.join(BASE_UPLOAD_DIR, bin_id.strip())
    if not os.path.exists(bin_path): return "❌ Không tìm thấy Bin.", None
    files = [os.path.join(bin_path, f) for f in os.listdir(bin_path) if f != "all_files.zip"]
    return f"📂 Bin: {bin_id}", files

def download_all_zip(bin_id):
    bin_path = os.path.join(BASE_UPLOAD_DIR, bin_id.strip())
    zip_path = os.path.join(bin_path, "all_files.zip")
    files = [f for f in os.listdir(bin_path) if f != "all_files.zip"]
    with zipfile.ZipFile(zip_path, 'w') as zipf:
        for f in files: zipf.write(os.path.join(bin_path, f), f)
    return zip_path

def delete_bin(bin_id):
    bin_path = os.path.join(BASE_UPLOAD_DIR, bin_id.strip())
    if os.path.exists(bin_path):
        shutil.rmtree(bin_path)
        return "🗑️ Đã xóa Bin.", None
    return "❌ Không tìm thấy.", None

# --- GIAO DIỆN ---
with gr.Blocks(theme=gr.themes.Default()) as demo:
    gr.Markdown("# 🚀 HF FileBin Pro")
    bin_id_input = gr.Textbox(label="Mã Bin (Private ID)")
    
    with gr.Row():
        with gr.Column():
            file_input = gr.File(label="Upload", file_count="multiple")
            btn_upload = gr.Button("📤 Tải lên", variant="primary")
        with gr.Column():
            status_msg = gr.Textbox(label="Trạng thái")
            file_output = gr.File(label="Danh sách file")
            with gr.Row():
                btn_access = gr.Button("🔍 Kiểm tra")
                btn_zip = gr.Button("📦 Tải ZIP")
                btn_delete = gr.Button("🗑️ Xóa", variant="stop")

    btn_upload.click(upload_to_bin, [bin_id_input, file_input], [status_msg, file_output])
    btn_access.click(get_bin_files, [bin_id_input], [status_msg, file_output])
    btn_zip.click(download_all_zip, [bin_id_input], [file_output])
    btn_delete.click(delete_bin, [bin_id_input], [status_msg, file_output])

# --- PHẦN QUAN TRỌNG CHO UPTIME ROBOT ---
# Chúng ta sẽ dùng FastAPI trực tiếp thông qua thuộc tính .app của Gradio
app = demo.app 

@app.get("/health")
def health():
    return {"status": "ok"}

if __name__ == "__main__":
    # Dùng launch() trực tiếp là cách an toàn nhất trên HF
    demo.launch(server_name="0.0.0.0", server_port=7860)