@@ -417,38 +417,39 @@ def extract_drive_id(link):
417417def public_clips ():
418418 admin = session .get ('admin' , False )
419419 message = ""
420+ db_filename = None
420421
421- # Handle upload or link if admin and POST
422422 if admin and request .method == 'POST' :
423- file = request .files .get ('file' )
424- link = request .form .get ('link' , '' ).strip ()
423+ action = request .form .get ('action' , '' )
425424 title = request .form .get ('title' , '' ).strip ()
426425 description = request .form .get ('description' , '' ).strip ()
427- db_filename = None
428426
429427 if not title :
430428 message = "Title is required."
431- elif file and allowed_music_file (file .filename ):
432- filename = secure_filename (file .filename )
433- upload_folder = os .path .join (app .config ['UPLOAD_FOLDER' ], 'clips' )
434- os .makedirs (upload_folder , exist_ok = True )
435- filepath = os .path .join (upload_folder , filename )
436- file .save (filepath )
437- db_filename = 'clips/' + filename
438- elif link :
439- media_type = request .form .get ('media_type' , 'audio' ) # get file type from form (mp3 or mp4)
429+ elif action == 'upload_file' :
430+ file = request .files .get ('file' )
431+ if not file or not allowed_music_file (file .filename ):
432+ message = "Please upload a valid music or video file."
433+ else :
434+ filename = secure_filename (file .filename )
435+ upload_folder = os .path .join (app .config ['UPLOAD_FOLDER' ], 'clips' )
436+ os .makedirs (upload_folder , exist_ok = True )
437+ filepath = os .path .join (upload_folder , filename )
438+ file .save (filepath )
439+ db_filename = 'clips/' + filename
440+
441+ elif action == 'drive_link' :
442+ link = request .form .get ('link' , '' ).strip ()
443+ media_type = request .form .get ('media_type' , 'audio' )
440444 try :
441445 file_id = extract_drive_id (link )
442- if media_type == 'video' :
443- db_filename = f"https://drive.google.com/uc?export=download&id={ file_id } .mp4"
444- else :
445- db_filename = f"https://drive.google.com/uc?export=download&id={ file_id } .mp3"
446- except :
447- message = "Invalid Google Drive link."
448- else :
449- message = "Please upload a file or provide a valid Drive link."
446+ # Manually add extension to help with player detection
447+ ext = '.mp4' if media_type == 'video' else '.mp3'
448+ db_filename = f"https://drive.google.com/uc?export=download&id={ file_id } { ext } "
449+ except Exception as e :
450+ message = f"Invalid Google Drive link. ({ e } )"
450451
451- # Save to DB if filename was set
452+ # Insert into DB
452453 if db_filename :
453454 try :
454455 with sqlite3 .connect (DB_NAME ) as conn :
@@ -459,10 +460,10 @@ def public_clips():
459460 )
460461 conn .commit ()
461462 message = "Clip added!"
462- except sqlite3 . OperationalError :
463- message = "Database table not ready. Use SQL tool to create it first!"
463+ except Exception as e :
464+ message = "Error saving to database: " + str ( e )
464465
465- # Get clips (or show empty list if table missing)
466+ # Fetch all clips
466467 try :
467468 with sqlite3 .connect (DB_NAME ) as conn :
468469 c = conn .cursor ()
@@ -476,19 +477,6 @@ def public_clips():
476477
477478 return render_template ('clips.html' , clips = clips , admin = admin , message = message )
478479
479- # Get clips (or show empty list if table missing)
480- try :
481- with sqlite3 .connect (DB_NAME ) as conn :
482- c = conn .cursor ()
483- c .execute ("SELECT id, filename, title, description FROM music_clips" )
484- # Always fix: use forward slashes in filename
485- clips = [
486- (id , filename .replace ('\\ ' , '/' ), title , description )
487- for (id , filename , title , description ) in c .fetchall ()
488- ]
489- except sqlite3 .OperationalError :
490- clips = []
491- return render_template ('clips.html' , clips = clips , admin = admin , message = message )
492480
493481@app .route ('/dataset/<table>' )
494482def public_view (table ):
0 commit comments