# starlette-validation-uploadfile
![PyPI](https://img.shields.io/pypi/v/starlette-validation-uploadfile?color=orange)
![License](https://img.shields.io/github/license/terib0l/starlette-validation-uploadfile)
Middleware for validation upload-file in FastAPI and Starlette.
## Installation
```bash
pip install starlette-validation-uploadfile
```
## What this package can do:
- Put a limit on the size of the uploaded file
- Restrict the types of files that can be uploaded
## Usage example with FastAPI
The following is almost identical to `test.py`.
Note: requirements.txt is for test.
```python
from fastapi import FastAPI, Request, UploadFile, File
from starlette_validation_uploadfile import ValidateUploadFileMiddleware
app = FastAPI()
app.add_middleware(
ValidateUploadFileMiddleware,
app_path=[
"/upload/first",
"/upload/second",
],
max_size=16777216,
file_type=["image/png", "image/jpeg"]
)
@app.post("/upload/first")
async def upload_file(request: Request, file: UploadFile = File(...)):
form = await request.form()
content_type = form[next(iter(form))].content_type
size = request.headers["content-length"]
return {
"filename": file.filename,
"content_type": content_type,
"file_size": size,
}
@app.post("/upload/second")
async def upload_file_second(request: Request, file: UploadFile = File(...)):
form = await request.form()
content_type = form[next(iter(form))].content_type
size = request.headers["content-length"]
return {
"filename": file.filename,
"content_type": content_type,
"file_size": size,
}
```
Raw data
{
"_id": null,
"home_page": "https://github.com/terib0l/starlette-validation-uploadfile",
"name": "starlette-validation-uploadfile",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "starlette fastapi middleware upload file validation",
"author": "terib0l",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/33/75/c6805687b0c49088378433ffbb564163677ff64e72132d3c65856a2ad97d/starlette-validation-uploadfile-0.2.0.tar.gz",
"platform": null,
"description": "# starlette-validation-uploadfile\n\n![PyPI](https://img.shields.io/pypi/v/starlette-validation-uploadfile?color=orange)\n![License](https://img.shields.io/github/license/terib0l/starlette-validation-uploadfile)\n\nMiddleware for validation upload-file in FastAPI and Starlette.\n\n## Installation\n\n```bash\npip install starlette-validation-uploadfile\n```\n\n## What this package can do:\n\n- Put a limit on the size of the uploaded file\n- Restrict the types of files that can be uploaded\n\n## Usage example with FastAPI\n\nThe following is almost identical to `test.py`. \nNote: requirements.txt is for test.\n\n```python\nfrom fastapi import FastAPI, Request, UploadFile, File \n\nfrom starlette_validation_uploadfile import ValidateUploadFileMiddleware\n\napp = FastAPI()\n\napp.add_middleware(\n ValidateUploadFileMiddleware,\n app_path=[\n \"/upload/first\",\n \"/upload/second\",\n ],\n max_size=16777216,\n file_type=[\"image/png\", \"image/jpeg\"]\n)\n\n@app.post(\"/upload/first\")\nasync def upload_file(request: Request, file: UploadFile = File(...)):\n form = await request.form()\n content_type = form[next(iter(form))].content_type\n\n size = request.headers[\"content-length\"]\n\n return {\n \"filename\": file.filename,\n \"content_type\": content_type,\n \"file_size\": size,\n }\n\n@app.post(\"/upload/second\")\nasync def upload_file_second(request: Request, file: UploadFile = File(...)):\n form = await request.form()\n content_type = form[next(iter(form))].content_type\n\n size = request.headers[\"content-length\"]\n\n return {\n \"filename\": file.filename,\n \"content_type\": content_type,\n \"file_size\": size,\n }\n```\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Middleware for validation upload-file in FastAPI and Starlette.",
"version": "0.2.0",
"project_urls": {
"Homepage": "https://github.com/terib0l/starlette-validation-uploadfile"
},
"split_keywords": [
"starlette",
"fastapi",
"middleware",
"upload",
"file",
"validation"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5c5c708f171f94a4c6ff68847d25578fbaee7831a2d120a99c0ba2f4aba6e7d7",
"md5": "732088cbd85c063a99691a9005c5df09",
"sha256": "b8b36ab426e6dbda1d8241d25dbf1ae3fcdcda1a383d3dbe69a5aa442c31ed77"
},
"downloads": -1,
"filename": "starlette_validation_uploadfile-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "732088cbd85c063a99691a9005c5df09",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 4089,
"upload_time": "2023-06-13T14:07:30",
"upload_time_iso_8601": "2023-06-13T14:07:30.014204Z",
"url": "https://files.pythonhosted.org/packages/5c/5c/708f171f94a4c6ff68847d25578fbaee7831a2d120a99c0ba2f4aba6e7d7/starlette_validation_uploadfile-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3375c6805687b0c49088378433ffbb564163677ff64e72132d3c65856a2ad97d",
"md5": "afdedd8bac6dabc288949ff5688af57e",
"sha256": "98cb0070c724a75c9ff4e8648bf1c9960f40f66d0fbefcfe413e7d94d21c697b"
},
"downloads": -1,
"filename": "starlette-validation-uploadfile-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "afdedd8bac6dabc288949ff5688af57e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 4467,
"upload_time": "2023-06-13T14:07:31",
"upload_time_iso_8601": "2023-06-13T14:07:31.860215Z",
"url": "https://files.pythonhosted.org/packages/33/75/c6805687b0c49088378433ffbb564163677ff64e72132d3c65856a2ad97d/starlette-validation-uploadfile-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-06-13 14:07:31",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "terib0l",
"github_project": "starlette-validation-uploadfile",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "starlette-validation-uploadfile"
}