Commit 5246336b authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

clean

parent c8cf1d57
Loading
Loading
Loading
Loading
+28 −1
Original line number Diff line number Diff line
@@ -8,7 +8,27 @@ import os
from pathlib import Path


def file_passthru(request: HttpRequest, path: str) -> HttpResponseBase:
def file_passthru_noauth(request: HttpRequest, path: str) -> HttpResponseBase:
    try:
        file_hash = path[6:70]
        resource_instance = Resource.objects.get(
            file__hash=file_hash,
        )
        tested_path = os.path.realpath(Path(settings.HLS_DIR, path))
        if not tested_path.startswith(settings.HLS_DIR):
            raise PermissionDenied()
        if not Path(tested_path).exists():
            raise Http404()
        return RangedFileResponse(
            request,
            open(tested_path, "rb"),
            content_type=resource_instance.file.file_type.mime,
        )
    except Resource.DoesNotExist:
        raise Http404()


def file_passthru_auth(request: HttpRequest, path: str) -> HttpResponseBase:
    if not request.user.is_anonymous:
        file_hash = path[6:70]
        for project_instance in Project.objects.filter(
@@ -31,3 +51,10 @@ def file_passthru(request: HttpRequest, path: str) -> HttpResponseBase:
                    content_type=resource_instance.file.file_type.mime,
                )
    raise PermissionDenied()


def file_passthru(request: HttpRequest, path: str) -> HttpResponseBase:
    if settings.HLS_URLS_NEED_AUTH:
        return file_passthru_auth(request, path)
    else:
        return file_passthru_noauth(request, path)
+5 −1
Original line number Diff line number Diff line
from django.http import StreamingHttpResponse, HttpResponse, HttpRequest
from revproxy.views import ProxyView
from django.conf import settings
from django.shortcuts import redirect
from revproxy.response import get_django_response
from typing import Union


def get_request_headers(self):
@@ -51,7 +53,9 @@ class IIIFProxyView(ProxyView):
    upstream = settings.JAMA_IIIF_UPSTREAM_URL
    add_x_forwarded = True

    def dispatch(self, request, path):
    def dispatch(
        self, request: HttpRequest, path
    ) -> Union[StreamingHttpResponse, HttpResponse]:
        # print(f"### Upstream in setting: {settings.JAMA_IIIF_UPSTREAM_URL}")
        # print(f"### Upstream in class: {self.upstream}")
        # print(request.path)
+4 −1
Original line number Diff line number Diff line
@@ -77,10 +77,13 @@ JAMA_IIIF_UPSCALING_PREFIX = (
IIIF_DIR = os.getenv("JAMA_IIIF_DIR") or str(Path(VAR_DIR, "iiif").resolve())
os.makedirs(IIIF_DIR, exist_ok=True)
IIIF_PATH_SEPARATOR = os.getenv("JAMA_IIIF_PATH_SEPARATOR") or os.path.sep
IIIF_URLS_NEED_AUTH = (
    False if os.getenv("JAMA_IIIF_URLS_NEED_AUTH", "0") == "0" else True
)

HLS_DIR = os.getenv("JAMA_HLS_DIR") or str(Path(VAR_DIR, "hls").resolve())
os.makedirs(HLS_DIR, exist_ok=True)

HLS_URLS_NEED_AUTH = False if os.getenv("JAMA_HLS_URLS_NEED_AUTH", "0") == "0" else True

ARK_SERVER = os.getenv("JAMA_ARK_SERVER", "")
ARK_APP_ID = os.getenv("JAMA_ARK_APP_ID", "")
+21 −6
Original line number Diff line number Diff line
@@ -249,21 +249,36 @@ class UserAccess:
        return self._permissions

    def can_crud(self, object_or_class, crud_access) -> bool:
        return has_crud_access(self.permissions, object_or_class, crud_access)
        return (
            has_crud_access(self.permissions, object_or_class, crud_access)
            and self.user.is_active
        )

    def can_create(self, object_or_class) -> bool:
        return has_crud_access(self.permissions, object_or_class, CRUD_CREATE)
        return (
            has_crud_access(self.permissions, object_or_class, CRUD_CREATE)
            and self.user.is_active
        )

    def can_read(self, object_or_class) -> bool:
        if self.user.is_superuser:  # superuser gets a pass
        if self.user.is_superuser and self.user.is_active:  # superuser gets a pass
            return True
        return has_crud_access(self.permissions, object_or_class, CRUD_READ)
        return (
            has_crud_access(self.permissions, object_or_class, CRUD_READ)
            and self.user.is_active
        )

    def can_update(self, object_or_class) -> bool:
        return has_crud_access(self.permissions, object_or_class, CRUD_UPDATE)
        return (
            has_crud_access(self.permissions, object_or_class, CRUD_UPDATE)
            and self.user.is_active
        )

    def can_delete(self, object_or_class) -> bool:
        return has_crud_access(self.permissions, object_or_class, CRUD_DELETE)
        return (
            has_crud_access(self.permissions, object_or_class, CRUD_DELETE)
            and self.user.is_active
        )

    def check_create(self, object_or_class):
        """
+0 −9
Original line number Diff line number Diff line
@@ -202,15 +202,6 @@ def file(
            settings.JAMA_IIIF_ENDPOINT, iiif_path, settings.JAMA_IIIF_UPSCALING_PREFIX
        )
    if include_metas:
        # This will NOT use the prefetched data and will generate a HUGE
        # number of queries with large sets of resources (see Herbier Corbières):
        #
        # for prop in file_instance.metadataresourcevalue_set.filter(
        #    metadata__expose=True
        # ).order_by("-metadata__set", "metadata__rank", "id"):
        #    props.append(metadata_resource_value(prop, cache=cache))
        #
        # Slightly different result but much less
        for prop in file_instance.metadataresourcevalue_set.all():
            if prop.metadata.expose and prop.metadata.title.lower().strip() not in [
                "sourcefile",