vpn integration

This commit is contained in:
2026-04-11 22:07:59 +02:00
parent c4d27e9842
commit 00ac38d126
47 changed files with 945 additions and 749 deletions
+43 -37
View File
@@ -1,21 +1,20 @@
import anyio
from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import Count, OuterRef, Q, Sum
from django.db.models.functions import Coalesce
from django.http import Http404, HttpResponse, StreamingHttpResponse
from django.shortcuts import redirect
from django.urls import reverse
from django.views.generic import TemplateView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import Q, Count, OuterRef, Sum
from django.db.models.functions import Coalesce
from django.http import HttpResponse, Http404, StreamingHttpResponse
from rest_framework.viewsets import GenericViewSet
from rest_framework import mixins
from rest_framework.response import Response
from rest_framework.decorators import action
import anyio
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet
from app.utils import StreamingZipFileResponse
from user.models import User
from .models import Torrent, File, SharedUser
from .serializers import TorrentSerializer, FileSerializer
from .models import File, SharedUser, Torrent
from .serializers import FileSerializer, TorrentSerializer
from .utils import torrent_proceed, torrent_share
@@ -35,7 +34,7 @@ async def download_file(request, file_id):
| Q(torrent__user__friends=user)
| Q(torrent__shared_users__friends=user),
torrent__transmission_data__progress__gte=100,
pk=file_id
pk=file_id,
).distinct()
try:
@@ -44,10 +43,12 @@ async def download_file(request, file_id):
raise Http404()
else:
if int(request.GET.get("dl_hotfix", 0)) == 1:
async def read_file():
async with await anyio.open_file(file.abs_pathname, "rb") as f:
while chunk := await f.read(128 * 1024):
yield chunk
response = StreamingHttpResponse(read_file())
response["Content-Length"] = file.size
response["Content-Type"] = "application/octet-stream"
@@ -86,7 +87,7 @@ async def secured_flux_file(request, file_id):
| Q(torrent__user__friends=user)
| Q(torrent__shared_users__friends=user),
torrent__transmission_data__progress__gte=100,
pk=file_id
pk=file_id,
).distinct()
try:
@@ -105,39 +106,42 @@ async def secured_flux_file(request, file_id):
async def download_torrent(request, torrent_id):
# py version
user = await request.auser()
qs = Torrent.objects.filter(
Q(user=user)
| Q(shared_users=user)
| Q(user__friends=user)
| Q(shared_users__friends=user),
transmission_data__progress__gte=100,
pk=torrent_id
).annotate(count_files=Count("files")).distinct()
qs = (
Torrent.objects.filter(
Q(user=user)
| Q(shared_users=user)
| Q(user__friends=user)
| Q(shared_users__friends=user),
transmission_data__progress__gte=100,
pk=torrent_id,
)
.annotate(count_files=Count("files"))
.distinct()
)
torrent = await qs.aget()
if await torrent.alen_files == 1:
file = await torrent.files.afirst()
return redirect(reverse("torrent:download_file", kwargs={
"file_id": file.pk
}))
return redirect(reverse("torrent:download_file", kwargs={"file_id": file.pk}))
response = StreamingZipFileResponse(
filename=f"{torrent.name}.zip",
file_list=[
(file.abs_pathname, file.rel_name)
async for file in torrent.files.all()
(file.abs_pathname, file.rel_name) async for file in torrent.files.all()
],
is_async=True
is_async=True,
)
return response
class TorrentViewSet(mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
mixins.ListModelMixin,
GenericViewSet):
class TorrentViewSet(
mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
mixins.ListModelMixin,
GenericViewSet,
):
queryset = Torrent.objects.all().annotate(count_files=Count("files"))
serializer_class = TorrentSerializer
@@ -158,7 +162,9 @@ class TorrentViewSet(mixins.CreateModelMixin,
else:
user_id = self.request.user.id
sub = SharedUser.objects.filter(torrent_id=OuterRef("pk"), user_id=user_id).values("date_created")
sub = SharedUser.objects.filter(
torrent_id=OuterRef("pk"), user_id=user_id
).values("date_created")
qs = qs.annotate(last_date=Coalesce(sub, "date_created")).order_by("-last_date")
search = self.request.query_params.get("search", None)
@@ -188,7 +194,9 @@ class TorrentViewSet(mixins.CreateModelMixin,
def share(self, request, pk):
user_id = self.request.data.get("user_id")
torrent = self.get_object()
is_share_success = torrent_share(torrent=torrent, current_user=self.request.user, target_user_id=user_id)
is_share_success = torrent_share(
torrent=torrent, current_user=self.request.user, target_user_id=user_id
)
return Response({"success": is_share_success})
@action(methods=["get"], detail=False)
@@ -196,9 +204,7 @@ class TorrentViewSet(mixins.CreateModelMixin,
Torrent.objects.filter(user=self.request.user).aggregate(total_size=Sum("size"))
class FileViewSet(mixins.RetrieveModelMixin,
mixins.ListModelMixin,
GenericViewSet):
class FileViewSet(mixins.RetrieveModelMixin, mixins.ListModelMixin, GenericViewSet):
queryset = File.objects.all()
serializer_class = FileSerializer
filterset_fields = ["torrent"]