Skip to content

Commit 06bd6f0

Browse files
authored
Merge pull request #173 from omgate234/feat/audio-image-fetch
feat: handle audio image CRUD endpoints
2 parents 48147fc + 466d845 commit 06bd6f0

3 files changed

Lines changed: 175 additions & 1 deletion

File tree

‎backend/director/entrypoint/api/routes.py‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,32 @@ def get_video_or_all(collection_id, video_id):
125125
return videodb.get_video(video_id)
126126
else:
127127
return videodb.get_videos()
128+
129+
@videodb_bp.route(
130+
"/collection/<collection_id>/audio", defaults={"audio_id": None}, methods=["GET"]
131+
)
132+
@videodb_bp.route("/collection/<collection_id>/audio/<audio_id>", methods=["GET"])
133+
def get_audio_or_all(collection_id, audio_id, **kwargs):
134+
"""Get a video by ID or all videos in a collection."""
135+
videodb = VideoDBHandler(collection_id)
136+
if audio_id:
137+
return videodb.get_audio(audio_id)
138+
else:
139+
return videodb.get_audios()
140+
141+
142+
@videodb_bp.route(
143+
"/collection/<collection_id>/image", defaults={"image_id": None}, methods=["GET"]
144+
)
145+
@videodb_bp.route("/collection/<collection_id>/image/<image_id>", methods=["GET"])
146+
def get_image_or_all(collection_id, image_id, **kwargs):
147+
"""Get a video by ID or all videos in a collection."""
148+
videodb = VideoDBHandler(collection_id)
149+
if image_id:
150+
return videodb.get_image(image_id)
151+
else:
152+
return videodb.get_images()
153+
128154

129155

130156
@videodb_bp.route("/collection/<collection_id>/video/<video_id>", methods=["DELETE"])
@@ -140,6 +166,32 @@ def delete_video(collection_id, video_id):
140166
return {"message": str(e)}, 500
141167

142168

169+
@videodb_bp.route("/collection/<collection_id>/audio/<audio_id>", methods=["DELETE"])
170+
def delete_audio(collection_id, audio_id):
171+
"""Delete a audio by ID from a specific collection."""
172+
try:
173+
if not audio_id:
174+
return {"message": "Video ID is required"}, 400
175+
videodb = VideoDBHandler(collection_id)
176+
result = videodb.delete_audio(audio_id)
177+
return result, 200
178+
except Exception as e:
179+
return {"message": str(e)}, 500
180+
181+
182+
@videodb_bp.route("/collection/<collection_id>/image/<image_id>", methods=["DELETE"])
183+
def delete_image(collection_id, image_id):
184+
"""Delete a image by ID from a specific collection."""
185+
try:
186+
if not image_id:
187+
return {"message": "Video ID is required"}, 400
188+
videodb = VideoDBHandler(collection_id)
189+
result = videodb.delete_image(image_id)
190+
return result, 200
191+
except Exception as e:
192+
return {"message": str(e)}, 500
193+
194+
143195
@videodb_bp.route(
144196
"/collection/<collection_id>/image/<image_id>/generate_url", methods=["GET"]
145197
)
@@ -157,6 +209,22 @@ def generate_image_url(collection_id, image_id):
157209
except Exception as e:
158210
return {"message": str(e)}, 500
159211

212+
@videodb_bp.route(
213+
"/collection/<collection_id>/audio/<audio_id>/generate_url", methods=["GET"]
214+
)
215+
def generate_audio_url(collection_id, audio_id):
216+
try:
217+
if not collection_id:
218+
return {"message": "Collection ID is required"}, 400
219+
220+
if not audio_id:
221+
return {"message": "Audio ID is required"}, 400
222+
223+
videodb = VideoDBHandler(collection_id)
224+
result = videodb.generate_audio_url(audio_id)
225+
return result, 200
226+
except Exception as e:
227+
return {"message": str(e)}, 500
160228

161229
@videodb_bp.route("/collection/<collection_id>/upload", methods=["POST"])
162230
def upload_video(collection_id):

‎backend/director/handler.py‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def __init__(self, collection_id="default"):
148148
def upload(self, source, source_type="url", media_type="video", name=None):
149149
return self.videodb_tool.upload(source, source_type, media_type, name)
150150

151-
def get_collection(self):
151+
def get_collection(self, collection_id):
152152
"""Get a collection by ID."""
153153
return self.videodb_tool.get_collection()
154154

@@ -170,10 +170,40 @@ def delete_video(self, video_id):
170170
"""Delete a specific video by its ID."""
171171
return self.videodb_tool.delete_video(video_id)
172172

173+
def delete_image(self, image_id):
174+
"""Delete a specific image by its ID."""
175+
return self.videodb_tool.delete_image(image_id)
176+
177+
def delete_audio(self, video_id):
178+
"""Delete a specific audio by its ID."""
179+
return self.videodb_tool.delete_audio(video_id)
180+
173181
def get_videos(self):
174182
"""Get all videos in a collection."""
175183
return self.videodb_tool.get_videos()
176184

185+
def get_audio(self, audio_id):
186+
"""Get a audio by ID."""
187+
return self.videodb_tool.get_audio(audio_id)
188+
189+
def get_audios(self):
190+
"""Get all audios in a collection."""
191+
return self.videodb_tool.get_audios()
192+
193+
def generate_audio_url(self, audio_id):
194+
return self.videodb_tool.generate_audio_url(audio_id=audio_id)
195+
196+
def delete_audio(self, audio_id):
197+
return self.videodb_tool.delete_audio(audio_id)
198+
199+
def get_image(self, image_id):
200+
"""Get a image by ID."""
201+
return self.videodb_tool.get_image(image_id)
202+
203+
def get_images(self):
204+
"""Get all images in a collection."""
205+
return self.videodb_tool.get_images()
206+
177207
def generate_image_url(self, image_id):
178208
return self.videodb_tool.generate_image_url(image_id=image_id)
179209

‎backend/director/tools/videodb_tool.py‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ def get_image(self, image_id: str = None):
5353
except Exception as e:
5454
raise Exception(f"Failed to fetch image with ID {image_id}: {e}")
5555

56+
def get_images(self):
57+
"""Get all images in a collection."""
58+
images = self.collection.get_images()
59+
return [
60+
{
61+
"id": image.id,
62+
"collection_id": image.collection_id,
63+
"name": image.name,
64+
"url": image.url,
65+
"type": "image",
66+
}
67+
for image in images
68+
]
69+
5670
def create_collection(self, name, description=""):
5771
"""Create a new collection with the given name and description."""
5872
if not name:
@@ -140,6 +154,7 @@ def get_videos(self):
140154
"stream_url": video.stream_url,
141155
"length": video.length,
142156
"thumbnail_url": video.thumbnail_url,
157+
"type": "video",
143158
}
144159
for video in videos
145160
]
@@ -161,12 +176,18 @@ def get_audios(self):
161176
return [
162177
{
163178
"id": audio.id,
179+
"collection_id": audio.collection_id,
164180
"name": audio.name,
165181
"length": audio.length,
182+
"type": "audio",
166183
}
167184
for audio in audios
168185
]
169186

187+
def generate_audio_url(self, audio_id):
188+
audio = self.collection.get_audio(audio_id)
189+
return audio.generate_url()
190+
170191
def generate_image_url(self, image_id):
171192
image = self.collection.get_image(image_id)
172193
return image.generate_url()
@@ -320,3 +341,58 @@ def add_subtitle(self, video_id, style: SubtitleStyle = SubtitleStyle()):
320341
video = self.collection.get_video(video_id)
321342
stream_url = video.add_subtitle(style)
322343
return stream_url
344+
345+
def delete_audio(self, audio_id):
346+
"""Delete a specific audio by its ID."""
347+
if not audio_id:
348+
raise ValueError("Audio ID is required to delete a audio.")
349+
try:
350+
audio = self.collection.get_audio(audio_id)
351+
if not audio:
352+
raise ValueError(
353+
f"Audio with ID {audio_id} not found in collection {self.collection.id}."
354+
)
355+
356+
audio.delete()
357+
return {
358+
"success": True,
359+
"message": f"Video {audio.id} deleted successfully",
360+
}
361+
except ValueError as ve:
362+
logging.error(f"ValueError while deleting video: {ve}")
363+
raise ve
364+
except Exception as e:
365+
logging.exception(
366+
f"Unexpected error occurred while deleting video {audio_id}"
367+
)
368+
raise Exception(
369+
"An unexpected error occurred while deleting the video. Please try again later."
370+
)
371+
372+
def delete_image(self, image_id):
373+
"""Delete a specific image by its ID."""
374+
if not image_id:
375+
raise ValueError("Image ID is required to delete a image.")
376+
try:
377+
image = self.collection.get_image(image_id)
378+
if not image:
379+
raise ValueError(
380+
f"Image with ID {image_id} not found in collection {self.collection.id}."
381+
)
382+
383+
image.delete()
384+
return {
385+
"success": True,
386+
"message": f"Image {image_id} deleted successfully",
387+
}
388+
except ValueError as ve:
389+
logging.error(f"ValueError while deleting video: {ve}")
390+
raise ve
391+
except Exception as e:
392+
logging.exception(
393+
f"Unexpected error occurred while deleting video {image_id}"
394+
)
395+
raise Exception(
396+
"An unexpected error occurred while deleting the video. Please try again later."
397+
)
398+

0 commit comments

Comments
 (0)