albumup commited on
Commit
7b356ed
·
verified ·
1 Parent(s): eb1add6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -3
app.py CHANGED
@@ -7,6 +7,7 @@ import os
7
  import uuid
8
  import json
9
  from datetime import datetime
 
10
 
11
  app = FastAPI()
12
 
@@ -103,7 +104,7 @@ ALBUM_VIEW_HTML = """
103
  }}
104
  .file-grid {{
105
  display: grid;
106
- grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
107
  gap: 20px;
108
  }}
109
  .file-item {{
@@ -131,7 +132,87 @@ ALBUM_VIEW_HTML = """
131
  .button:hover {{
132
  background-color: #0056b3;
133
  }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  </style>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  </head>
136
  <body>
137
  <div class="container">
@@ -144,6 +225,13 @@ ALBUM_VIEW_HTML = """
144
  {file_list}
145
  </div>
146
  </div>
 
 
 
 
 
 
 
147
  </body>
148
  </html>
149
  """
@@ -198,6 +286,15 @@ SEARCH_RESULTS_HTML = """
198
  </html>
199
  """
200
 
 
 
 
 
 
 
 
 
 
201
  @app.get("/", response_class=HTMLResponse)
202
  async def index():
203
  return HTML_CONTENT
@@ -293,11 +390,38 @@ async def view_album(album_id: str):
293
  file_list_html = ""
294
 
295
  for file in album['files']:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 
 
7
  import uuid
8
  import json
9
  from datetime import datetime
10
+ import mimetypes
11
 
12
  app = FastAPI()
13
 
 
104
  }}
105
  .file-grid {{
106
  display: grid;
107
+ grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
108
  gap: 20px;
109
  }}
110
  .file-item {{
 
132
  .button:hover {{
133
  background-color: #0056b3;
134
  }}
135
+ .preview-container {{
136
+ margin-bottom: 10px;
137
+ max-width: 100%;
138
+ height: 200px;
139
+ overflow: hidden;
140
+ display: flex;
141
+ align-items: center;
142
+ justify-content: center;
143
+ background-color: #f8f9fa;
144
+ border-radius: 4px;
145
+ }}
146
+ .preview-container img {{
147
+ max-width: 100%;
148
+ max-height: 100%;
149
+ object-fit: contain;
150
+ }}
151
+ .preview-container video {{
152
+ max-width: 100%;
153
+ max-height: 100%;
154
+ object-fit: contain;
155
+ }}
156
+ .modal {{
157
+ display: none;
158
+ position: fixed;
159
+ z-index: 1000;
160
+ top: 0;
161
+ left: 0;
162
+ width: 100%;
163
+ height: 100%;
164
+ background-color: rgba(0,0,0,0.9);
165
+ overflow: auto;
166
+ }}
167
+ .modal-content {{
168
+ margin: auto;
169
+ display: block;
170
+ max-width: 90%;
171
+ max-height: 90vh;
172
+ margin-top: 50px;
173
+ }}
174
+ .close {{
175
+ position: absolute;
176
+ right: 35px;
177
+ top: 15px;
178
+ color: #f1f1f1;
179
+ font-size: 40px;
180
+ font-weight: bold;
181
+ cursor: pointer;
182
+ }}
183
  </style>
184
+ <script>
185
+ function openModal(url) {{
186
+ const modal = document.getElementById('previewModal');
187
+ const modalContent = document.getElementById('modalContent');
188
+ const contentType = getContentType(url);
189
+
190
+ if (contentType === 'image') {{
191
+ modalContent.innerHTML = `<img src="${{url}}" style="max-width:100%;max-height:90vh;">`;
192
+ }} else if (contentType === 'video') {{
193
+ modalContent.innerHTML = `<video controls style="max-width:100%;max-height:90vh;">
194
+ <source src="${{url}}" type="video/mp4">
195
+ Your browser does not support the video tag.
196
+ </video>`;
197
+ }}
198
+
199
+ modal.style.display = 'block';
200
+ }}
201
+
202
+ function closeModal() {{
203
+ const modal = document.getElementById('previewModal');
204
+ modal.style.display = 'none';
205
+ const modalContent = document.getElementById('modalContent');
206
+ modalContent.innerHTML = '';
207
+ }}
208
+
209
+ function getContentType(url) {{
210
+ const ext = url.split('.').pop().toLowerCase();
211
+ if (['jpg', 'jpeg', 'png', 'gif', 'webp'].includes(ext)) return 'image';
212
+ if (['mp4', 'webm', 'ogg'].includes(ext)) return 'video';
213
+ return 'other';
214
+ }}
215
+ </script>
216
  </head>
217
  <body>
218
  <div class="container">
 
225
  {file_list}
226
  </div>
227
  </div>
228
+
229
+ <!-- Modal for previews -->
230
+ <div id="previewModal" class="modal" onclick="closeModal()">
231
+ <span class="close">&times;</span>
232
+ <div id="modalContent" class="modal-content">
233
+ </div>
234
+ </div>
235
  </body>
236
  </html>
237
  """
 
286
  </html>
287
  """
288
 
289
+ def get_file_type(filename):
290
+ mime_type, _ = mimetypes.guess_type(filename)
291
+ if mime_type:
292
+ if mime_type.startswith('image/'):
293
+ return 'image'
294
+ elif mime_type.startswith('video/'):
295
+ return 'video'
296
+ return 'other'
297
+
298
  @app.get("/", response_class=HTMLResponse)
299
  async def index():
300
  return HTML_CONTENT
 
390
  file_list_html = ""
391
 
392
  for file in album['files']:
393
+ file_url = f"/upload/{file['path']}"
394
+ file_type = get_file_type(file['filename'])
395
+
396
+ preview_html = ""
397
+ if file_type == 'image':
398
+ preview_html = f"""
399
+ <div class="preview-container">
400
+ <img src="{file_url}" alt="{file['filename']}" onclick="openModal('{file_url}')">
401
+ </div>
402
+ """
403
+ elif file_type == 'video':
404
+ preview_html = f"""
405
+ <div class="preview-container">
406
+ <video onclick="openModal('{file_url}')" style="cursor: pointer;">
407
+ <source src="{file_url}" type="{file['content_type']}">
408
+ Your browser does not support the video tag.
409
+ </video>
410
+ </div>
411
+ """
412
+ else:
413
+ preview_html = f"""
414
+ <div class="preview-container">
415
+ <p>No preview available for {file['filename']}</p>
416
+ </div>
417
+ """
418
+
419
  file_list_html += f"""
420
  <div class="file-item">
421
+ {preview_html}
422
  <p>{file['filename']}</p>
423
+ <a href="{file_url}" class="button" onclick="openModal('{file_url}'); return false;">View</a>
424
+ <a href="{file_url}?download=true" class="button" target="_blank">Download</a>
425
  </div>
426
  """
427