sharedkernel 2.3.0__tar.gz → 2.4.1__tar.gz

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.
Files changed (40) hide show
  1. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/PKG-INFO +5 -1
  2. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/README.md +4 -0
  3. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/setup.py +1 -1
  4. sharedkernel-2.4.1/sharedkernel/file_validation.py +85 -0
  5. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel.egg-info/PKG-INFO +5 -1
  6. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel.egg-info/SOURCES.txt +1 -0
  7. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/setup.cfg +0 -0
  8. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/common.py +0 -0
  9. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/data_format_converter.py +0 -0
  10. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/database/__init__.py +0 -0
  11. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/database/audit_model.py +0 -0
  12. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/database/mongo_generic_audit_repository.py +0 -0
  13. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/database/mongo_generic_repository.py +0 -0
  14. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/database/pagination_response_dto.py +0 -0
  15. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/date_converter.py +0 -0
  16. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/diff_utils.py +0 -0
  17. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/enum/__init__.py +0 -0
  18. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/enum/error_code.py +0 -0
  19. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/enum/sort_order.py +0 -0
  20. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/exception/__init__.py +0 -0
  21. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/exception/exception.py +0 -0
  22. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/exception/exception_handlers.py +0 -0
  23. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/jwt_service.py +0 -0
  24. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/multipart_upload.py +0 -0
  25. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/normalizer/__init__.py +0 -0
  26. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/normalizer/number_normalizer.py +0 -0
  27. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/normalizer/phone_number_normalizer.py +0 -0
  28. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/normalizer/string_normalizer.py +0 -0
  29. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/objects/__init__.py +0 -0
  30. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/objects/base_document.py +0 -0
  31. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/objects/json_string_model.py +0 -0
  32. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/objects/jwt_model.py +0 -0
  33. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/objects/result.py +0 -0
  34. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/objects/user_info.py +0 -0
  35. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/regex_masking.py +0 -0
  36. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/s3_uploader.py +0 -0
  37. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel/string_extentions.py +0 -0
  38. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel.egg-info/dependency_links.txt +0 -0
  39. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel.egg-info/requires.txt +0 -0
  40. {sharedkernel-2.3.0 → sharedkernel-2.4.1}/sharedkernel.egg-info/top_level.txt +0 -0
@@ -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,10 @@
2
2
  this is a shared kernel package
3
3
 
4
4
  # Change Log
5
+ ### Version 2.4.1
6
+ - Fix file validation
7
+ ### Version 2.4.0
8
+ - Implement file validation
5
9
  ### Version 2.3.0
6
10
  - Implement multipart uploader for S3
7
11
  ### Version 2.2.7
@@ -37,7 +37,7 @@ setup(
37
37
  "deepdiff",
38
38
  ],
39
39
  # *strongly* suggested for sharing
40
- version="2.3.0",
40
+ version="2.4.1",
41
41
  description="sharekernel is a shared package between all python projects",
42
42
  long_description=long_description,
43
43
  long_description_content_type="text/markdown",
@@ -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
@@ -4,6 +4,7 @@ sharedkernel/common.py
4
4
  sharedkernel/data_format_converter.py
5
5
  sharedkernel/date_converter.py
6
6
  sharedkernel/diff_utils.py
7
+ sharedkernel/file_validation.py
7
8
  sharedkernel/jwt_service.py
8
9
  sharedkernel/multipart_upload.py
9
10
  sharedkernel/regex_masking.py
File without changes