2025-09-16 18:53:58 +00:00
|
|
|
|
from flask import Flask, send_from_directory, request, jsonify
|
2025-09-16 19:41:05 +00:00
|
|
|
|
import os, shutil, time, threading, datetime
|
2025-09-16 18:53:58 +00:00
|
|
|
|
from werkzeug.utils import secure_filename
|
2025-09-16 19:14:17 +00:00
|
|
|
|
from flask_cors import CORS
|
|
|
|
|
|
from pathlib import Path
|
2025-09-16 18:53:58 +00:00
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
2025-09-16 19:14:17 +00:00
|
|
|
|
CORS(app)
|
|
|
|
|
|
BASE_DIR = Path(__file__).resolve().parent
|
|
|
|
|
|
UPLOAD_DIR = BASE_DIR / "uploads"
|
2025-09-16 18:53:58 +00:00
|
|
|
|
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
def run_flask():
|
|
|
|
|
|
app.run(host="10.147.18.141", port=5000)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
|
|
def index():
|
|
|
|
|
|
return "<h1>Hello</h1><p>This is the backend of our cellpose server, please visit our website.</p>"
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/testdl")
|
|
|
|
|
|
def test_download():
|
2025-09-16 19:14:17 +00:00
|
|
|
|
return send_from_directory("test_output/2025-09-16-20-03-51", "img_overlay.png", as_attachment=True)
|
2025-09-16 18:53:58 +00:00
|
|
|
|
|
|
|
|
|
|
@app.route("/dl/<timestamp>")
|
|
|
|
|
|
def download(timestamp):
|
2025-09-16 19:14:17 +00:00
|
|
|
|
input_dir = os.path.join("output", timestamp)
|
|
|
|
|
|
output_dir = os.path.join("output/tmp", timestamp) # 不要加 .zip,make_archive 会自动加
|
|
|
|
|
|
os.makedirs("output/tmp", exist_ok=True) # 确保 tmp 存在
|
2025-09-16 18:53:58 +00:00
|
|
|
|
shutil.make_archive(output_dir, 'zip', input_dir)
|
|
|
|
|
|
print(f"压缩完成: {output_dir}.zip")
|
2025-09-16 19:14:17 +00:00
|
|
|
|
return send_from_directory("output/tmp", f"{timestamp}.zip", as_attachment=True)
|
2025-09-16 18:53:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/upload")
|
|
|
|
|
|
def upload():
|
2025-09-16 19:41:05 +00:00
|
|
|
|
ts = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
|
|
|
|
|
|
os.makedirs(UPLOAD_DIR / ts, exist_ok=True)
|
2025-09-16 19:14:17 +00:00
|
|
|
|
files = request.files.getlist("files")
|
2025-09-16 18:53:58 +00:00
|
|
|
|
saved = []
|
|
|
|
|
|
for f in files:
|
|
|
|
|
|
if not f or f.filename == "":
|
|
|
|
|
|
continue
|
|
|
|
|
|
name = secure_filename(f.filename)
|
2025-09-16 19:41:05 +00:00
|
|
|
|
f.save(os.path.join(UPLOAD_DIR / ts, name))
|
2025-09-16 18:53:58 +00:00
|
|
|
|
saved.append(name)
|
2025-09-16 19:41:05 +00:00
|
|
|
|
return jsonify({"ok": True, "count": len(saved), "files": saved, "id": ts})
|