Skip to content

Commit

Permalink
Merge pull request #1118 from jacobpgallagher/bug_fixes
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
derneuere authored Jan 6, 2024
2 parents 7f64a6c + c67c4dc commit f92992c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
4 changes: 4 additions & 0 deletions api/directory_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
30 changes: 21 additions & 9 deletions api/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -183,7 +183,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,
Expand Down Expand Up @@ -552,8 +552,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)

Expand All @@ -579,21 +582,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)

Expand Down
2 changes: 1 addition & 1 deletion librephotos/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,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"),
},
}

Expand Down

0 comments on commit f92992c

Please sign in to comment.