Skip to content

Commit 3dbb6b1

Browse files
committed
handle markdown mime
1 parent 1906c93 commit 3dbb6b1

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lfss/eng/database.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ async def save_file(
102102
raise StorageExceededError(f"Unable to save file, user {user.username} has storage limit of {user.max_storage}, used {user_size_used}, requested {file_size}")
103103

104104
# check mime type
105+
if mime_type is None:
106+
def custom_mime_checker(url: str) -> Optional[str]:
107+
last_component = url.split('/')[-1]
108+
if not '.' in last_component:
109+
return None
110+
ext = last_component.split('.')[-1].lower()
111+
if ext == 'md':
112+
return 'text/markdown'
113+
return None
114+
mime_type = custom_mime_checker(url)
105115
if mime_type is None:
106116
mime_type, _ = mimetypes.guess_type(url)
107117
if mime_type is None:

test/cases/test_15_mime_type.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
import subprocess
3+
from .common import create_server_context, get_conn
4+
from ..config import SANDBOX_DIR
5+
6+
server = create_server_context()
7+
8+
def test_init_user_creation(server):
9+
subprocess.check_output(['lfss-user', 'add', 'u0', 'test', "--admin"], cwd=SANDBOX_DIR)
10+
11+
def test_markdown_mime_type(server):
12+
u0 = get_conn('u0')
13+
u0.put('u0/readme.md', b'# Hello World\nThis is a markdown file.')
14+
fmeta = u0.get_fmeta('u0/readme.md')
15+
assert fmeta is not None, "Get file meta failed"
16+
assert fmeta.mime_type == 'text/markdown', f"Mime type is not correct, got {fmeta.mime_type}"
17+
18+
def test_json_mime_type(server):
19+
u0 = get_conn('u0')
20+
u0.put_json('u0/data.json', {'key': 'value'})
21+
fmeta = u0.get_fmeta('u0/data.json')
22+
assert fmeta is not None, "Get file meta failed"
23+
assert fmeta.mime_type == 'application/json', f"Mime type is not correct, got {fmeta.mime_type}"
24+
25+
def test_png_infer(server):
26+
u0 = get_conn('u0')
27+
png_data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\nIDATx\xdac\xf8\x0f\x00\x01\x01\x01\x00\x18\xdd\x8d\xe5\x00\x00\x00\x00IEND\xaeB`\x82'
28+
u0.put('u0/image.png', png_data)
29+
fmeta = u0.get_fmeta('u0/image.png')
30+
assert fmeta is not None, "Get file meta failed"
31+
assert fmeta.mime_type == 'image/png', f"Mime type is not correct, got {fmeta.mime_type}"
32+
33+
def test_known_extension(server):
34+
u0 = get_conn('u0')
35+
u0.put('u0/document.102ng-aah2', b'\a\r\nThis is a test document with a known extension.')
36+
fmeta = u0.get_fmeta('u0/document.102ng-aah2')
37+
assert fmeta is not None, "Get file meta failed"
38+
assert fmeta.mime_type == 'application/octet-stream', f"Mime type is not correct, got {fmeta.mime_type}"

0 commit comments

Comments
 (0)