https://github.com/joegasewicz/bobtail-upload
File uploads for Bobtail
https://github.com/joegasewicz/bobtail-upload
Last synced: 6 months ago
JSON representation
File uploads for Bobtail
- Host: GitHub
- URL: https://github.com/joegasewicz/bobtail-upload
- Owner: joegasewicz
- License: mit
- Created: 2023-01-23T16:16:43.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-01-23T20:43:49.000Z (over 2 years ago)
- Last Synced: 2025-03-24T15:52:34.502Z (7 months ago)
- Language: Python
- Size: 14.6 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/joegasewicz/bobtail-upload/actions/workflows/python-package.yml)
[](https://github.com/joegasewicz/bobtail-upload/actions/workflows/python-publish.yml)# Bobtail File Upload
Middleware to upload files for [Bobtail](https://github.com/joegasewicz/bobtail)### Install
```bash
pip install bobtail-upload
```### Usage
```python
from bobtail_upload import BobtailUploadapp = Bobtail(routes=routes)
app.use(BobtailUpload(options={}))
```### Saving files
Bobtail Upload will attach the Upload API to the request object.
There are 2 new methods now available on `Request`:- `add(self, *, file_name: str, data: bytes, mimetype: str) -> None`
- `save(self, *, table_name: str = None, pk: Union[int, str] = None) -> None````python
def post(self, req: Request, res: Response):
data = req.get_multipart_data()
req.upload.add(
file_name=data["logo"]["value"]["filename"],
data=data["logo"]["value"]["file_data"],
mimetype=data["logo"]["value"]["mimetype"],
)
req.upload.save()
```### Mapping file saves to your database tables
To save files based on a table name & primary key.The default upload directory path is `/uploads`
```
- uploads
- images
- 2
```
For example
```python
def post(self, req: Request, res: Response):
data = req.get_multipart_data()
req.upload.add(
file_name=data["logo"]["value"]["filename"],
data=data["logo"]["value"]["file_data"],
mimetype=data["logo"]["value"]["mimetype"],
)
# Use your ORM to save the file to your db & obtain the returned primary key (pk)
req.upload.save(table_name="images", pk=pk)
```### Options
- UPLOAD_DIR - the directory path where files will be saved (defaults to `uploads`).
```python
options = {
"UPLOAD_DIR": "uploads",
}
```