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()