fastapi-file-validator 0.0.0__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.
- fastapi_file_validator-0.0.0/PKG-INFO +14 -0
- fastapi_file_validator-0.0.0/fastapi_file_validator.egg-info/PKG-INFO +14 -0
- fastapi_file_validator-0.0.0/fastapi_file_validator.egg-info/SOURCES.txt +9 -0
- fastapi_file_validator-0.0.0/fastapi_file_validator.egg-info/dependency_links.txt +1 -0
- fastapi_file_validator-0.0.0/fastapi_file_validator.egg-info/requires.txt +1 -0
- fastapi_file_validator-0.0.0/fastapi_file_validator.egg-info/top_level.txt +1 -0
- fastapi_file_validator-0.0.0/file_validator/__init__.py +7 -0
- fastapi_file_validator-0.0.0/file_validator/file_extension_validator.py +42 -0
- fastapi_file_validator-0.0.0/file_validator/file_size_validator.py +34 -0
- fastapi_file_validator-0.0.0/pyproject.toml +40 -0
- fastapi_file_validator-0.0.0/setup.cfg +4 -0
|
@@ -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,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,9 @@
|
|
|
1
|
+
pyproject.toml
|
|
2
|
+
fastapi_file_validator.egg-info/PKG-INFO
|
|
3
|
+
fastapi_file_validator.egg-info/SOURCES.txt
|
|
4
|
+
fastapi_file_validator.egg-info/dependency_links.txt
|
|
5
|
+
fastapi_file_validator.egg-info/requires.txt
|
|
6
|
+
fastapi_file_validator.egg-info/top_level.txt
|
|
7
|
+
file_validator/__init__.py
|
|
8
|
+
file_validator/file_extension_validator.py
|
|
9
|
+
file_validator/file_size_validator.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
fastapi>=0.110.1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
file_validator
|
|
@@ -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
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "fastapi-file-validator"
|
|
7
|
+
version = "0.0.0"
|
|
8
|
+
description = "A lightweight, flexible file extension validator and MIME type checker for fastapi"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = {text = "MIT"}
|
|
12
|
+
keywords = [
|
|
13
|
+
"validator",
|
|
14
|
+
]
|
|
15
|
+
authors = [
|
|
16
|
+
{name = "Seungkyu-Han", email="trust1204@gmail.com"}
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
dependencies = [
|
|
20
|
+
"fastapi>=0.110.1",
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
[project.urls]
|
|
24
|
+
"Homepage" = "https://github.com/Seungkyu-Han/fastapi-file-validator"
|
|
25
|
+
"Bug Tracker" = "https://github.com/Seungkyu-Han/fastapi-file-validator"
|
|
26
|
+
"Documentation" = "https://github.com/Seungkyu-Han/fastapi-file-validator"
|
|
27
|
+
"Source" = "https://github.com/Seungkyu-Han/fastapi-file-validator"
|
|
28
|
+
|
|
29
|
+
[tool.pytest.ini_options]
|
|
30
|
+
testpaths = ["tests"]
|
|
31
|
+
asyncio_mode = "auto"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
[tool.black]
|
|
35
|
+
line-length = 100
|
|
36
|
+
target-version = ['py38', 'py39', 'py310', 'py311']
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
[tool.setuptools]
|
|
40
|
+
packages = ["file_validator"]
|