tlong-ds commited on
Commit
fbbfcf2
·
verified ·
1 Parent(s): 826e4bc

Update services/api/api_endpoints.py

Browse files
Files changed (1) hide show
  1. services/api/api_endpoints.py +4 -46
services/api/api_endpoints.py CHANGED
@@ -10,7 +10,6 @@ import boto3
10
  import json
11
  import threading
12
  import asyncio
13
- from io import BytesIO
14
  from botocore.exceptions import ClientError
15
  from datetime import datetime
16
  from fastapi.middleware.cors import CORSMiddleware
@@ -2119,58 +2118,17 @@ async def create_lecture(
2119
  # Commit all database changes at once
2120
  conn.commit()
2121
 
2122
- # Return immediately after database operations - handle video upload asynchronously
2123
  response = {
2124
  "id": lecture_id,
2125
  "title": title,
2126
  "message": "Lecture created successfully"
2127
  }
2128
 
2129
- # Handle video upload synchronously but efficiently
 
2130
  if video:
2131
- try:
2132
- # File validation
2133
- video.file.seek(0, 2) # Seek to end
2134
- file_size = video.file.tell()
2135
- video.file.seek(0) # Reset to beginning
2136
-
2137
- if file_size > 500 * 1024 * 1024: # 500MB in bytes
2138
- raise HTTPException(status_code=400, detail="Video file size must be less than 500MB")
2139
-
2140
- # Check file type
2141
- if not video.content_type.startswith('video/'):
2142
- raise HTTPException(status_code=400, detail="Invalid file type. Please upload a video file")
2143
-
2144
- # Read video content into memory for upload
2145
- video_content = video.file.read()
2146
- video.file.seek(0) # Reset file position
2147
-
2148
- # Upload to S3 with the video content
2149
- media_path = f"videos/cid{course_id}/lid{lecture_id}/vid_lecture.mp4"
2150
-
2151
- # Create BytesIO object from video content
2152
- video_stream = BytesIO(video_content)
2153
-
2154
- # Upload to S3 using the BytesIO stream
2155
- s3.upload_fileobj(
2156
- video_stream,
2157
- "tlhmaterials",
2158
- media_path,
2159
- ExtraArgs={
2160
- 'ContentType': video.content_type,
2161
- 'ACL': 'public-read',
2162
- 'ContentDisposition': 'inline'
2163
- }
2164
- )
2165
-
2166
- print(f"Video upload completed for lecture {lecture_id}")
2167
- response["video_status"] = "uploaded"
2168
- response["video_url"] = f"https://tlhmaterials.s3-{REGION}.amazonaws.com/{media_path}"
2169
- response["message"] = "Lecture created successfully with video."
2170
-
2171
- except Exception as video_error:
2172
- print(f"Video upload failed: {str(video_error)}")
2173
- response["warning"] = f"Lecture created but video upload failed: {str(video_error)}"
2174
 
2175
 
2176
  # Invalidate cache patterns in background (non-blocking)
 
10
  import json
11
  import threading
12
  import asyncio
 
13
  from botocore.exceptions import ClientError
14
  from datetime import datetime
15
  from fastapi.middleware.cors import CORSMiddleware
 
2118
  # Commit all database changes at once
2119
  conn.commit()
2120
 
2121
+ # Prepare response
2122
  response = {
2123
  "id": lecture_id,
2124
  "title": title,
2125
  "message": "Lecture created successfully"
2126
  }
2127
 
2128
+ # Note: Video upload is now handled separately through upload_endpoints.py
2129
+ # This separates concerns and allows for better error handling and chunked uploads
2130
  if video:
2131
+ response["note"] = "Lecture created successfully. Please use the dedicated upload endpoints for video upload."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2132
 
2133
 
2134
  # Invalidate cache patterns in background (non-blocking)