albumup commited on
Commit
43305eb
·
verified ·
1 Parent(s): 9419c65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py CHANGED
@@ -7,6 +7,8 @@ import uuid
7
  from datetime import datetime
8
  import mimetypes
9
  import httpx
 
 
10
 
11
  app = FastAPI()
12
 
@@ -69,6 +71,11 @@ async def index():
69
  <input type="file" name="files" accept="*/*" multiple required><br><br>
70
  <button type="submit">Create Album</button>
71
  </form>
 
 
 
 
 
72
  </body>
73
  </html>
74
  """
@@ -159,6 +166,7 @@ async def view_album(album_id: str):
159
  </head>
160
  <body>
161
  <h1>{album['albumName']}</h1>
 
162
  <div class="file-grid">
163
  {file_list_html}
164
  </div>
@@ -166,6 +174,63 @@ async def view_album(album_id: str):
166
  </html>
167
  """
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
  @app.get("/upload/{path:path}")
170
  async def handle_stream(path: str, request: Request):
171
  original_url = f'https://replicate.delivery/pbxt/{path}'
 
7
  from datetime import datetime
8
  import mimetypes
9
  import httpx
10
+ import io
11
+ import zipfile
12
 
13
  app = FastAPI()
14
 
 
71
  <input type="file" name="files" accept="*/*" multiple required><br><br>
72
  <button type="submit">Create Album</button>
73
  </form>
74
+ <h2>Search Albums</h2>
75
+ <form action="/search" method="get">
76
+ <input type="text" name="query" placeholder="Search by album name..." required>
77
+ <button type="submit">Search</button>
78
+ </form>
79
  </body>
80
  </html>
81
  """
 
166
  </head>
167
  <body>
168
  <h1>{album['albumName']}</h1>
169
+ <a href="/album/{album_id}/download" class="button">Download All Files as ZIP</a>
170
  <div class="file-grid">
171
  {file_list_html}
172
  </div>
 
174
  </html>
175
  """
176
 
177
+ @app.get("/album/{album_id}/download")
178
+ async def download_album(album_id: str):
179
+ album = await get_album_from_db(album_id)
180
+ if not album or 'files' not in album:
181
+ return {"error": "Album not found or invalid data"}, 404
182
+
183
+ zip_buffer = io.BytesIO()
184
+ with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
185
+ for file in album['files']:
186
+ response = requests.get(f"https://replicate.delivery/pbxt/{file['path']}")
187
+ zip_file.writestr(file['filename'], response.content)
188
+
189
+ zip_buffer.seek(0)
190
+
191
+ return StreamingResponse(
192
+ io.BytesIO(zip_buffer.getvalue()),
193
+ media_type="application/zip",
194
+ headers={
195
+ "Content-Disposition": f"attachment; filename={album['albumName']}.zip"
196
+ }
197
+ )
198
+
199
+ @app.get("/search", response_class=HTMLResponse)
200
+ async def search_albums(query: str):
201
+ db_albums = await get_albums_from_db()
202
+ matching_albums = [album for album in db_albums['data'] if query.lower() in album['albumName'].lower()]
203
+
204
+ results_html = ""
205
+ for album in matching_albums:
206
+ results_html += f"""
207
+ <div class="album-item">
208
+ <h3>{album['albumName']}</h3>
209
+ <p>Created: {album['createdAt']}</p>
210
+ <p>Files: {len(album['files'])}</p>
211
+ <a href="/album/{album['albumLink']}" class="button">View Album</a>
212
+ </div>
213
+ """
214
+
215
+ return f"""
216
+ <!DOCTYPE html>
217
+ <html lang="en">
218
+ <head>
219
+ <meta charset="UTF-8">
220
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
221
+ <title>Search Results</title>
222
+ </head>
223
+ <body>
224
+ <h1>Search Results</h1>
225
+ <p>Found {len(matching_albums)} albums for "{query}"</p>
226
+ <div class="search-results">
227
+ {results_html}
228
+ </div>
229
+ <a href="/" class="button">← Back to Home</a>
230
+ </body>
231
+ </html>
232
+ """
233
+
234
  @app.get("/upload/{path:path}")
235
  async def handle_stream(path: str, request: Request):
236
  original_url = f'https://replicate.delivery/pbxt/{path}'