sharedkernel 2.3.0__py3-none-any.whl → 2.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sharedkernel/file_validation.py +83 -0
- {sharedkernel-2.3.0.dist-info → sharedkernel-2.4.0.dist-info}/METADATA +3 -1
- {sharedkernel-2.3.0.dist-info → sharedkernel-2.4.0.dist-info}/RECORD +5 -4
- {sharedkernel-2.3.0.dist-info → sharedkernel-2.4.0.dist-info}/WHEEL +0 -0
- {sharedkernel-2.3.0.dist-info → sharedkernel-2.4.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
from functools import wraps
|
|
2
|
+
from fastapi import HTTPException
|
|
3
|
+
import magic
|
|
4
|
+
|
|
5
|
+
DEFAULT_ALLOWED_MIME_TYPES = (
|
|
6
|
+
"image/jpeg",
|
|
7
|
+
"image/png",
|
|
8
|
+
"image/gif",
|
|
9
|
+
"image/webp",
|
|
10
|
+
"application/pdf",
|
|
11
|
+
"application/msword",
|
|
12
|
+
"application/x-ole-storage",
|
|
13
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
14
|
+
"application/vnd.ms-excel",
|
|
15
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
16
|
+
"application/vnd.ms-powerpoint",
|
|
17
|
+
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
18
|
+
"text/plain",
|
|
19
|
+
"text/csv",
|
|
20
|
+
"audio/mpeg",
|
|
21
|
+
"audio/wav",
|
|
22
|
+
"audio/mp3",
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def get_mime_type(fileobj) -> str:
|
|
27
|
+
"""
|
|
28
|
+
Detects the real MIME type of a file based on its content (magic bytes),
|
|
29
|
+
not the client-provided extension or Content-Type header.
|
|
30
|
+
"""
|
|
31
|
+
try:
|
|
32
|
+
fileobj.seek(0)
|
|
33
|
+
sample = fileobj.read(2048)
|
|
34
|
+
fileobj.seek(0)
|
|
35
|
+
m = magic.Magic(mime=True)
|
|
36
|
+
mime = m.from_buffer(sample)
|
|
37
|
+
return mime or "application/octet-stream"
|
|
38
|
+
except Exception:
|
|
39
|
+
return "application/octet-stream"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def validate_file_type(fileobj, allowed_mimes=None):
|
|
43
|
+
"""
|
|
44
|
+
Validates the uploaded file’s real MIME type against the allowed list.
|
|
45
|
+
"""
|
|
46
|
+
allowed_mimes = allowed_mimes or DEFAULT_ALLOWED_MIME_TYPES
|
|
47
|
+
mime = get_mime_type(fileobj)
|
|
48
|
+
if mime not in allowed_mimes:
|
|
49
|
+
raise ValueError(f"نوع فایل مجاز نیست: {mime}")
|
|
50
|
+
return mime
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def is_upload_file(obj):
|
|
54
|
+
return (
|
|
55
|
+
hasattr(obj, "file") and
|
|
56
|
+
hasattr(obj, "filename") and
|
|
57
|
+
callable(getattr(obj.file, "read", None)) and
|
|
58
|
+
callable(getattr(obj.file, "seek", None))
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def validate_upload_file(allowed_mimes=None):
|
|
63
|
+
allowed_mimes = allowed_mimes or DEFAULT_ALLOWED_MIME_TYPES
|
|
64
|
+
|
|
65
|
+
def decorator(func):
|
|
66
|
+
@wraps(func)
|
|
67
|
+
async def wrapper(*args, **kwargs):
|
|
68
|
+
for value in list(args) + list(kwargs.values()):
|
|
69
|
+
if is_upload_file(value):
|
|
70
|
+
try:
|
|
71
|
+
validate_file_type(value.file, allowed_mimes)
|
|
72
|
+
except ValueError as e:
|
|
73
|
+
raise HTTPException(status_code=400, detail=str(e))
|
|
74
|
+
elif isinstance(value, list):
|
|
75
|
+
for f in value:
|
|
76
|
+
if is_upload_file(f):
|
|
77
|
+
try:
|
|
78
|
+
validate_file_type(f.file, allowed_mimes)
|
|
79
|
+
except ValueError as e:
|
|
80
|
+
raise HTTPException(status_code=400, detail=str(e))
|
|
81
|
+
return await func(*args, **kwargs)
|
|
82
|
+
return wrapper
|
|
83
|
+
return decorator
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sharedkernel
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.4.0
|
|
4
4
|
Summary: sharekernel is a shared package between all python projects
|
|
5
5
|
Author: Smilinno
|
|
6
6
|
Description-Content-Type: text/markdown
|
|
@@ -29,6 +29,8 @@ Dynamic: summary
|
|
|
29
29
|
this is a shared kernel package
|
|
30
30
|
|
|
31
31
|
# Change Log
|
|
32
|
+
### Version 2.4.0
|
|
33
|
+
- Implement file validation
|
|
32
34
|
### Version 2.3.0
|
|
33
35
|
- Implement multipart uploader for S3
|
|
34
36
|
### Version 2.2.7
|
|
@@ -2,6 +2,7 @@ sharedkernel/common.py,sha256=HL5vsuJBaIeBcoXA8Hbe6jnYAv4danIszo5Y7G2gGDA,622
|
|
|
2
2
|
sharedkernel/data_format_converter.py,sha256=GWGbfhKJBifkz-cfnqKAFjJM43WC0qdq9KSELj3xR30,3774
|
|
3
3
|
sharedkernel/date_converter.py,sha256=Cjd4ewm0pIfQzv7nlgAAB_EYrr-VvXxQGehJCNphgXc,4491
|
|
4
4
|
sharedkernel/diff_utils.py,sha256=mtwJmc05GAXUOB0ZLtqAhfBT1kGoSQ7qmP5N44P73ho,2564
|
|
5
|
+
sharedkernel/file_validation.py,sha256=kedQRckWaMf6fBZ7yFtkfLTt0jvhEwO_w0fxBpW8PZY,2758
|
|
5
6
|
sharedkernel/jwt_service.py,sha256=KSkrpXVqmKMGdaoDg0DqhOfzR9CIGVTg7HfOlAaz1Zo,1611
|
|
6
7
|
sharedkernel/multipart_upload.py,sha256=JVlCBlznB9dWh2_spjAqzLOqQT1CHUTvrR4m7ug8qaM,1877
|
|
7
8
|
sharedkernel/regex_masking.py,sha256=zQrgteP8Cuq1EC9B7QUJqAXUxK9ISD9kWMYK2AbRfw0,3288
|
|
@@ -28,7 +29,7 @@ sharedkernel/objects/json_string_model.py,sha256=j63tnoqiok0EmBP6T-ChYuQYKPw7mLq
|
|
|
28
29
|
sharedkernel/objects/jwt_model.py,sha256=XQHQhTbg7PT8XiUh5fd9MwRH4ldPsesI_hfbjaSqdKg,134
|
|
29
30
|
sharedkernel/objects/result.py,sha256=I_9hX5TPEO1oStzuFLjFh1rtimXorz7ml-OaW_2BMvc,680
|
|
30
31
|
sharedkernel/objects/user_info.py,sha256=51WyspRxlIWzK7Lfxgqg4D6mylXeHe9ZSenf-RhYTdA,286
|
|
31
|
-
sharedkernel-2.
|
|
32
|
-
sharedkernel-2.
|
|
33
|
-
sharedkernel-2.
|
|
34
|
-
sharedkernel-2.
|
|
32
|
+
sharedkernel-2.4.0.dist-info/METADATA,sha256=ZvKXI2BBibdquCpAdpWO0CRslbPOVuajeBiM5C9ikEo,3385
|
|
33
|
+
sharedkernel-2.4.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
34
|
+
sharedkernel-2.4.0.dist-info/top_level.txt,sha256=TVTOnV1MItSSlpSjqkiijuHkoVsGHS4CArpsM-lylkE,13
|
|
35
|
+
sharedkernel-2.4.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|