From cea17cb88c7aa5fab75e268adb3048eb2524bb65 Mon Sep 17 00:00:00 2001 From: Jacob Gallagher Date: Fri, 27 Oct 2023 11:02:55 -0600 Subject: [PATCH 1/4] Replace magic number, handle non-standard path files --- api/views/views.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/api/views/views.py b/api/views/views.py index 2b05c8a6c9..6b6722ec3d 100644 --- a/api/views/views.py +++ b/api/views/views.py @@ -8,7 +8,7 @@ from constance import config as site_config from django.conf import settings from django.db.models import Q, Sum -from django.http import HttpResponse, HttpResponseForbidden, StreamingHttpResponse +from django.http import HttpResponse, HttpResponseForbidden, StreamingHttpResponse, FileResponse from django.utils.decorators import method_decorator from django.utils.encoding import iri_to_uri from django.views.decorators.csrf import ensure_csrf_cookie @@ -549,8 +549,11 @@ def get(self, request, path, fname, format=None): "/nextcloud_media/", "/nextcloud_original/" ) internal_path = "/nextcloud_original" + photo.main_file.path[21:] + elif photo.main_file.path.startswith(settings.PHOTOS): + internal_path = "/original" + photo.main_file.path[len(settings.PHOTOS):] else: - internal_path = "/original" + photo.main_file.path[5:] + # If, for some reason, the file is in a weird place, handle that. + internal_path = None internal_path = quote(internal_path) @@ -576,21 +579,30 @@ def get(self, request, path, fname, format=None): # or the photo is shared with the user image_hash = fname.split(".")[0].split("_")[0] # janky alert user = User.objects.filter(id=token["user_id"]).only("id").first() - if photo.owner == user or user in photo.shared_to.all(): + + if internal_path is not None: response = HttpResponse() mime = magic.Magic(mime=True) filename = mime.from_file(photo.main_file.path) response["Content-Type"] = filename response["X-Accel-Redirect"] = internal_path + else: + try: + response = FileResponse(open(photo.main_file.path, 'rb')) + except FileNotFoundError: + return HttpResponse(status=404) + except PermissionError: + return HttpResponse(status=403) + except IOError: + return HttpResponse(status=500) + except: + raise + + if photo.owner == user or user in photo.shared_to.all(): return response else: for album in photo.albumuser_set.only("shared_to"): if user in album.shared_to.all(): - response = HttpResponse() - mime = magic.Magic(mime=True) - filename = mime.from_file(photo.main_file.path) - response["Content-Type"] = filename - response["X-Accel-Redirect"] = internal_path return response return HttpResponse(status=404) From c78094d34ad52a41f98503378e4772f0a937df0e Mon Sep 17 00:00:00 2001 From: Jacob Gallagher Date: Sat, 28 Oct 2023 11:58:00 -0600 Subject: [PATCH 2/4] Save finished scan job to db --- api/directory_watcher.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/api/directory_watcher.py b/api/directory_watcher.py index d9a8452e08..7d368a8688 100644 --- a/api/directory_watcher.py +++ b/api/directory_watcher.py @@ -392,6 +392,10 @@ def scan_photos(user, full_scan, job_id, scan_directory="", scan_files=[]): util.logger.exception("An error occurred: ") lrj.failed = True + lrj.finished_at = datetime.datetime.now().replace(tzinfo=pytz.utc) + lrj.finished = True + lrj.save() + added_photo_count = Photo.objects.count() - photo_count_before util.logger.info("Added {} photos".format(added_photo_count)) From 09cef1efaae19a929d655ec3af56f31781ab5179 Mon Sep 17 00:00:00 2001 From: Jacob Gallagher Date: Sun, 29 Oct 2023 07:52:13 -0600 Subject: [PATCH 3/4] Use environment variable instead of hard-coded DB port --- librephotos/settings/production.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/librephotos/settings/production.py b/librephotos/settings/production.py index d6efc5ed45..4f6a661955 100644 --- a/librephotos/settings/production.py +++ b/librephotos/settings/production.py @@ -218,7 +218,7 @@ "USER": os.environ.get("DB_USER", "docker"), "PASSWORD": os.environ.get("DB_PASS", "AaAa1234"), "HOST": os.environ.get("DB_HOST", "db"), - "PORT": "5432", + "PORT": os.environ.get("DB_PORT", "5432"), }, } From c67c4dcee1794f02c592bfddb8f4418ace01b3b8 Mon Sep 17 00:00:00 2001 From: Jacob Gallagher Date: Fri, 29 Dec 2023 09:01:20 -0700 Subject: [PATCH 4/4] Use data root for storage used --- api/views/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/views/views.py b/api/views/views.py index 6b6722ec3d..b009844240 100644 --- a/api/views/views.py +++ b/api/views/views.py @@ -180,7 +180,7 @@ class StorageStatsView(APIView): def get(self, request, format=None): import shutil - total_storage, used_storage, free_storage = shutil.disk_usage("/") + total_storage, used_storage, free_storage = shutil.disk_usage(settings.DATA_ROOT) return Response( { "total_storage": total_storage,