Пример #1
0
def handle_stream(track, request, download, explicit_file, format,
                  max_bitrate):
    actor = utils.get_actor_from_request(request)
    queryset = track.uploads.prefetch_related("track__album__artist",
                                              "track__artist")
    if explicit_file:
        queryset = queryset.filter(uuid=explicit_file)
    queryset = queryset.playable_by(actor)
    queryset = queryset.order_by(F("audio_file").desc(nulls_last=True))
    upload = queryset.first()
    if not upload:
        return Response(status=404)

    try:
        max_bitrate = min(max(int(max_bitrate), 0), 320) or None
    except (TypeError, ValueError):
        max_bitrate = None

    if max_bitrate:
        max_bitrate = max_bitrate * 1000
    return handle_serve(
        upload=upload,
        user=request.user,
        format=format,
        max_bitrate=max_bitrate,
        proxy_media=settings.PROXY_MEDIA,
        download=download,
        wsgi_request=request._request,
    )
Пример #2
0
    def retrieve(self, request, *args, **kwargs):
        track = self.get_object()
        actor = utils.get_actor_from_request(request)
        queryset = track.uploads.prefetch_related(
            "track__album__artist", "track__artist"
        )
        explicit_file = request.GET.get("upload")
        download = request.GET.get("download", "true").lower() == "true"
        if explicit_file:
            queryset = queryset.filter(uuid=explicit_file)
        queryset = queryset.playable_by(actor)
        queryset = queryset.order_by(F("audio_file").desc(nulls_last=True))
        upload = queryset.first()
        if not upload:
            return Response(status=404)

        format = request.GET.get("to")
        max_bitrate = request.GET.get("max_bitrate")
        try:
            max_bitrate = min(max(int(max_bitrate), 0), 320) or None
        except (TypeError, ValueError):
            max_bitrate = None

        if max_bitrate:
            max_bitrate = max_bitrate * 1000
        return handle_serve(
            upload=upload,
            user=request.user,
            format=format,
            max_bitrate=max_bitrate,
            proxy_media=settings.PROXY_MEDIA,
            download=download,
            wsgi_request=request._request,
        )
Пример #3
0
 def get_queryset(self):
     queryset = super().get_queryset()
     albums = (models.Album.objects.with_tracks_count().select_related(
         "attachment_cover").prefetch_related("tracks"))
     albums = albums.annotate_playable_by_actor(
         utils.get_actor_from_request(self.request))
     return queryset.prefetch_related(Prefetch("albums", queryset=albums),
                                      TAG_PREFETCH)
Пример #4
0
 def get_queryset(self):
     qs = super().get_queryset()
     if self.action in ["update", "partial_update"]:
         # prevent updating an upload that is already processed
         qs = qs.filter(import_status="draft")
     if self.action != "retrieve":
         qs = qs.filter(library__actor=self.request.user.actor)
     else:
         actor = utils.get_actor_from_request(self.request)
         qs = qs.playable_by(actor)
     return qs
Пример #5
0
    def get_queryset(self):
        qs = super().get_queryset()
        # allow retrieving a single library by uuid if request.user isn't
        # the owner. Any other get should be from the owner only
        if self.action not in ["retrieve", "list"]:
            qs = qs.filter(actor=self.request.user.actor)
        if self.action == "list":
            actor = utils.get_actor_from_request(self.request)
            qs = qs.viewable_by(actor)

        return qs
Пример #6
0
    def get_queryset(self):
        queryset = super().get_queryset()
        if self.action in ["destroy"]:
            queryset = queryset.exclude(artist__channel=None).filter(
                artist__attributed_to=self.request.user.actor)
        filter_favorites = self.request.GET.get("favorites", None)
        user = self.request.user
        if user.is_authenticated and filter_favorites == "true":
            queryset = queryset.filter(track_favorites__user=user)

        queryset = queryset.with_playable_uploads(
            utils.get_actor_from_request(self.request))
        return queryset.prefetch_related(TAG_PREFETCH)
Пример #7
0
 def get_queryset(self):
     queryset = super().get_queryset()
     if self.action in ["destroy"]:
         queryset = queryset.exclude(artist__channel=None).filter(
             artist__attributed_to=self.request.user.actor
         )
     tracks = (
         models.Track.objects.prefetch_related("artist")
         .with_playable_uploads(utils.get_actor_from_request(self.request))
         .order_for_album()
     )
     qs = queryset.prefetch_related(
         Prefetch("tracks", queryset=tracks), TAG_PREFETCH
     )
     return qs
Пример #8
0
    def libraries(self, request, *args, **kwargs):
        obj = self.get_object()
        actor = utils.get_actor_from_request(request)
        uploads = models.Upload.objects.all()
        uploads = filter_uploads(obj, uploads)
        uploads = uploads.playable_by(actor)
        qs = models.Library.objects.filter(
            pk__in=uploads.values_list("library", flat=True), channel=None,
        ).annotate(_uploads_count=Count("uploads"))
        qs = qs.prefetch_related("actor")
        page = self.paginate_queryset(qs)
        if page is not None:
            serializer = federation_api_serializers.LibrarySerializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = federation_api_serializers.LibrarySerializer(qs, many=True)
        return Response(serializer.data)