35 lines
1002 B
Python
35 lines
1002 B
Python
|
|
import os
|
||
|
|
import uuid
|
||
|
|
import time
|
||
|
|
import requests
|
||
|
|
from urllib.parse import quote
|
||
|
|
from werkzeug.utils import secure_filename
|
||
|
|
from config import Config
|
||
|
|
from extensions import s3_client
|
||
|
|
|
||
|
|
def handle_file_uploads(files):
|
||
|
|
"""处理文件上传到 MinIO"""
|
||
|
|
img_urls = []
|
||
|
|
for f in files:
|
||
|
|
ext = os.path.splitext(f.filename)[1]
|
||
|
|
filename = f"{uuid.uuid4().hex}{ext}"
|
||
|
|
s3_client.upload_fileobj(
|
||
|
|
f, Config.MINIO["bucket"], filename,
|
||
|
|
ExtraArgs={"ContentType": f.content_type}
|
||
|
|
)
|
||
|
|
img_urls.append(f"{Config.MINIO['public_url']}{quote(filename)}")
|
||
|
|
return img_urls
|
||
|
|
|
||
|
|
def get_remote_file_stream(url):
|
||
|
|
"""获取远程文件的流"""
|
||
|
|
req = requests.get(url, stream=True, timeout=60)
|
||
|
|
req.raise_for_status()
|
||
|
|
|
||
|
|
headers = {}
|
||
|
|
if req.headers.get('Content-Type'):
|
||
|
|
headers['Content-Type'] = req.headers['Content-Type']
|
||
|
|
else:
|
||
|
|
headers['Content-Type'] = 'application/octet-stream'
|
||
|
|
|
||
|
|
return req, headers
|