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.

66 lines
2.0 KiB

# -*- coding:utf-8 -*-
"""
@Created on : 2023/6/13 15:19
@Author: hxl
@Des:
"""
import os
import io
import shutil
from fastapi import APIRouter
from helper import respond_to
from helper import usb
router = APIRouter()
@router.get("/files/{pid}/{date}")
async def get_files_by_date(pid: str, date: str):
pid_dir = f"/data/{pid}"
if not os.path.exists(pid_dir):
return respond_to(code=400, desc=f'没有找到{pid}的目录')
files = os.listdir(pid_dir)
result = []
for file in files:
if file.startswith(date.replace('-', '')) and file.endswith(".mp4"):
result.append(file)
return respond_to(data=result)
@router.post("/download_file/{pid}", summary='导出视频')
async def copy_videos(pid: str, filelist: list):
if not filelist:
return respond_to(400, desc='未选择导出文件')
try:
for filename in filelist:
# 检查U盘挂载路径
usb_mount_path = "/media/yanei" # U盘的挂载路径
r = os.popen('ls -a /media/yanei')
text = r.read()
r.close()
udisklist = text.splitlines()
if(len(udisklist) < 3):
return respond_to(400, desc='U盘未正确挂载')
source_directory = f"/data/{pid}" # 视频文件所在的目录路径
source_file = os.path.join(source_directory, filename) # 待导入的视频文件路径
# 检查源文件是否存在
if not os.path.isfile(source_file):
return respond_to(400, desc='指定的视频文件不存在')
# 构建目标U盘中的完整路径
destination_path = os.path.join(usb_mount_path, filename)
# 复制视频文件到U盘
with open(source_file, mode="rb") as file:
file_content = file.read()
buffer = io.BytesIO(file_content)
usb.put_in(filename, buffer)
return respond_to(desc='导出U盘成功')
except Exception as e:
return {"message": str(e)}