Skip to content

Commit

Permalink
Merge pull request #89 from video-db/upload-modal
Browse files Browse the repository at this point in the history
backend support for upload modal
  • Loading branch information
ashish-spext authored Nov 30, 2024
2 parents 0908ce3 + 5e2c65c commit 3ce91cf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
31 changes: 31 additions & 0 deletions backend/director/entrypoint/api/routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

from flask import Blueprint, request, current_app as app
from werkzeug.utils import secure_filename

from director.db import load_db
from director.handler import ChatHandler, SessionHandler, VideoDBHandler, ConfigHandler
Expand Down Expand Up @@ -85,6 +86,36 @@ def get_video_or_all(collection_id, video_id):
return videodb.get_videos()


@videodb_bp.route("/collection/<collection_id>/upload", methods=["POST"])
def upload_video(collection_id):
"""Upload a video to a collection."""
try:
videodb = VideoDBHandler(collection_id)

if "file" in request.files:
file = request.files["file"]
file_bytes = file.read()
safe_filename = secure_filename(file.filename)
if not safe_filename:
return {"message": "Invalid filename"}, 400
file_name = os.path.splitext(safe_filename)[0]
media_type = file.content_type.split("/")[0]
return videodb.upload(
source=file_bytes,
source_type="file",
media_type=media_type,
name=file_name,
)
elif "source" in request.json:
source = request.json["source"]
source_type = request.json["source_type"]
return videodb.upload(source=source, source_type=source_type)
else:
return {"message": "No valid source provided"}, 400
except Exception as e:
return {"message": str(e)}, 500


@config_bp.route("/check", methods=["GET"])
def config_check():
config_handler = ConfigHandler()
Expand Down
5 changes: 5 additions & 0 deletions backend/director/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ class VideoDBHandler:
def __init__(self, collection_id):
self.videodb_tool = VideoDBTool(collection_id=collection_id)

def upload(
self, source, source_type="url", media_type="video", name=None
):
return self.videodb_tool.upload(source, source_type, media_type, name)

def get_collection(self):
"""Get a collection by ID."""
return self.videodb_tool.get_collection()
Expand Down
12 changes: 11 additions & 1 deletion backend/director/tools/videodb_tool.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import os
import requests
import videodb

from videodb import SearchType, SubtitleStyle, IndexType, SceneExtractionType
from videodb.timeline import Timeline
from videodb.asset import VideoAsset, ImageAsset



class VideoDBTool:
def __init__(self, collection_id="default"):
self.conn = videodb.connect(
Expand Down Expand Up @@ -81,6 +81,16 @@ def upload(self, source, source_type="url", media_type="video", name=None):
upload_args["name"] = name
if source_type == "url":
upload_args["url"] = source
elif source_type == "file":
upload_url_data = self.conn.get(
path=f"/collection/{self.collection.id}/upload_url",
params={"name": name},
)
upload_url = upload_url_data.get("upload_url")
files = {"file": (name, source)}
response = requests.post(upload_url, files=files)
response.raise_for_status()
upload_args["url"] = upload_url
else:
upload_args["file_path"] = source
media = self.conn.upload(**upload_args)
Expand Down

0 comments on commit 3ce91cf

Please sign in to comment.