sharedkernel 2.3.0__py3-none-any.whl → 2.4.1__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.
@@ -0,0 +1,85 @@
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/x-wav",
23
+ "audio/mp3",
24
+ "application/zip",
25
+ )
26
+
27
+
28
+ def get_mime_type(fileobj) -> str:
29
+ """
30
+ Detects the real MIME type of a file based on its content (magic bytes),
31
+ not the client-provided extension or Content-Type header.
32
+ """
33
+ try:
34
+ fileobj.seek(0)
35
+ sample = fileobj.read(2048)
36
+ fileobj.seek(0)
37
+ m = magic.Magic(mime=True)
38
+ mime = m.from_buffer(sample)
39
+ return mime or "application/octet-stream"
40
+ except Exception:
41
+ return "application/octet-stream"
42
+
43
+
44
+ def validate_file_type(fileobj, allowed_mimes=None):
45
+ """
46
+ Validates the uploaded file’s real MIME type against the allowed list.
47
+ """
48
+ allowed_mimes = allowed_mimes or DEFAULT_ALLOWED_MIME_TYPES
49
+ mime = get_mime_type(fileobj)
50
+ if mime not in allowed_mimes:
51
+ raise ValueError(f"قالب فایل '{fileobj.name}' معتبر نیست. لطفاً یکی از قالب‌های مجاز را انتخاب کنید.")
52
+ return mime
53
+
54
+
55
+ def is_upload_file(obj):
56
+ return (
57
+ hasattr(obj, "file") and
58
+ hasattr(obj, "filename") and
59
+ callable(getattr(obj.file, "read", None)) and
60
+ callable(getattr(obj.file, "seek", None))
61
+ )
62
+
63
+
64
+ def validate_upload_file(allowed_mimes=None):
65
+ allowed_mimes = allowed_mimes or DEFAULT_ALLOWED_MIME_TYPES
66
+
67
+ def decorator(func):
68
+ @wraps(func)
69
+ async def wrapper(*args, **kwargs):
70
+ for value in list(args) + list(kwargs.values()):
71
+ if is_upload_file(value):
72
+ try:
73
+ validate_file_type(value.file, allowed_mimes)
74
+ except ValueError as e:
75
+ raise HTTPException(status_code=400, detail=str(e))
76
+ elif isinstance(value, list):
77
+ for f in value:
78
+ if is_upload_file(f):
79
+ try:
80
+ validate_file_type(f.file, allowed_mimes)
81
+ except ValueError as e:
82
+ raise HTTPException(status_code=400, detail=str(e))
83
+ return await func(*args, **kwargs)
84
+ return wrapper
85
+ return decorator
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sharedkernel
3
- Version: 2.3.0
3
+ Version: 2.4.1
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,10 @@ Dynamic: summary
29
29
  this is a shared kernel package
30
30
 
31
31
  # Change Log
32
+ ### Version 2.4.1
33
+ - Fix file validation
34
+ ### Version 2.4.0
35
+ - Implement file validation
32
36
  ### Version 2.3.0
33
37
  - Implement multipart uploader for S3
34
38
  ### 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=X-vdUy1Se2li3f-_Ro5UJi_Muqz-QmkKu6b5eHs-eiA,2892
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.3.0.dist-info/METADATA,sha256=sJZef9JTB6R7oQ5kGWKCQPuV6d7vIztvimh2VWN6Uh4,3339
32
- sharedkernel-2.3.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
33
- sharedkernel-2.3.0.dist-info/top_level.txt,sha256=TVTOnV1MItSSlpSjqkiijuHkoVsGHS4CArpsM-lylkE,13
34
- sharedkernel-2.3.0.dist-info/RECORD,,
32
+ sharedkernel-2.4.1.dist-info/METADATA,sha256=rE0UStixLUZxRxksyyQdoUTFXkaR-cMqSkM4oA3LhXM,3425
33
+ sharedkernel-2.4.1.dist-info/WHEEL,sha256=YCfwYGOYMi5Jhw2fU4yNgwErybb2IX5PEwBKV4ZbdBo,91
34
+ sharedkernel-2.4.1.dist-info/top_level.txt,sha256=TVTOnV1MItSSlpSjqkiijuHkoVsGHS4CArpsM-lylkE,13
35
+ sharedkernel-2.4.1.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.9.0)
2
+ Generator: setuptools (82.0.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5