该用 curl 脚本,直接下载到服务器并供用户下载
This commit is contained in:
parent
acd7853986
commit
5d5ec57e3c
@ -136,19 +136,32 @@ def convert_to_docx(text):
|
||||
return "file.docx"
|
||||
|
||||
|
||||
def power_shell_pdf_download(powershell_script):
|
||||
def remove_headers(curl_command):
|
||||
# 使用正则表达式去除 if-none-match 和 range 标头
|
||||
powershell_script = re.sub(r'\s*"if-none-match"="`"[^"]*`""\s*', '\n ', powershell_script)
|
||||
powershell_script = re.sub(r'\s*"range"="[^"]*"\s*', '\n ', powershell_script)
|
||||
|
||||
# 在脚本最后加上 "-OutFile 1.pdf"
|
||||
powershell_script = powershell_script.strip() + ' -OutFile 1.pdf'
|
||||
return powershell_script
|
||||
curl_command = re.sub(r'(-H\s*\'if-none-match:[^\\]*\'\s*)', '', curl_command)
|
||||
curl_command = re.sub(r'(-H\s*\'range:[^\\]*\'\s*)', '', curl_command)
|
||||
return curl_command
|
||||
|
||||
|
||||
def copy_text(text):
|
||||
clipman.set(text)
|
||||
return "Text copied to clipboard!"
|
||||
def do_download_pdf_file(curl_command):
|
||||
# 去除不需要的标头
|
||||
curl_command = remove_headers(curl_command)
|
||||
|
||||
# 使用 subprocess 调用 curl 命令
|
||||
try:
|
||||
result = subprocess.run(curl_command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
# 保存文件到临时文件
|
||||
temp_file_path = "./download.pdf"
|
||||
with open(temp_file_path, 'wb') as f:
|
||||
f.write(result.stdout)
|
||||
return "File downloaded successfully", temp_file_path
|
||||
except subprocess.CalledProcessError as e:
|
||||
return f"Failed to download file. Error: {e.stderr.decode()}", None
|
||||
|
||||
|
||||
def download_pdf(curl_command):
|
||||
message, file_path = do_download_pdf_file(curl_command)
|
||||
return file_path
|
||||
|
||||
|
||||
with gr.Blocks() as iface:
|
||||
@ -202,14 +215,13 @@ with gr.Blocks() as iface:
|
||||
with gr.Tab("下载pdf"):
|
||||
gr.Markdown("## pdf 下载指令修复")
|
||||
with gr.Row():
|
||||
power_shell_text_input = gr.Textbox(lines=10, placeholder="请在此输入power shell 脚本...")
|
||||
curl_text_input = gr.Textbox(lines=10, placeholder="请在此输入cURL脚本...")
|
||||
with gr.Row():
|
||||
power_shell_pdf_button = gr.Button("转换")
|
||||
pdf_download_button = gr.Button("下载")
|
||||
with gr.Row():
|
||||
power_shell_text_output = gr.Textbox(lines=10, placeholder="这里会显示结果", show_copy_button=True)
|
||||
pdf_download_result = gr.File(label="下载pdf文件")
|
||||
|
||||
power_shell_pdf_button.click(power_shell_pdf_download, inputs=power_shell_text_input,
|
||||
outputs=power_shell_text_output)
|
||||
pdf_download_button.click(download_pdf, inputs=curl_text_input, outputs=pdf_download_result)
|
||||
|
||||
iface.load(fn=update_log_output, outputs=[log_output], every=1)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user