You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.1 KiB

8 months ago
  1. from fastapi import FastAPI, File, UploadFile, HTTPException, Form
  2. import paramiko
  3. from fastapi import Depends, APIRouter, status, Query, Path, HTTPException
  4. from internal.models import *
  5. from internal.database import fetch_one, fetch_all, execute_query, response_success, raise_if_exists, raise_if_not_found
  6. from dependencies import get_current_active_user
  7. from datetime import datetime
  8. import os
  9. from pathlib import Path
  10. router = APIRouter(
  11. prefix="/photos",
  12. tags=['图片管理']
  13. )
  14. app = FastAPI()
  15. # 配置远程服务器信息
  16. REMOTE_HOST = "111.229.38.129"
  17. REMOTE_PORT = 6000 # 默认为 22
  18. REMOTE_USERNAME = "root"
  19. REMOTE_PASSWORD = "zl981023"
  20. REMOTE_BASE_DIRECTORY = "/home/blog"
  21. def file_exists(sftp, remote_file_path: str) -> bool:
  22. """检查远程文件是否存在"""
  23. try:
  24. sftp.stat(remote_file_path)
  25. return True
  26. except FileNotFoundError:
  27. return False
  28. def ensure_remote_directory(sftp, remote_directory: str):
  29. """确保远程目录存在"""
  30. try:
  31. sftp.chdir(remote_directory) # 目录存在
  32. except IOError:
  33. sftp.mkdir(remote_directory) # 目录不存在,创建
  34. sftp.chdir(remote_directory)
  35. def list_files_in_remote_directory(sftp, remote_directory):
  36. try:
  37. file_list = sftp.listdir(remote_directory)
  38. return file_list
  39. except Exception as e:
  40. raise HTTPException(
  41. status_code=500, detail=f"Could not list files in remote directory: {e}")
  42. @router.post("/uploadfile/")
  43. async def upload_file(file: UploadFile = File(...), album_key: str = Form(...), new_filename: str = None):
  44. try:
  45. # 获取文件扩展名
  46. file_extension = os.path.splitext(file.filename)[1]
  47. # 使用原文件名,如果没有提供新的文件名
  48. if not new_filename:
  49. new_filename = file.filename
  50. # 保持扩展名
  51. if not new_filename.endswith(file_extension):
  52. new_filename += file_extension
  53. # 根据相册的 key 生成远程文件路径
  54. remote_directory = Path(REMOTE_BASE_DIRECTORY) / album_key
  55. remote_file_path = remote_directory / new_filename
  56. remote_file_path = remote_file_path.as_posix()
  57. # 使用 Paramiko 连接远程服务器
  58. ssh = paramiko.SSHClient()
  59. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  60. ssh.connect(REMOTE_HOST, port=REMOTE_PORT,
  61. username=REMOTE_USERNAME, password=REMOTE_PASSWORD)
  62. sftp = ssh.open_sftp()
  63. # 确保远程目录存在
  64. ensure_remote_directory(sftp, remote_directory.as_posix())
  65. # 检查文件是否存在,如果存在,返回错误消息
  66. if file_exists(sftp, remote_file_path):
  67. sftp.close()
  68. ssh.close()
  69. raise HTTPException(
  70. status_code=400, detail="File with the given name already exists in the album.")
  71. # 将文件对象直接上传到远程服务器
  72. sftp.putfo(file.file, remote_file_path)
  73. sftp.close()
  74. ssh.close()
  75. return {"filename": new_filename, "message": "File uploaded successfully"}
  76. except Exception as e:
  77. raise HTTPException(
  78. status_code=500, detail=f"Could not upload file to remote server: {e}")
  79. @router.get("/listfiles/")
  80. async def list_files(album_key: str):
  81. try:
  82. remote_directory = Path(REMOTE_BASE_DIRECTORY) / album_key
  83. remote_directory = remote_directory.as_posix()
  84. # 使用 Paramiko 连接远程服务器
  85. ssh = paramiko.SSHClient()
  86. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  87. ssh.connect(REMOTE_HOST, port=REMOTE_PORT, username=REMOTE_USERNAME, password=REMOTE_PASSWORD)
  88. sftp = ssh.open_sftp()
  89. # 获取远程目录中的文件列表
  90. file_list = list_files_in_remote_directory(sftp, remote_directory)
  91. sftp.close()
  92. ssh.close()
  93. # 构造文件URL列表
  94. file_urls = [f"{album_key}/{filename}" for filename in file_list]
  95. return {"files": file_urls}
  96. except Exception as e:
  97. raise HTTPException(status_code=500, detail=f"Could not retrieve file list: {e}")