增加下载 pdf 文件名输入框

This commit is contained in:
William Jin 2024-09-23 07:27:17 +08:00
parent 5d5ec57e3c
commit 17a6524697

View File

@ -143,7 +143,7 @@ def remove_headers(curl_command):
return curl_command return curl_command
def do_download_pdf_file(curl_command): def do_download_pdf_file(curl_command, pdf_filename):
# 去除不需要的标头 # 去除不需要的标头
curl_command = remove_headers(curl_command) curl_command = remove_headers(curl_command)
@ -151,7 +151,10 @@ def do_download_pdf_file(curl_command):
try: try:
result = subprocess.run(curl_command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) result = subprocess.run(curl_command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# 保存文件到临时文件 # 保存文件到临时文件
temp_file_path = "./download.pdf" if pdf_filename != "":
temp_file_path = f"./{pdf_filename}.pdf"
else:
temp_file_path = "./download.pdf"
with open(temp_file_path, 'wb') as f: with open(temp_file_path, 'wb') as f:
f.write(result.stdout) f.write(result.stdout)
return "File downloaded successfully", temp_file_path return "File downloaded successfully", temp_file_path
@ -159,8 +162,8 @@ def do_download_pdf_file(curl_command):
return f"Failed to download file. Error: {e.stderr.decode()}", None return f"Failed to download file. Error: {e.stderr.decode()}", None
def download_pdf(curl_command): def download_pdf(curl_command, pdf_filename):
message, file_path = do_download_pdf_file(curl_command) message, file_path = do_download_pdf_file(curl_command, pdf_filename)
return file_path return file_path
@ -217,11 +220,14 @@ with gr.Blocks() as iface:
with gr.Row(): with gr.Row():
curl_text_input = gr.Textbox(lines=10, placeholder="请在此输入cURL脚本...") curl_text_input = gr.Textbox(lines=10, placeholder="请在此输入cURL脚本...")
with gr.Row(): with gr.Row():
pdf_download_button = gr.Button("下载") with gr.Column():
pdf_filename = gr.Textbox(placeholder="输入文件名")
with gr.Column():
pdf_download_button = gr.Button("下载")
with gr.Row(): with gr.Row():
pdf_download_result = gr.File(label="下载pdf文件") pdf_download_result = gr.File(label="下载pdf文件")
pdf_download_button.click(download_pdf, inputs=curl_text_input, outputs=pdf_download_result) pdf_download_button.click(download_pdf, inputs=[curl_text_input, pdf_filename], outputs=pdf_download_result)
iface.load(fn=update_log_output, outputs=[log_output], every=1) iface.load(fn=update_log_output, outputs=[log_output], every=1)