This commit is contained in:
2025-04-13 11:59:50 +02:00
parent 80640d2580
commit fe3191d4a2
21 changed files with 567 additions and 117 deletions

View File

@@ -73,7 +73,7 @@ class File(models.Model):
def mime_types(self):
mime = mimetypes.guess_type(self.pathname)
if mime:
return mime
return mime[0] or mime[1] or "application/octet-stream"
else:
return "application/octet-stream"
@@ -87,7 +87,21 @@ class File(models.Model):
@property
def accel_redirect(self):
return shlex.quote(f"{settings.NGINX_ACCEL_BASE}/{self.pathname}")
# Encode chaque partie du chemin séparément pour préserver la structure
encoded_parts = []
for part in self.pathname.parts:
# Ignorer un slash initial si présent
if part == '/' or part == '\\':
continue
encoded_parts.append(quote(part))
# Construction du chemin final avec le préfixe Nginx
if settings.NGINX_ACCEL_BASE.endswith('/'):
base = settings.NGINX_ACCEL_BASE.rstrip('/')
else:
base = settings.NGINX_ACCEL_BASE
return f"{base}/{'/'.join(encoded_parts)}"
@property
def disposition(self):

View File

@@ -6,4 +6,14 @@
const current_user = {{ request.user.min_infos|safe }};
</script>
{% vite_asset "app/torrent.js" %}
<script>
function QWebInit(){
if(typeof(QWebChannel) !== 'undefined'){
new QWebChannel(qt.webChannelTransport, channel => {
})
}
}
</script>
{% endblock %}

View File

@@ -1,10 +1,11 @@
from django.urls import path
from .views import HomeView, download_file, download_torrent
from .views import HomeView, download_file, download_torrent, pping
app_name = "torrent"
urlpatterns = [
path("", HomeView.as_view(), name="home"),
path("pping/", pping, name="pping"),
path("download_file/<uuid:file_id>", download_file, name="download_file"),
path("download_torrent/<str:torrent_id>", download_torrent, name="download_torrent"),
]

View File

@@ -6,11 +6,11 @@ from django.db.models import Q, Count, OuterRef
from django.db.models.functions import Coalesce
from django.http import HttpResponse, Http404, StreamingHttpResponse
import aiofiles
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 app.utils import StreamingZipFileResponse
from user.models import User
@@ -23,6 +23,10 @@ class HomeView(LoginRequiredMixin, TemplateView):
template_name = "torrent/home.html"
def pping(request):
return HttpResponse(str(dict(request.session)))
async def download_file(request, file_id):
user = await request.auser()
qs = File.objects.filter(
@@ -32,16 +36,16 @@ async def download_file(request, file_id):
| Q(torrent__shared_users__friends=user),
torrent__transmission_data__progress__gte=100,
pk=file_id
)
).distinct()
try:
file = await qs.aget()
except File.DoesNotExist:
raise Http404()
else:
if request.GET.get("dl_hotfix", "0") == "1":
if int(request.GET.get("dl_hotfix", 0)) == 1:
async def read_file():
async with aiofiles.open(file.abs_pathname, "rb") as f:
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())
@@ -57,6 +61,30 @@ async def download_file(request, file_id):
return response
async def flux_file(request, file_id):
user = await request.auser()
qs = File.objects.filter(
Q(torrent__user=user)
| Q(torrent__shared_users=user)
| Q(torrent__user__friends=user)
| Q(torrent__shared_users__friends=user),
torrent__transmission_data__progress__gte=100,
pk=file_id
).distinct()
try:
file = await qs.aget()
except File.DoesNotExist:
raise Http404()
else:
response = HttpResponse()
response["X-Accel-Redirect"] = file.accel_redirect
response["X-Accel-Buffering"] = "no"
response["Content-Type"] = file.mime_types
response["Content-Disposition"] = file.disposition
return response
async def download_torrent(request, torrent_id):
# py version
user = await request.auser()
@@ -67,11 +95,11 @@ async def download_torrent(request, torrent_id):
| Q(shared_users__friends=user),
transmission_data__progress__gte=100,
pk=torrent_id
).annotate(count_files=Count("files"))
).annotate(count_files=Count("files")).distinct()
torrent = await qs.aget()
if torrent.count_files == 1:
if await torrent.alen_files == 1:
file = await torrent.files.afirst()
return redirect(reverse("torrent:download_file", kwargs={
"file_id": file.pk