Skip to content

Commit 003a16b

Browse files
committed
⚡️(back) redirect video url instead of streaming on /transcript-source
The transcript-source video api endpoint was streaming the video content. Instead of doing this we can return a permanent redirect response to the video url. Peertube runner is able to manage this redirection, it releases pressure on our servers and will directly use a CDN URL.
1 parent dbe4127 commit 003a16b

File tree

3 files changed

+9
-58
lines changed

3 files changed

+9
-58
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Versioning](https://semver.org/spec/v2.0.0.html).
1212

1313
- Allow to configure transcoding resolutions
1414
- Upgrade to python 3.12
15+
- Redirect video url instead of streaming it on /transcript-source
1516

1617
## [5.3.1] - 2024-11-07
1718

src/backend/marsha/core/api/video.py

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@
1111
from django.core.exceptions import ValidationError
1212
from django.db import IntegrityError, OperationalError, transaction
1313
from django.db.models import F, Func, Q, Value
14-
from django.http import Http404, StreamingHttpResponse
15-
from django.shortcuts import get_object_or_404
14+
from django.http import Http404
15+
from django.shortcuts import get_object_or_404, redirect
1616
from django.urls import reverse
1717
from django.utils import timezone
1818
from django.utils.module_loading import import_string
1919

2020
from boto3.exceptions import Boto3Error
2121
import django_filters
2222
from django_peertube_runner_connector.models import RunnerJob
23-
import requests
2423
from rest_framework import filters, status, viewsets
2524
from rest_framework.decorators import action
2625
from rest_framework.exceptions import APIException, MethodNotAllowed
@@ -1283,24 +1282,6 @@ def transcript_source(self, request, pk=None):
12831282
{"detail": "No video source available for this video."},
12841283
status=HTTPStatus.NOT_FOUND,
12851284
)
1286-
video_url = video_urls.get(min(video_urls.keys()))
1285+
video_url = video_urls.get(max(video_urls.keys()))
12871286

1288-
# stream the video source
1289-
try:
1290-
response = requests.get(
1291-
video_url,
1292-
stream=True,
1293-
timeout=settings.TRANSCRIPTION_VIDEO_SOURCE_TIMEOUT,
1294-
)
1295-
response.raise_for_status()
1296-
except (requests.HTTPError, requests.RequestException) as err:
1297-
return Response(
1298-
{"detail": f"Error occurred: {err}"}, status=HTTPStatus.BAD_REQUEST
1299-
)
1300-
1301-
return StreamingHttpResponse(
1302-
response.iter_content(
1303-
chunk_size=settings.TRANSCRIPTION_VIDEO_SOURCE_CHUNK_SIZE
1304-
),
1305-
content_type=response.headers["Content-Type"],
1306-
)
1287+
return redirect(video_url, permanent=True)

src/backend/marsha/core/tests/api/video/test_transcript_source.py

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
from http import HTTPStatus
55

66
from django_peertube_runner_connector.factories import RunnerJobFactory
7-
import requests
8-
import responses
97
from rest_framework.test import APITestCase
108

119
from marsha.core import factories
@@ -26,30 +24,21 @@ def setUp(self):
2624
)
2725
self.url = f"/api/videos/{self.video.pk}/transcript-source/"
2826
serializer = VideoSerializer(self.video)
29-
self.video_url = serializer.data.get("urls").get("mp4").get(144)
27+
self.video_url = serializer.data.get("urls").get("mp4").get(720)
3028
runner_job = RunnerJobFactory()
3129
self.runner_job_data = {
3230
"runnerToken": runner_job.runner.runnerToken,
3331
"jobToken": runner_job.processingJobToken,
3432
}
3533

36-
@responses.activate
3734
def test_api_video_transcript_source(self):
38-
"""The API should return the video source."""
39-
responses.get(
40-
url=self.video_url,
41-
body=b"video content",
42-
status=200,
43-
content_type="video/mp4",
44-
)
35+
"""The API should redirect to the video url."""
4536

4637
response = self.client.post(self.url, data=self.runner_job_data)
4738

48-
self.assertEqual(response.status_code, HTTPStatus.OK)
49-
self.assertEqual(response["Content-Type"], "video/mp4")
50-
self.assertEqual(b"".join(response.streaming_content), b"video content")
39+
self.assertEqual(response.status_code, HTTPStatus.MOVED_PERMANENTLY)
40+
self.assertEqual(response["Location"], self.video_url)
5141

52-
@responses.activate
5342
def test_api_video_transcript_source_missing_video_url(self):
5443
"""The API should return a 404 if the video source is not available."""
5544
self.video.resolutions = []
@@ -63,28 +52,8 @@ def test_api_video_transcript_source_missing_video_url(self):
6352
status_code=HTTPStatus.NOT_FOUND,
6453
)
6554

66-
@responses.activate
67-
def test_api_video_transcript_source_request_failure(self):
68-
"""The API should return a 400 if the request to the video source fails."""
69-
responses.get(
70-
url=self.video_url,
71-
body=requests.RequestException(),
72-
status=500,
73-
)
74-
75-
response = self.client.post(self.url, data=self.runner_job_data)
76-
77-
self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST)
78-
79-
@responses.activate
8055
def test_api_video_transcript_source_no_runner(self):
8156
"""The API should return a 403 if the request is not from a runner job."""
82-
responses.get(
83-
url=self.video_url,
84-
body=b"video content",
85-
status=200,
86-
content_type="video/mp4",
87-
)
8857
data = {
8958
"runnerToken": "unknown-runner-token",
9059
"jobToken": "unknown-job-token",

0 commit comments

Comments
 (0)