Skip to content

Commit 9b879f5

Browse files
authored
Merge pull request #88 from ComputerScienceHouse/feature/tag-content-disposition
Tag content-disposition on uploads
2 parents 7524103 + db8465a commit 9b879f5

4 files changed

Lines changed: 30 additions & 16 deletions

File tree

gallery/__init__.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from datetime import timedelta
1010
from sys import stderr
11-
from typing import Any, Dict, Optional, List, Union, cast
11+
from typing import Any, Dict, Optional, List, Union, cast, Tuple
1212

1313
from alembic import command
1414
import click
@@ -180,14 +180,16 @@ def upload_file(auth_dict: Optional[Dict[str, Any]] = None):
180180
assert dir_path
181181
assert parent
182182
assert owner
183-
file_model = add_file(filename, dir_path, parent, "", owner)
183+
mime, file_model = add_file(filename, dir_path, parent, "", owner)
184184

185185
# Upload File
186186
file_stat = os.stat(filepath)
187187
with open(filepath, "rb") as f_hnd:
188188
storage_interface.put(
189189
"files/{}".format(file_model.s3_id),
190-
f_hnd
190+
f_hnd,
191+
filename,
192+
mime
191193
)
192194
os.remove(filepath)
193195

@@ -198,6 +200,8 @@ def upload_file(auth_dict: Optional[Dict[str, Any]] = None):
198200
storage_interface.put(
199201
"thumbnails/" + file_model.s3_id,
200202
f_hnd,
203+
"thumb_" + filename + "." + filepath.split(".")[-1],
204+
"image/gif" if filepath.endswith(".gif") else "image/jpeg"
201205
)
202206
os.remove(filepath)
203207
os.rmdir(dir_path)
@@ -347,6 +351,8 @@ def init_root():
347351
storage_interface.put(
348352
"files/{}".format(DEFAULT_THUMBNAIL_NAME),
349353
f_hnd,
354+
"thumb_" + DEFAULT_THUMBNAIL_NAME + ".jpg",
355+
"image/jpeg"
350356
)
351357

352358

@@ -367,12 +373,12 @@ def add_directory(parent_id: str, name: str, description: str, owner: str):
367373
return dir_model.id
368374

369375

370-
def add_file(file_name: str, path: str, dir_id: str, description: str, owner: str) -> Optional[File]:
376+
def add_file(file_name: str, path: str, dir_id: str, description: str, owner: str) -> Tuple[str, Optional[File]]:
371377
file_path = os.path.join('/', path, file_name)
372378

373-
file_data = parse_file_info(file_path, path)
379+
mime, file_data = parse_file_info(file_path, path)
374380
if file_data is None:
375-
return None
381+
return mime, None
376382

377383
file_model = File(
378384
dir_id,
@@ -389,7 +395,7 @@ def add_file(file_name: str, path: str, dir_id: str, description: str, owner: st
389395
db.session.flush()
390396
db.session.commit()
391397
db.session.refresh(file_model)
392-
return file_model
398+
return mime, file_model
393399

394400

395401
def refresh_directory_thumbnail(dir_model: Directory) -> str:

gallery/file_modules/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import magic
22
import os
3-
from typing import Any, Dict, List, Optional
3+
from typing import Any, Dict, List, Optional, Tuple
44
from wand.image import Image
55
from wand.color import Color
66

@@ -87,15 +87,17 @@ def generate_thumbnail(self):
8787

8888

8989
# classism
90-
def parse_file_info(file_path: str, dir_path: str) -> Optional[FileModule]:
90+
def parse_file_info(file_path: str, dir_path: str) -> Tuple[str, Optional[FileModule]]:
9191
print("entering parse_file_info")
9292
mime_type = magic.from_file(file_path, mime=True)
9393
print(mime_type)
9494
print(file_path)
95+
96+
model = None
9597
if mime_type in file_mimetype_relation:
96-
return file_mimetype_relation[mime_type](file_path, dir_path)
98+
model = file_mimetype_relation[mime_type](file_path, dir_path)
9799

98-
return None
100+
return mime_type, model
99101

100102

101103
def supported_mimetypes() -> List[str]:

gallery/file_store.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class FileStorage(object):
1919
thumbnail data persistantly.
2020
"""
2121

22-
def put(self, key: str, handle: IO[bytes]):
22+
def put(self, key: str, handle: IO[bytes], filename: str, mime: str):
2323
"""
2424
put is used to stream data from a local file descriptor into the backing
2525
storage implementation.
@@ -72,7 +72,7 @@ def _temp_link_handler(self, token: str):
7272
# sign and verify the file path anyway
7373
return send_from_directory(self._base_dir, payload["key"])
7474

75-
def put(self, key: str, handle: IO[bytes]):
75+
def put(self, key: str, handle: IO[bytes], filename: str, mime: str):
7676
local_path = os.path.join(self._base_dir, key)
7777
os.makedirs(os.path.dirname(local_path), exist_ok=True)
7878
with open(local_path, 'wb+') as f:
@@ -103,8 +103,14 @@ def __init__(self, app: flask.Flask):
103103
self._bucket = app.config['S3_BUCKET_ID']
104104
self._link_expiration = timedelta(minutes=5)
105105

106-
def put(self, key: str, handle: IO[bytes]):
107-
self._client.upload_fileobj(handle, self._bucket, key)
106+
def put(self, key: str, handle: IO[bytes], filename: str, mime: str):
107+
self._client.upload_fileobj(
108+
handle, self._bucket, key,
109+
ExtraArgs={
110+
"ContentDisposition": f'inline; filename="{filename}"',
111+
"ContentType": mime,
112+
}
113+
)
108114

109115
def remove(self, key: str):
110116
self._client.delete_object(Bucket=self._bucket, Key=key)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Flask==1.0.2
22
Flask-pyoidc==2.0.0
3-
csh_ldap~=2.2.0
3+
csh_ldap~=2.3.1
44
addict==2.2.0
55
flask_sqlalchemy==2.5
66
flask_migrate==2.3.1

0 commit comments

Comments
 (0)