from flask import Flask, send_from_directory
import os
import atexit
app = Flask(__name__)
# 假设这是你的文件高亮处理类
class FileHighlighter:
def __init__(self, file_path, chunk_id_list):
self.file_path = file_path
self.chunk_id_list = chunk_id_list
# 这里省略了 FileHighlighter 的其他实现细节
def highlight_file(self):
# 这里省略了 highlight_file 的实现细节
pass
@property
def new_file_path(self):
# 返回处理后的新文件路径
return "path/to/new/highlighted/file"
def delete_file_on_request_end(new_file_path):
"""在请求结束后删除文件"""
def cleanup():
if os.path.exists(new_file_path):
os.remove(new_file_path)
atexit.register(cleanup)
@app.route('/highlight/<file_path>/<chunk_id_list>')
def highlight_and_send(file_path, chunk_id_list):
with FileHighlighter(file_path, chunk_id_list) as highlighter:
highlighter.highlight_file()
delete_file_on_request_end(highlighter.new_file_path)
return send_from_directory(directory=os.getcwd(), path=highlighter.new_file_path, as_attachment=True)
if __name__ == '__main__':
app.run()
标签:__,文件,请求,删除,file,new,path,id,def
From: https://www.cnblogs.com/dreammooncy/p/18236972