添加创建请求和读取进度接口
This commit is contained in:
@@ -6,7 +6,8 @@ import tempfile
|
||||
import shutil
|
||||
import logging
|
||||
from contextlib import contextmanager
|
||||
import time # 导入 time 模块
|
||||
import time
|
||||
from shared_utils import add_task_log
|
||||
|
||||
@contextmanager
|
||||
def temp_directory(dir=None):
|
||||
@@ -16,13 +17,16 @@ def temp_directory(dir=None):
|
||||
finally:
|
||||
pass
|
||||
|
||||
def download_file(url, temp_dir):
|
||||
def download_file(url, temp_dir, task_id=None, task_registry=None, task_lock=None):
|
||||
try:
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0',
|
||||
'Accept': '*/*'
|
||||
}
|
||||
|
||||
if task_id and task_registry and task_lock:
|
||||
add_task_log(task_id, f"开始从URL下载文件: {url}", task_registry, task_lock)
|
||||
|
||||
logging.info(f"开始从URL下载文件: {url}")
|
||||
response = requests.get(url, headers=headers, stream=True, timeout=30)
|
||||
response.raise_for_status()
|
||||
@@ -40,44 +44,79 @@ def download_file(url, temp_dir):
|
||||
logging.error(error_msg)
|
||||
raise Exception(error_msg)
|
||||
|
||||
if task_id and task_registry and task_lock:
|
||||
add_task_log(task_id, f"文件下载成功,保存到: {file_path} (大小: {file_size}字节)", task_registry, task_lock)
|
||||
|
||||
logging.info(f"文件下载成功,保存到: {file_path} (大小: {file_size}字节)")
|
||||
return file_path
|
||||
except Exception as e:
|
||||
logging.error(f"从 {url} 下载文件时出错: {e}")
|
||||
error_msg = f"从 {url} 下载文件时出错: {e}"
|
||||
if task_id and task_registry and task_lock:
|
||||
add_task_log(task_id, error_msg, task_registry, task_lock)
|
||||
logging.error(error_msg)
|
||||
raise
|
||||
|
||||
def execute_sanjuuni(input_path, output_path, sanjuuni_args):
|
||||
def execute_sanjuuni(input_path, output_path, sanjuuni_args, task_id=None, task_registry=None, task_lock=None):
|
||||
try:
|
||||
cmd = ['lib/sanjuuni/sanjuuni', '-i', input_path] + sanjuuni_args + ['-o', output_path]
|
||||
|
||||
if task_id and task_registry and task_lock:
|
||||
add_task_log(task_id, f"执行Sanjuuni命令: {' '.join(cmd)}", task_registry, task_lock)
|
||||
|
||||
logging.info(f"执行Sanjuuni命令: {' '.join(cmd)}")
|
||||
|
||||
result = subprocess.run(
|
||||
# 使用Popen来实时捕获输出
|
||||
process = subprocess.Popen(
|
||||
cmd,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
universal_newlines=True,
|
||||
encoding='utf-8',
|
||||
errors='replace'
|
||||
errors='replace',
|
||||
bufsize=1
|
||||
)
|
||||
|
||||
if result.returncode != 0:
|
||||
error_msg = f"Sanjuuni处理失败,返回码: {result.returncode}, 错误: {result.stderr}"
|
||||
# 实时读取输出
|
||||
while True:
|
||||
output = process.stdout.readline()
|
||||
if output == '' and process.poll() is not None:
|
||||
break
|
||||
if output:
|
||||
output = output.strip()
|
||||
if output and task_id and task_registry and task_lock:
|
||||
add_task_log(task_id, output, task_registry, task_lock)
|
||||
|
||||
returncode = process.poll()
|
||||
if returncode != 0:
|
||||
error_msg = f"Sanjuuni处理失败,返回码: {returncode}"
|
||||
if task_id and task_registry and task_lock:
|
||||
add_task_log(task_id, error_msg, task_registry, task_lock)
|
||||
logging.error(error_msg)
|
||||
raise Exception(error_msg)
|
||||
|
||||
if task_id and task_registry and task_lock:
|
||||
add_task_log(task_id, "Sanjuuni处理成功完成", task_registry, task_lock)
|
||||
|
||||
logging.info("Sanjuuni处理成功完成")
|
||||
return True
|
||||
except Exception as e:
|
||||
logging.error(f"执行Sanjuuni时出错: {e}")
|
||||
error_msg = f"执行Sanjuuni时出错: {e}"
|
||||
if task_id and task_registry and task_lock:
|
||||
add_task_log(task_id, error_msg, task_registry, task_lock)
|
||||
logging.error(error_msg)
|
||||
raise
|
||||
|
||||
def process_sanjuuni(data, file_registry, file_lock):
|
||||
def process_sanjuuni(data, file_registry, file_lock, task_id=None, task_registry=None, task_lock=None):
|
||||
try:
|
||||
temp_dir = tempfile.mkdtemp(dir='temp_files')
|
||||
|
||||
if task_id and task_registry and task_lock:
|
||||
add_task_log(task_id, f"创建临时目录: {temp_dir}", task_registry, task_lock)
|
||||
|
||||
logging.info(f"创建临时目录: {temp_dir}")
|
||||
|
||||
input_url = data.get('input_url')
|
||||
sanjuuni_args = data.get('args', []) # 从请求数据中获取 args 参数
|
||||
sanjuuni_args = data.get('args', [])
|
||||
|
||||
# 定义不允许的参数
|
||||
disallowed_params = [
|
||||
@@ -114,33 +153,46 @@ def process_sanjuuni(data, file_registry, file_lock):
|
||||
else:
|
||||
raise ValueError(f"Unsupported output format: {output_format}")
|
||||
|
||||
input_path = download_file(input_url, temp_dir)
|
||||
input_path = download_file(input_url, temp_dir, task_id, task_registry, task_lock)
|
||||
|
||||
output_id = str(uuid.uuid4())[:8]
|
||||
output_filename = f"{output_id}.{output_format}"
|
||||
output_path = os.path.join(temp_dir, output_filename)
|
||||
|
||||
execute_sanjuuni(input_path, output_path, sanjuuni_args)
|
||||
if task_id and task_registry and task_lock:
|
||||
add_task_log(task_id, f"开始Sanjuuni处理,输出格式: {output_format}", task_registry, task_lock)
|
||||
with task_lock:
|
||||
if task_id in task_registry:
|
||||
task_registry[task_id]['progress'] = 50
|
||||
|
||||
execute_sanjuuni(input_path, output_path, sanjuuni_args, task_id, task_registry, task_lock)
|
||||
|
||||
with file_lock:
|
||||
file_registry[output_id] = {
|
||||
'path': os.path.abspath(output_path),
|
||||
'filename': output_filename,
|
||||
'last_access': time.time(), # 使用 time.time() 记录最后访问时间
|
||||
'last_access': time.time(),
|
||||
'download_count': 0
|
||||
}
|
||||
logging.info(f"已注册新文件ID: {output_id}, 路径: {output_path}")
|
||||
|
||||
if task_id and task_registry and task_lock:
|
||||
add_task_log(task_id, f"已注册新文件ID: {output_id}, 路径: {output_path}", task_registry, task_lock)
|
||||
with task_lock:
|
||||
if task_id in task_registry:
|
||||
task_registry[task_id]['progress'] = 100
|
||||
|
||||
logging.info(f"已注册新文件ID: {output_id}, 路径: {output_path}")
|
||||
|
||||
return {
|
||||
'status': 'success',
|
||||
'download_url': f"http://ffmpeg.liulikeji.cn/download/{output_id}/{output_filename}",
|
||||
'file_id': output_id,
|
||||
'temp_dir': temp_dir # 返回临时目录路径
|
||||
'temp_dir': temp_dir
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"处理过程中出错: {e}")
|
||||
return {'error': str(e)}
|
||||
|
||||
|
||||
|
||||
error_msg = f"处理过程中出错: {e}"
|
||||
if task_id and task_registry and task_lock:
|
||||
add_task_log(task_id, error_msg, task_registry, task_lock)
|
||||
logging.error(error_msg)
|
||||
return {'error': str(e)}
|
||||
Reference in New Issue
Block a user