fastapi-file-validator 0.0.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.
@@ -0,0 +1,14 @@
1
+ Metadata-Version: 2.4
2
+ Name: fastapi-file-validator
3
+ Version: 0.0.0
4
+ Summary: A lightweight, flexible file extension validator and MIME type checker for fastapi
5
+ Author-email: Seungkyu-Han <trust1204@gmail.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/Seungkyu-Han/fastapi-file-validator
8
+ Project-URL: Bug Tracker, https://github.com/Seungkyu-Han/fastapi-file-validator
9
+ Project-URL: Documentation, https://github.com/Seungkyu-Han/fastapi-file-validator
10
+ Project-URL: Source, https://github.com/Seungkyu-Han/fastapi-file-validator
11
+ Keywords: validator
12
+ Requires-Python: >=3.10
13
+ Description-Content-Type: text/markdown
14
+ Requires-Dist: fastapi>=0.110.1
@@ -0,0 +1,7 @@
1
+ file_validator/__init__.py,sha256=45cYSsnJB7JHfv2j3_8TZLEJmOhoXw97e_WiGMya3Lc,189
2
+ file_validator/file_extension_validator.py,sha256=6JHv4L81fbE0aQFoIejr4EdFEUveQfpxbJJDo15K9lo,1249
3
+ file_validator/file_size_validator.py,sha256=4hm_cx-MwnoQT9-_t0LMrBXTrfVRlp_Dg_2si-fbYNk,1028
4
+ fastapi_file_validator-0.0.0.dist-info/METADATA,sha256=Nzb9bi9APuM4XoBPxM4dMuK_oRJ924ff-Dnl0BUwIRU,654
5
+ fastapi_file_validator-0.0.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
6
+ fastapi_file_validator-0.0.0.dist-info/top_level.txt,sha256=eyZ1S8dWz5evkVYPVPaEUqBRn5nlw1QYV0sp0h72aBY,15
7
+ fastapi_file_validator-0.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ file_validator
@@ -0,0 +1,7 @@
1
+ from .file_extension_validator import file_extension_validator
2
+ from .file_size_validator import file_size_validator
3
+
4
+ __all__ = [
5
+ "file_extension_validator",
6
+ "file_size_validator",
7
+ ]
@@ -0,0 +1,42 @@
1
+ import os
2
+
3
+ from fastapi import UploadFile, HTTPException, status
4
+ from functools import wraps
5
+
6
+
7
+ def file_extension_validator(file_arg_name: str, file_extensions: set[str]):
8
+ def decorator(func):
9
+ @wraps(func)
10
+ async def wrapper(*args, **kwargs):
11
+ if file_arg_name in kwargs:
12
+
13
+ file_arg = kwargs.get(file_arg_name)
14
+
15
+ if not isinstance(file_arg, UploadFile):
16
+ return await func(*args, **kwargs)
17
+
18
+ file: UploadFile = file_arg
19
+
20
+ filename: str | None = file.filename
21
+
22
+ if not filename:
23
+ raise HTTPException(
24
+ status_code=status.HTTP_400_BAD_REQUEST,
25
+ detail=f"filename is empty",
26
+ )
27
+
28
+ _, ext = os.path.splitext(filename)
29
+
30
+ file_extension = ext[1:].lower()
31
+
32
+ if file_extension not in file_extensions:
33
+ raise HTTPException(
34
+ status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
35
+ detail=f"{file_extension} type is unavailable",
36
+ )
37
+
38
+ return await func(*args, **kwargs)
39
+
40
+ return wrapper
41
+
42
+ return decorator
@@ -0,0 +1,34 @@
1
+ from fastapi import UploadFile, HTTPException, status
2
+ from functools import wraps
3
+
4
+
5
+ def file_size_validator(file_arg_name: str, max_size_mb: int):
6
+ max_size_bytes = max_size_mb * 1024 * 1024
7
+
8
+ def decorator(func):
9
+ @wraps(func)
10
+ async def wrapper(*args, **kwargs):
11
+ if file_arg_name in kwargs:
12
+ file_arg = kwargs.get(file_arg_name)
13
+
14
+ if not isinstance(file_arg, UploadFile):
15
+ return await func(*args, **kwargs)
16
+
17
+ file: UploadFile = file_arg
18
+
19
+ file_size: int | None = file.size
20
+
21
+ if not file_size:
22
+ return await func(*args, **kwargs)
23
+
24
+ if file_size > max_size_bytes:
25
+ raise HTTPException(
26
+ status_code=status.HTTP_413_CONTENT_TOO_LARGE,
27
+ detail=f"file size is bigger than {max_size_mb}MB",
28
+ )
29
+
30
+ return await func(*args, **kwargs)
31
+
32
+ return wrapper
33
+
34
+ return decorator