Skip to content

Commit

Permalink
feat(server): add download view for checking download requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ericswpark committed Oct 11, 2023
1 parent 629d8c5 commit 7381dc4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions server/downloads/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
DownloadsDeviceView,
DownloadsMainView,
LanguageSwitchView,
download_view,
)

urlpatterns = [
Expand All @@ -20,4 +21,7 @@
name="downloads_build",
),
path("language_switch/", LanguageSwitchView.as_view(), name="language_switch"),
path(
"media/<slug:codename>/<slug:file_name>/", download_view, name="download_view"
),
]
15 changes: 15 additions & 0 deletions server/downloads/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.views.generic import DetailView, ListView, TemplateView
from django.shortcuts import render
from core.models import Build, Device

from constance import config


class DownloadsMainView(TemplateView):
template_name = "downloads_main.html"
Expand Down Expand Up @@ -53,3 +56,15 @@ def get(self, request, *args, **kwargs):
if not redirect_url:
redirect_url = "/"
return render(request, self.template_name, {"redirect_to": redirect_url})


def download_view(request, codename, file_name):
build = get_object_or_404(Build, file_name=file_name, device__codename=codename)
response = HttpResponse()
response["X-Accel-Redirect"] = f"/media/{codename}/{file_name}"

if build.is_archived:
limit_speed = config.SHIPPER_DOWNLOADS_ARCHIVE_THROTTLE * 1000
response["X-Accel-Limit-Rate"] = str(limit_speed)

return response

0 comments on commit 7381dc4

Please sign in to comment.