feat: Implement user history retrieval and MinIO file proxy with updated public URL configuration.
This commit is contained in:
parent
ced1020235
commit
26f96b917b
26
app.py
26
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
|
||||
@ -82,6 +82,28 @@ def create_app():
|
||||
def video_page():
|
||||
return render_template('video.html')
|
||||
|
||||
@app.route('/files/<path:filename>')
|
||||
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():
|
||||
print("🔧 正在检查并创建数据库表...")
|
||||
|
||||
@ -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 配置
|
||||
|
||||
@ -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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user