Added filename setting for the caff endpoint

This commit is contained in:
Pünkösd Marcell 2020-11-28 20:52:46 +01:00
parent 4f95cb7f2f
commit beb259b4d8
1 changed files with 17 additions and 5 deletions

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import string
from flask import abort, Response, current_app from flask import abort, Response, current_app
from flask_classful import FlaskView from flask_classful import FlaskView
from flask_security import login_required, current_user from flask_security import login_required, current_user
@ -14,18 +15,22 @@ class ContentView(FlaskView):
This is just a simple content proxy with access control This is just a simple content proxy with access control
""" """
def _stream_from_minio(self, bucket: str, filename: str): def _stream_from_minio(self, bucket: str, id_: int, filename: str = None):
try: try:
data = storage.connection.get_object(bucket, filename) data = storage.connection.get_object(bucket, str(id_))
except NoSuchKey: except NoSuchKey:
abort(404) abort(404)
return Response(data.stream(), mimetype=data.headers['Content-type']) headers = {}
if filename:
headers['Content-Disposition'] = f'attachment; filename="{filename}"'
return Response(data.stream(), mimetype=data.headers['Content-type'], headers=headers)
def preview(self, id_: int): def preview(self, id_: int):
i = Item.query.get_or_404(id_) i = Item.query.get_or_404(id_)
return self._stream_from_minio(current_app.config['MINIO_PREVIEW_BUCKET_NAME'], str(i.id)) return self._stream_from_minio(current_app.config['MINIO_PREVIEW_BUCKET_NAME'], i.id)
@login_required @login_required
def caff(self, id_: int): def caff(self, id_: int):
@ -34,4 +39,11 @@ class ContentView(FlaskView):
if not p: if not p:
abort(403) abort(403)
return self._stream_from_minio(current_app.config['MINIO_CAFF_BUCKET_NAME'], str(p.item.id)) filename = ''.join(filter(lambda x: x in string.ascii_lowercase, p.item.name))
if not filename:
filename = str(p.item.id)
filename += f'_{p.id}.caff'
return self._stream_from_minio(current_app.config['MINIO_CAFF_BUCKET_NAME'], p.item.id, filename)