Upload 2 files
Browse files- app.py +88 -80
- requirements.txt +2 -0
app.py
CHANGED
|
@@ -10,7 +10,9 @@ import uuid
|
|
| 10 |
import threading
|
| 11 |
import time
|
| 12 |
import random
|
|
|
|
| 13 |
from fastapi.responses import FileResponse, JSONResponse
|
|
|
|
| 14 |
|
| 15 |
# 环境变量
|
| 16 |
ACCESS_PASSWORD = os.environ.get("ACCESS_PASSWORD", "changeme")
|
|
@@ -395,6 +397,89 @@ init_success = init_db()
|
|
| 395 |
keep_alive_thread = threading.Thread(target=keep_alive, daemon=True)
|
| 396 |
keep_alive_thread.start()
|
| 397 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 398 |
# 自定义CSS
|
| 399 |
custom_css = """
|
| 400 |
.compact-gallery .gallery {
|
|
@@ -540,85 +625,8 @@ with gr.Blocks(title="My图床", theme=gr.themes.Soft(), css=custom_css) as grad
|
|
| 540 |
outputs=[export_output, export_file_output]
|
| 541 |
)
|
| 542 |
|
| 543 |
-
#
|
| 544 |
-
|
| 545 |
-
async def get_image(image_hash: str):
|
| 546 |
-
"""通过hash获取图片"""
|
| 547 |
-
try:
|
| 548 |
-
conn = get_db_connection()
|
| 549 |
-
cursor = conn.cursor()
|
| 550 |
-
cursor.execute("SELECT * FROM images WHERE hash = ?", (image_hash,))
|
| 551 |
-
row = cursor.fetchone()
|
| 552 |
-
conn.close()
|
| 553 |
-
|
| 554 |
-
if not row:
|
| 555 |
-
return JSONResponse(
|
| 556 |
-
status_code=404,
|
| 557 |
-
content={"error": "Image not found"}
|
| 558 |
-
)
|
| 559 |
-
|
| 560 |
-
file_path = Path(row['file_path'])
|
| 561 |
-
|
| 562 |
-
if not file_path.exists():
|
| 563 |
-
return JSONResponse(
|
| 564 |
-
status_code=404,
|
| 565 |
-
content={"error": "Image file not found"}
|
| 566 |
-
)
|
| 567 |
-
|
| 568 |
-
# 返回图片文件
|
| 569 |
-
return FileResponse(
|
| 570 |
-
file_path,
|
| 571 |
-
media_type=row['mime_type'],
|
| 572 |
-
headers={
|
| 573 |
-
"Cache-Control": "public, max-age=31536000",
|
| 574 |
-
"Content-Disposition": f'inline; filename="{row["original_filename"]}"'
|
| 575 |
-
}
|
| 576 |
-
)
|
| 577 |
-
|
| 578 |
-
except Exception as e:
|
| 579 |
-
return JSONResponse(
|
| 580 |
-
status_code=500,
|
| 581 |
-
content={"error": str(e)}
|
| 582 |
-
)
|
| 583 |
-
|
| 584 |
-
@gradio_app.app.get("/api/img/{image_hash}/info")
|
| 585 |
-
async def get_image_info(image_hash: str, password: str = None):
|
| 586 |
-
"""获取图片信息(需要密码)"""
|
| 587 |
-
if not password or not check_password(password):
|
| 588 |
-
return JSONResponse(
|
| 589 |
-
status_code=401,
|
| 590 |
-
content={"error": "Unauthorized"}
|
| 591 |
-
)
|
| 592 |
-
|
| 593 |
-
try:
|
| 594 |
-
conn = get_db_connection()
|
| 595 |
-
cursor = conn.cursor()
|
| 596 |
-
cursor.execute("SELECT * FROM images WHERE hash = ?", (image_hash,))
|
| 597 |
-
row = cursor.fetchone()
|
| 598 |
-
conn.close()
|
| 599 |
-
|
| 600 |
-
if not row:
|
| 601 |
-
return JSONResponse(
|
| 602 |
-
status_code=404,
|
| 603 |
-
content={"error": "Image not found"}
|
| 604 |
-
)
|
| 605 |
-
|
| 606 |
-
return JSONResponse(content={
|
| 607 |
-
"hash": row['hash'],
|
| 608 |
-
"filename": row['filename'],
|
| 609 |
-
"original_filename": row['original_filename'],
|
| 610 |
-
"file_size": row['file_size'],
|
| 611 |
-
"mime_type": row['mime_type'],
|
| 612 |
-
"upload_time": row['upload_time'],
|
| 613 |
-
"description": row['description'],
|
| 614 |
-
"url": generate_full_url(row['hash'])
|
| 615 |
-
})
|
| 616 |
-
|
| 617 |
-
except Exception as e:
|
| 618 |
-
return JSONResponse(
|
| 619 |
-
status_code=500,
|
| 620 |
-
content={"error": str(e)}
|
| 621 |
-
)
|
| 622 |
|
| 623 |
if __name__ == "__main__":
|
| 624 |
-
|
|
|
|
| 10 |
import threading
|
| 11 |
import time
|
| 12 |
import random
|
| 13 |
+
from fastapi import FastAPI
|
| 14 |
from fastapi.responses import FileResponse, JSONResponse
|
| 15 |
+
import uvicorn
|
| 16 |
|
| 17 |
# 环境变量
|
| 18 |
ACCESS_PASSWORD = os.environ.get("ACCESS_PASSWORD", "changeme")
|
|
|
|
| 397 |
keep_alive_thread = threading.Thread(target=keep_alive, daemon=True)
|
| 398 |
keep_alive_thread.start()
|
| 399 |
|
| 400 |
+
# 创建FastAPI应用
|
| 401 |
+
app = FastAPI(title="私人图床服务")
|
| 402 |
+
|
| 403 |
+
# 添加图片访问API路由(在Gradio之前)
|
| 404 |
+
@app.get("/api/img/{image_hash}")
|
| 405 |
+
async def get_image(image_hash: str):
|
| 406 |
+
"""通过hash获取图片"""
|
| 407 |
+
try:
|
| 408 |
+
conn = get_db_connection()
|
| 409 |
+
cursor = conn.cursor()
|
| 410 |
+
cursor.execute("SELECT * FROM images WHERE hash = ?", (image_hash,))
|
| 411 |
+
row = cursor.fetchone()
|
| 412 |
+
conn.close()
|
| 413 |
+
|
| 414 |
+
if not row:
|
| 415 |
+
return JSONResponse(
|
| 416 |
+
status_code=404,
|
| 417 |
+
content={"error": "Image not found"}
|
| 418 |
+
)
|
| 419 |
+
|
| 420 |
+
file_path = Path(row['file_path'])
|
| 421 |
+
|
| 422 |
+
if not file_path.exists():
|
| 423 |
+
return JSONResponse(
|
| 424 |
+
status_code=404,
|
| 425 |
+
content={"error": "Image file not found"}
|
| 426 |
+
)
|
| 427 |
+
|
| 428 |
+
# 返回图片文件
|
| 429 |
+
return FileResponse(
|
| 430 |
+
file_path,
|
| 431 |
+
media_type=row['mime_type'],
|
| 432 |
+
headers={
|
| 433 |
+
"Cache-Control": "public, max-age=31536000",
|
| 434 |
+
"Content-Disposition": f'inline; filename="{row["original_filename"]}"'
|
| 435 |
+
}
|
| 436 |
+
)
|
| 437 |
+
|
| 438 |
+
except Exception as e:
|
| 439 |
+
return JSONResponse(
|
| 440 |
+
status_code=500,
|
| 441 |
+
content={"error": str(e)}
|
| 442 |
+
)
|
| 443 |
+
|
| 444 |
+
@app.get("/api/img/{image_hash}/info")
|
| 445 |
+
async def get_image_info(image_hash: str, password: str = None):
|
| 446 |
+
"""获取图片信息(需要密码)"""
|
| 447 |
+
if not password or not check_password(password):
|
| 448 |
+
return JSONResponse(
|
| 449 |
+
status_code=401,
|
| 450 |
+
content={"error": "Unauthorized"}
|
| 451 |
+
)
|
| 452 |
+
|
| 453 |
+
try:
|
| 454 |
+
conn = get_db_connection()
|
| 455 |
+
cursor = conn.cursor()
|
| 456 |
+
cursor.execute("SELECT * FROM images WHERE hash = ?", (image_hash,))
|
| 457 |
+
row = cursor.fetchone()
|
| 458 |
+
conn.close()
|
| 459 |
+
|
| 460 |
+
if not row:
|
| 461 |
+
return JSONResponse(
|
| 462 |
+
status_code=404,
|
| 463 |
+
content={"error": "Image not found"}
|
| 464 |
+
)
|
| 465 |
+
|
| 466 |
+
return JSONResponse(content={
|
| 467 |
+
"hash": row['hash'],
|
| 468 |
+
"filename": row['filename'],
|
| 469 |
+
"original_filename": row['original_filename'],
|
| 470 |
+
"file_size": row['file_size'],
|
| 471 |
+
"mime_type": row['mime_type'],
|
| 472 |
+
"upload_time": row['upload_time'],
|
| 473 |
+
"description": row['description'],
|
| 474 |
+
"url": generate_full_url(row['hash'])
|
| 475 |
+
})
|
| 476 |
+
|
| 477 |
+
except Exception as e:
|
| 478 |
+
return JSONResponse(
|
| 479 |
+
status_code=500,
|
| 480 |
+
content={"error": str(e)}
|
| 481 |
+
)
|
| 482 |
+
|
| 483 |
# 自定义CSS
|
| 484 |
custom_css = """
|
| 485 |
.compact-gallery .gallery {
|
|
|
|
| 625 |
outputs=[export_output, export_file_output]
|
| 626 |
)
|
| 627 |
|
| 628 |
+
# 将Gradio应用挂载到FastAPI
|
| 629 |
+
app = gr.mount_gradio_app(app, gradio_app, path="/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 630 |
|
| 631 |
if __name__ == "__main__":
|
| 632 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,5 @@
|
|
| 1 |
gradio==4.44.0
|
| 2 |
Pillow==10.4.0
|
|
|
|
|
|
|
| 3 |
|
|
|
|
| 1 |
gradio==4.44.0
|
| 2 |
Pillow==10.4.0
|
| 3 |
+
fastapi>=0.104.0
|
| 4 |
+
uvicorn[standard]>=0.24.0
|
| 5 |
|