0
# File Management
1
2
Secure file upload and download system using bucket containers with configurable limits, automatic URL generation, and integration with view lifecycle.
3
4
## Capabilities
5
6
### Bucket Class
7
8
Container for managing file uploads and downloads with security controls.
9
10
```python { .api }
11
class Bucket:
12
def __init__(self, request, max_files: int = None, max_size: int = None,
13
index: bool = True, on_add=None, on_delete=None):
14
"""
15
Create a file bucket.
16
17
Args:
18
request: Request object
19
max_files (int): Maximum number of files
20
max_size (int): Maximum total size in bytes
21
index (bool): Whether to create file index
22
on_add: Callback for file additions
23
on_delete: Callback for file deletions
24
"""
25
26
def get_path(self, file_name: str = '') -> str:
27
"""
28
Get file system path.
29
30
Args:
31
file_name (str): Specific file name
32
33
Returns:
34
str: File system path
35
"""
36
37
def get_url(self, file_name: str = '') -> str:
38
"""
39
Get web URL for file.
40
41
Args:
42
file_name (str): Specific file name
43
44
Returns:
45
str: Web accessible URL
46
"""
47
48
def get_file_names(self) -> list:
49
"""
50
Get list of file names.
51
52
Returns:
53
list: List of file names in bucket
54
"""
55
56
def get_size(self) -> int:
57
"""
58
Get total size of files.
59
60
Returns:
61
int: Total size in bytes
62
"""
63
64
def get_add_url(self) -> str:
65
"""
66
Get upload URL.
67
68
Returns:
69
str: URL for uploading files
70
"""
71
72
def get_delete_url(self, file_name: str) -> str:
73
"""
74
Get deletion URL for file.
75
76
Args:
77
file_name (str): File to delete
78
79
Returns:
80
str: URL for deleting the file
81
"""
82
```
83
84
#### Usage Example
85
86
```python
87
from lona import App, View, Bucket
88
from lona.html import HTML, H1, P, A, Ul, Li
89
90
app = App(__file__)
91
92
@app.route('/files')
93
class FileManagerView(View):
94
def handle_request(self, request):
95
# Create bucket with limits
96
bucket = Bucket(
97
request,
98
max_files=10,
99
max_size=10 * 1024 * 1024 # 10MB
100
)
101
102
# Display current files
103
file_list = Ul()
104
for filename in bucket.get_file_names():
105
file_url = bucket.get_url(filename)
106
delete_url = bucket.get_delete_url(filename)
107
108
file_list.append(
109
Li(
110
A(filename, href=file_url),
111
' - ',
112
A('Delete', href=delete_url)
113
)
114
)
115
116
upload_url = bucket.get_add_url()
117
118
html = HTML(
119
H1('File Manager'),
120
P(f'Total size: {bucket.get_size()} bytes'),
121
P(f'Upload files: {upload_url}'),
122
file_list
123
)
124
125
return html
126
```
127
128
## Types
129
130
```python { .api }
131
from typing import Optional, Callable, List
132
133
FileName = str
134
FilePath = str
135
FileSize = int
136
FileCallback = Callable[[str], None]
137
```