Skip to content

Commit a43a294

Browse files
author
SM_SAYEED
committed
google drive linked for holding music file to be streaming on the database
1 parent 8088abb commit a43a294

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

app.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sqlite3
77
from werkzeug.utils import secure_filename
88
import datetime
9+
import re
910

1011
# ========== SETTINGS ==========
1112
UPLOAD_FOLDER = 'uploads'
@@ -403,28 +404,48 @@ def view_result_file(property_name, tab, filename):
403404
return render_template("view_result.html", filename=filename, property_name=property_name, tab=tab, ext=ext)
404405

405406

407+
def extract_drive_id(link):
408+
match = re.search(r'/d/([a-zA-Z0-9_-]+)', link)
409+
if match:
410+
return match.group(1)
411+
match = re.search(r'id=([a-zA-Z0-9_-]+)', link)
412+
if match:
413+
return match.group(1)
414+
raise ValueError("Invalid Drive link")
406415

407416
@app.route('/clips', methods=['GET', 'POST'])
408417
def public_clips():
409418
admin = session.get('admin', False)
410419
message = ""
411-
# Handle upload if admin and POST
420+
421+
# Handle upload or link if admin and POST
412422
if admin and request.method == 'POST':
413423
file = request.files.get('file')
424+
link = request.form.get('link', '').strip()
414425
title = request.form.get('title', '').strip()
415426
description = request.form.get('description', '').strip()
416-
if not file or not allowed_music_file(file.filename):
417-
message = "Please upload a valid music or video file."
418-
elif not title:
427+
db_filename = None
428+
429+
if not title:
419430
message = "Title is required."
420-
else:
431+
elif file and allowed_music_file(file.filename):
421432
filename = secure_filename(file.filename)
422433
upload_folder = os.path.join(app.config['UPLOAD_FOLDER'], 'clips')
423434
os.makedirs(upload_folder, exist_ok=True)
424435
filepath = os.path.join(upload_folder, filename)
425436
file.save(filepath)
426-
# Store path as forward slash for web
427437
db_filename = 'clips/' + filename
438+
elif link:
439+
try:
440+
file_id = extract_drive_id(link)
441+
db_filename = f"https://drive.google.com/uc?export=download&id={file_id}"
442+
except:
443+
message = "Invalid Google Drive link."
444+
else:
445+
message = "Please upload a file or provide a valid Drive link."
446+
447+
# Save to DB if filename was set
448+
if db_filename:
428449
try:
429450
with sqlite3.connect(DB_NAME) as conn:
430451
c = conn.cursor()
@@ -433,10 +454,24 @@ def public_clips():
433454
(db_filename, title, description)
434455
)
435456
conn.commit()
436-
message = "Clip uploaded!"
457+
message = "Clip added!"
437458
except sqlite3.OperationalError:
438459
message = "Database table not ready. Use SQL tool to create it first!"
439460

461+
# Get clips (or show empty list if table missing)
462+
try:
463+
with sqlite3.connect(DB_NAME) as conn:
464+
c = conn.cursor()
465+
c.execute("SELECT id, filename, title, description FROM music_clips")
466+
clips = [
467+
(id, filename.replace('\\', '/'), title, description)
468+
for (id, filename, title, description) in c.fetchall()
469+
]
470+
except sqlite3.OperationalError:
471+
clips = []
472+
473+
return render_template('clips.html', clips=clips, admin=admin, message=message)
474+
440475
# Get clips (or show empty list if table missing)
441476
try:
442477
with sqlite3.connect(DB_NAME) as conn:

0 commit comments

Comments
 (0)