From 26f96b917b5c9fdc2449c31519aa22f6c9a09f2c Mon Sep 17 00:00:00 2001 From: 24024 <240241002@qq.com> Date: Wed, 21 Jan 2026 19:32:02 +0800 Subject: [PATCH] feat: Implement user history retrieval and MinIO file proxy with updated public URL configuration. --- app.py | 26 ++++++++++++++++++++++++-- config.py | 2 +- services/history_service.py | 16 ++++++++++++++-- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 7dbf858..ec5bd79 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,6 @@ -from flask import Flask, render_template, jsonify +from flask import Flask, render_template, jsonify, Response, stream_with_context from config import Config -from extensions import db, redis_client, migrate +from extensions import db, redis_client, migrate, s3_client from blueprints.auth import auth_bp from blueprints.api import api_bp from blueprints.admin import admin_bp @@ -81,6 +81,28 @@ def create_app(): @app.route('/video') def video_page(): return render_template('video.html') + + @app.route('/files/') + def get_file(filename): + """Proxy route to serve files from MinIO via the backend""" + try: + # Use s3_client to get the object + file_obj = s3_client.get_object(Bucket=Config.MINIO["bucket"], Key=filename) + + def generate(): + for chunk in file_obj['Body'].iter_chunks(chunk_size=4096): + yield chunk + + return Response( + stream_with_context(generate()), + mimetype=file_obj['ContentType'], + headers={ + "Cache-Control": "public, max-age=86400" + } + ) + except Exception as e: + # system_logger.error(f"File proxy error: {str(e)}") # Optional logging + return jsonify({"error": "File not found"}), 404 # 自动创建数据库表 with app.app_context(): diff --git a/config.py b/config.py index 9f95722..1421ee6 100644 --- a/config.py +++ b/config.py @@ -27,7 +27,7 @@ class Config: "access_key": "l0VlsxrkASbXN2YSQrJk", "secret_key": "ZK8nXHieorl3fpbssUMGGfr8zZmbpXB5gAbma3z1", "bucket": "images", - "public_url": "http://331002.xyz:9000/images/" + "public_url": "/files/" } # AI API 配置 diff --git a/services/history_service.py b/services/history_service.py index 23b15df..5e9a836 100644 --- a/services/history_service.py +++ b/services/history_service.py @@ -26,6 +26,11 @@ def get_user_history_data(user_id, page=1, per_page=10, filter_type='all'): .paginate(page=page, per_page=per_page, error_out=False) # 格式化 URL,兼容新旧数据格式 + def fix_minio_url(url): + if url and ":9000/images/" in url: + return "/files/" + url.split(":9000/images/")[-1] + return url + history_list = [] for r in pagination.items: raw_urls = json.loads(r.image_urls) @@ -33,9 +38,16 @@ def get_user_history_data(user_id, page=1, per_page=10, filter_type='all'): for u in raw_urls: if isinstance(u, str): # 旧数据:直接返回原图作为缩略图 - formatted_urls.append({"url": u, "thumb": u}) + fixed_u = fix_minio_url(u) + formatted_urls.append({"url": fixed_u, "thumb": fixed_u}) else: - # 如果是视频类型,提供默认预览图 (此处使用一个公共视频占位图或空) + # 修复对象格式中的 URL + if 'url' in u: + u['url'] = fix_minio_url(u['url']) + if 'thumb' in u: + u['thumb'] = fix_minio_url(u['thumb']) + + # 如果是视频类型,提供默认预览图 if u.get('type') == 'video' and not u.get('thumb'): u['thumb'] = "https://img.icons8.com/flat-round/64/000000/play--v1.png" formatted_urls.append(u)