uploader first commit
This commit is contained in:
parent
68e0494f78
commit
cd92b32d57
100
scripts/uploader.md
Normal file
100
scripts/uploader.md
Normal file
@ -0,0 +1,100 @@
|
||||
# Uploader
|
||||
|
||||
File upload utility library for Bottle framework.
|
||||
|
||||
## Description
|
||||
|
||||
Simple file upload handler that saves files to a specified directory with optional renaming.
|
||||
|
||||
## Usage
|
||||
|
||||
### Import
|
||||
|
||||
```python
|
||||
from scripts import uploader
|
||||
```
|
||||
|
||||
### Function Signature
|
||||
|
||||
```python
|
||||
uploader.upload(file, directory, new_name=None)
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
| Parameter | Type | Required | Description |
|
||||
|-----------|------|----------|-------------|
|
||||
| `file` | FileStorage | Yes | File object from `request.files.get('field_name')` |
|
||||
| `directory` | string | Yes | Target directory path (will be created if not exists) |
|
||||
| `new_name` | string | No | New filename. If empty/None, uses original filename |
|
||||
|
||||
### Returns
|
||||
|
||||
```python
|
||||
{
|
||||
"status": "success",
|
||||
"path": "/full/path/to/saved/file.jpg"
|
||||
}
|
||||
```
|
||||
|
||||
On failure:
|
||||
|
||||
```python
|
||||
{
|
||||
"status": "failed",
|
||||
"error": "Error message here"
|
||||
}
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
### Basic Upload (Keep Original Filename)
|
||||
|
||||
```python
|
||||
from bottle import request
|
||||
from scripts import uploader
|
||||
|
||||
@app.route('/upload', method='POST')
|
||||
def do_upload():
|
||||
file = request.files.get('file')
|
||||
directory = '/var/www/uploads/'
|
||||
|
||||
result = uploader.upload(file, directory)
|
||||
|
||||
if result['status'] == 'success':
|
||||
return f"File saved to: {result['path']}"
|
||||
else:
|
||||
return f"Upload failed: {result['error']}"
|
||||
```
|
||||
|
||||
### Upload with Custom Filename
|
||||
|
||||
```python
|
||||
@app.route('/upload', method='POST')
|
||||
def do_upload():
|
||||
file = request.files.get('file')
|
||||
directory = '/var/www/uploads/'
|
||||
new_name = "profile_picture.jpg"
|
||||
|
||||
result = uploader.upload(file, directory, new_name)
|
||||
|
||||
return result
|
||||
```
|
||||
|
||||
### Using Generated Filename
|
||||
|
||||
```python
|
||||
import uuid
|
||||
|
||||
@app.route('/upload', method='POST')
|
||||
def do_upload():
|
||||
file = request.files.get('file')
|
||||
directory = '/var/www/uploads/'
|
||||
|
||||
ext = file.filename.split('.')[-1]
|
||||
new_name = f"{uuid.uuid4()}.{ext}"
|
||||
|
||||
result = uploader.upload(file, directory, new_name)
|
||||
|
||||
return result
|
||||
```
|
||||
11
scripts/uploader.py
Normal file
11
scripts/uploader.py
Normal file
@ -0,0 +1,11 @@
|
||||
import os
|
||||
def upload(file, directory, new_name=None):
|
||||
try:
|
||||
os.makedirs(directory, exist_ok=True)
|
||||
if not new_name:
|
||||
new_name = file.filename
|
||||
full_path = os.path.join(directory, new_name)
|
||||
file.save(full_path)
|
||||
return {"status": "success", "path": full_path}
|
||||
except Exception as e:
|
||||
return {"status": "failed", "error": str(e)}
|
||||
Loading…
Reference in New Issue
Block a user