Update app.py
Browse files
app.py
CHANGED
|
@@ -9,9 +9,6 @@ import mimetypes
|
|
| 9 |
import httpx
|
| 10 |
import io
|
| 11 |
import zipfile
|
| 12 |
-
import math
|
| 13 |
-
from PIL import Image
|
| 14 |
-
import os
|
| 15 |
|
| 16 |
app = FastAPI()
|
| 17 |
|
|
@@ -174,22 +171,11 @@ async def create_album(
|
|
| 174 |
uploaded_files = 0
|
| 175 |
|
| 176 |
for file in files:
|
| 177 |
-
file_content = await file.read()
|
| 178 |
-
original_extension = os.path.splitext(file.filename)[1].lower()
|
| 179 |
-
|
| 180 |
-
# Convert non-media files to PNG
|
| 181 |
if not is_media_file(file.content_type):
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
img = Image.new('RGB', (200, 200), color=(73, 109, 137))
|
| 185 |
-
img_byte_arr = io.BytesIO()
|
| 186 |
-
img.save(img_byte_arr, format='PNG')
|
| 187 |
-
file_content = img_byte_arr.getvalue()
|
| 188 |
-
file.content_type = "image/png"
|
| 189 |
-
except Exception as e:
|
| 190 |
-
print(f"Error converting file to PNG: {str(e)}")
|
| 191 |
-
continue
|
| 192 |
|
|
|
|
| 193 |
upload_result = await initiate_upload(cookies, file.filename, file.content_type)
|
| 194 |
|
| 195 |
if upload_result and 'upload_url' in upload_result:
|
|
@@ -200,7 +186,6 @@ async def create_album(
|
|
| 200 |
'filename': file.filename,
|
| 201 |
'path': serving_path,
|
| 202 |
'content_type': file.content_type,
|
| 203 |
-
'original_extension': original_extension, # Save original extension
|
| 204 |
'uploaded_at': datetime.now().isoformat()
|
| 205 |
})
|
| 206 |
uploaded_files += 1
|
|
@@ -250,12 +235,6 @@ async def view_album(album_id: str):
|
|
| 250 |
</audio>
|
| 251 |
</div>
|
| 252 |
"""
|
| 253 |
-
else:
|
| 254 |
-
preview_html = f"""
|
| 255 |
-
<div class="preview-container">
|
| 256 |
-
<p>No preview available for {file['filename']}</p>
|
| 257 |
-
</div>
|
| 258 |
-
"""
|
| 259 |
|
| 260 |
file_list_html += f"""
|
| 261 |
<div class="file-item">
|
|
@@ -294,11 +273,7 @@ async def download_album(album_id: str):
|
|
| 294 |
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
|
| 295 |
for file in album['files']:
|
| 296 |
response = requests.get(f"https://replicate.delivery/pbxt/{file['path']}")
|
| 297 |
-
|
| 298 |
-
original_filename = file['filename']
|
| 299 |
-
if 'original_extension' in file:
|
| 300 |
-
original_filename = os.path.splitext(file['filename'])[0] + file['original_extension']
|
| 301 |
-
zip_file.writestr(original_filename, response.content)
|
| 302 |
|
| 303 |
zip_buffer.seek(0)
|
| 304 |
|
|
|
|
| 9 |
import httpx
|
| 10 |
import io
|
| 11 |
import zipfile
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
|
|
|
|
| 171 |
uploaded_files = 0
|
| 172 |
|
| 173 |
for file in files:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
if not is_media_file(file.content_type):
|
| 175 |
+
# Redirect to single file upload for non-media files
|
| 176 |
+
return RedirectResponse(url="https://albumup-up1.hf.space/", status_code=303)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
+
file_content = await file.read()
|
| 179 |
upload_result = await initiate_upload(cookies, file.filename, file.content_type)
|
| 180 |
|
| 181 |
if upload_result and 'upload_url' in upload_result:
|
|
|
|
| 186 |
'filename': file.filename,
|
| 187 |
'path': serving_path,
|
| 188 |
'content_type': file.content_type,
|
|
|
|
| 189 |
'uploaded_at': datetime.now().isoformat()
|
| 190 |
})
|
| 191 |
uploaded_files += 1
|
|
|
|
| 235 |
</audio>
|
| 236 |
</div>
|
| 237 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
file_list_html += f"""
|
| 240 |
<div class="file-item">
|
|
|
|
| 273 |
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
|
| 274 |
for file in album['files']:
|
| 275 |
response = requests.get(f"https://replicate.delivery/pbxt/{file['path']}")
|
| 276 |
+
zip_file.writestr(file['filename'], response.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
|
| 278 |
zip_buffer.seek(0)
|
| 279 |
|