Update app.py
Browse files
app.py
CHANGED
|
@@ -21,15 +21,50 @@ HTML_CONTENT = """
|
|
| 21 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 22 |
<title>File Upload</title>
|
| 23 |
<style>
|
| 24 |
-
.container {
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
</style>
|
| 28 |
</head>
|
| 29 |
<body>
|
| 30 |
<div class="container">
|
| 31 |
-
<h1>File Upload</h1>
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
<!-- Single File Upload -->
|
| 34 |
<div class="upload-form">
|
| 35 |
<h2>Single File Upload</h2>
|
|
@@ -59,7 +94,7 @@ ALBUM_VIEW_HTML = """
|
|
| 59 |
<head>
|
| 60 |
<meta charset="UTF-8">
|
| 61 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 62 |
-
<title>Album View</title>
|
| 63 |
<style>
|
| 64 |
.container {{
|
| 65 |
max-width: 800px;
|
|
@@ -75,17 +110,35 @@ ALBUM_VIEW_HTML = """
|
|
| 75 |
border: 1px solid #ddd;
|
| 76 |
padding: 10px;
|
| 77 |
text-align: center;
|
|
|
|
| 78 |
}}
|
| 79 |
.download-all {{
|
| 80 |
margin: 20px 0;
|
| 81 |
}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
</style>
|
| 83 |
</head>
|
| 84 |
<body>
|
| 85 |
<div class="container">
|
|
|
|
| 86 |
<h1>{album_name}</h1>
|
| 87 |
<div class="download-all">
|
| 88 |
-
<a href="/album/{album_id}/download" class="button">Download All Files</a>
|
| 89 |
</div>
|
| 90 |
<div class="file-grid">
|
| 91 |
{file_list}
|
|
@@ -95,10 +148,85 @@ ALBUM_VIEW_HTML = """
|
|
| 95 |
</html>
|
| 96 |
"""
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
@app.get("/", response_class=HTMLResponse)
|
| 99 |
async def index():
|
| 100 |
return HTML_CONTENT
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
@app.post("/upload")
|
| 103 |
async def handle_upload(request: Request, file: UploadFile = File(...)):
|
| 104 |
if not file:
|
|
@@ -168,8 +296,8 @@ async def view_album(album_id: str):
|
|
| 168 |
file_list_html += f"""
|
| 169 |
<div class="file-item">
|
| 170 |
<p>{file['filename']}</p>
|
| 171 |
-
<a href="/upload/{file['path']}"
|
| 172 |
-
<a href="/upload/{file['path']}?download=true">Download</a>
|
| 173 |
</div>
|
| 174 |
"""
|
| 175 |
|
|
|
|
| 21 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 22 |
<title>File Upload</title>
|
| 23 |
<style>
|
| 24 |
+
.container {{
|
| 25 |
+
max-width: 800px;
|
| 26 |
+
margin: 0 auto;
|
| 27 |
+
padding: 20px;
|
| 28 |
+
}}
|
| 29 |
+
.upload-form {{
|
| 30 |
+
margin-bottom: 20px;
|
| 31 |
+
padding: 20px;
|
| 32 |
+
border: 1px solid #ddd;
|
| 33 |
+
border-radius: 5px;
|
| 34 |
+
}}
|
| 35 |
+
.preview {{
|
| 36 |
+
margin-top: 20px;
|
| 37 |
+
}}
|
| 38 |
+
.search-form {{
|
| 39 |
+
margin: 20px 0;
|
| 40 |
+
padding: 20px;
|
| 41 |
+
border: 1px solid #ddd;
|
| 42 |
+
border-radius: 5px;
|
| 43 |
+
}}
|
| 44 |
+
.album-list {{
|
| 45 |
+
margin-top: 20px;
|
| 46 |
+
}}
|
| 47 |
+
.album-item {{
|
| 48 |
+
padding: 10px;
|
| 49 |
+
border: 1px solid #ddd;
|
| 50 |
+
margin-bottom: 10px;
|
| 51 |
+
border-radius: 5px;
|
| 52 |
+
}}
|
| 53 |
</style>
|
| 54 |
</head>
|
| 55 |
<body>
|
| 56 |
<div class="container">
|
| 57 |
+
<h1>File Upload & Album Management</h1>
|
| 58 |
|
| 59 |
+
<!-- Search Albums -->
|
| 60 |
+
<div class="search-form">
|
| 61 |
+
<h2>Search Albums</h2>
|
| 62 |
+
<form action="/search" method="get">
|
| 63 |
+
<input type="text" name="query" placeholder="Search by album name..." required>
|
| 64 |
+
<button type="submit">Search</button>
|
| 65 |
+
</form>
|
| 66 |
+
</div>
|
| 67 |
+
|
| 68 |
<!-- Single File Upload -->
|
| 69 |
<div class="upload-form">
|
| 70 |
<h2>Single File Upload</h2>
|
|
|
|
| 94 |
<head>
|
| 95 |
<meta charset="UTF-8">
|
| 96 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 97 |
+
<title>Album View - {album_name}</title>
|
| 98 |
<style>
|
| 99 |
.container {{
|
| 100 |
max-width: 800px;
|
|
|
|
| 110 |
border: 1px solid #ddd;
|
| 111 |
padding: 10px;
|
| 112 |
text-align: center;
|
| 113 |
+
border-radius: 5px;
|
| 114 |
}}
|
| 115 |
.download-all {{
|
| 116 |
margin: 20px 0;
|
| 117 |
}}
|
| 118 |
+
.back-link {{
|
| 119 |
+
margin-bottom: 20px;
|
| 120 |
+
display: block;
|
| 121 |
+
}}
|
| 122 |
+
.button {{
|
| 123 |
+
display: inline-block;
|
| 124 |
+
padding: 8px 16px;
|
| 125 |
+
background-color: #007bff;
|
| 126 |
+
color: white;
|
| 127 |
+
text-decoration: none;
|
| 128 |
+
border-radius: 4px;
|
| 129 |
+
margin: 5px;
|
| 130 |
+
}}
|
| 131 |
+
.button:hover {{
|
| 132 |
+
background-color: #0056b3;
|
| 133 |
+
}}
|
| 134 |
</style>
|
| 135 |
</head>
|
| 136 |
<body>
|
| 137 |
<div class="container">
|
| 138 |
+
<a href="/" class="back-link button">← Back to Home</a>
|
| 139 |
<h1>{album_name}</h1>
|
| 140 |
<div class="download-all">
|
| 141 |
+
<a href="/album/{album_id}/download" class="button" target="_blank">Download All Files</a>
|
| 142 |
</div>
|
| 143 |
<div class="file-grid">
|
| 144 |
{file_list}
|
|
|
|
| 148 |
</html>
|
| 149 |
"""
|
| 150 |
|
| 151 |
+
SEARCH_RESULTS_HTML = """
|
| 152 |
+
<!DOCTYPE html>
|
| 153 |
+
<html lang="en">
|
| 154 |
+
<head>
|
| 155 |
+
<meta charset="UTF-8">
|
| 156 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 157 |
+
<title>Search Results</title>
|
| 158 |
+
<style>
|
| 159 |
+
.container {{
|
| 160 |
+
max-width: 800px;
|
| 161 |
+
margin: 0 auto;
|
| 162 |
+
padding: 20px;
|
| 163 |
+
}}
|
| 164 |
+
.album-item {{
|
| 165 |
+
border: 1px solid #ddd;
|
| 166 |
+
padding: 15px;
|
| 167 |
+
margin-bottom: 15px;
|
| 168 |
+
border-radius: 5px;
|
| 169 |
+
}}
|
| 170 |
+
.back-link {{
|
| 171 |
+
margin-bottom: 20px;
|
| 172 |
+
display: block;
|
| 173 |
+
}}
|
| 174 |
+
.button {{
|
| 175 |
+
display: inline-block;
|
| 176 |
+
padding: 8px 16px;
|
| 177 |
+
background-color: #007bff;
|
| 178 |
+
color: white;
|
| 179 |
+
text-decoration: none;
|
| 180 |
+
border-radius: 4px;
|
| 181 |
+
margin: 5px;
|
| 182 |
+
}}
|
| 183 |
+
.button:hover {{
|
| 184 |
+
background-color: #0056b3;
|
| 185 |
+
}}
|
| 186 |
+
</style>
|
| 187 |
+
</head>
|
| 188 |
+
<body>
|
| 189 |
+
<div class="container">
|
| 190 |
+
<a href="/" class="back-link button">← Back to Home</a>
|
| 191 |
+
<h1>Search Results</h1>
|
| 192 |
+
<p>Found {result_count} albums for "{query}"</p>
|
| 193 |
+
<div class="search-results">
|
| 194 |
+
{results}
|
| 195 |
+
</div>
|
| 196 |
+
</div>
|
| 197 |
+
</body>
|
| 198 |
+
</html>
|
| 199 |
+
"""
|
| 200 |
+
|
| 201 |
@app.get("/", response_class=HTMLResponse)
|
| 202 |
async def index():
|
| 203 |
return HTML_CONTENT
|
| 204 |
|
| 205 |
+
@app.get("/search", response_class=HTMLResponse)
|
| 206 |
+
async def search_albums(query: str = ""):
|
| 207 |
+
query = query.lower()
|
| 208 |
+
matching_albums = {
|
| 209 |
+
album_id: album for album_id, album in albums.items()
|
| 210 |
+
if query in album['name'].lower()
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
results_html = ""
|
| 214 |
+
for album_id, album in matching_albums.items():
|
| 215 |
+
results_html += f"""
|
| 216 |
+
<div class="album-item">
|
| 217 |
+
<h3>{album['name']}</h3>
|
| 218 |
+
<p>Created: {album['created_at']}</p>
|
| 219 |
+
<p>Files: {len(album['files'])}</p>
|
| 220 |
+
<a href="/album/{album_id}" class="button">View Album</a>
|
| 221 |
+
</div>
|
| 222 |
+
"""
|
| 223 |
+
|
| 224 |
+
return SEARCH_RESULTS_HTML.format(
|
| 225 |
+
query=query,
|
| 226 |
+
result_count=len(matching_albums),
|
| 227 |
+
results=results_html
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
@app.post("/upload")
|
| 231 |
async def handle_upload(request: Request, file: UploadFile = File(...)):
|
| 232 |
if not file:
|
|
|
|
| 296 |
file_list_html += f"""
|
| 297 |
<div class="file-item">
|
| 298 |
<p>{file['filename']}</p>
|
| 299 |
+
<a href="/upload/{file['path']}" class="button">View</a>
|
| 300 |
+
<a href="/upload/{file['path']}?download=true" class="button" target="_blank">Download</a>
|
| 301 |
</div>
|
| 302 |
"""
|
| 303 |
|