Skip to content

Commit

Permalink
Minor speed improvements (#2478)
Browse files Browse the repository at this point in the history
* less serializations
* disable livewire hack (#2479)
* add option to disable login requirements on albums (#2480)
  • Loading branch information
ildyria authored Jun 25, 2024
1 parent 81579f3 commit d347d42
Show file tree
Hide file tree
Showing 22 changed files with 118 additions and 52 deletions.
28 changes: 21 additions & 7 deletions app/Http/Middleware/LoginRequired.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Exceptions\ConfigurationException;
use App\Exceptions\ConfigurationKeyMissingException;
use App\Exceptions\Internal\FrameworkException;
use App\Exceptions\Internal\LycheeLogicException;
use App\Models\Configs;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
Expand All @@ -19,32 +20,45 @@
*/
class LoginRequired
{
public const ROOT = 'root';
public const ALBUM = 'album';

/**
* Handle an incoming request.
*
* @param Request $request
* @param \Closure $next
*
* @return mixed
* @param Request $request the incoming request to serve
* @param \Closure $next the next operation to be applied to the
* request
* @param string $requiredStatus the required login status; either
* {@link self::ROOT} or
* {@link self::ALBUM}
*
* @throws ConfigurationException
* @throws FrameworkException
*/
public function handle(Request $request, \Closure $next): mixed
public function handle(Request $request, \Closure $next, string $requiredStatus): mixed
{
// We are logged in. Proceed.
if (Auth::user() !== null) {
return $next($request);
}

$dir_url = config('app.dir_url');
if (Features::inactive('livewire') && !Str::startsWith($request->getRequestUri(), $dir_url . '/livewire/')) {
return $next($request);
}

if ($requiredStatus !== self::ALBUM && $requiredStatus !== self::ROOT) {
throw new LycheeLogicException($requiredStatus . ' is not a valid login requirement.');
}

try {
if (!Configs::getValueAsBool('login_required')) {
// Login is not required. Proceed.
return $next($request);
}

if (Auth::user() !== null) {
// We are logged in. Proceed.
if ($requiredStatus === self::ALBUM && Configs::getValueAsBool('login_required_root_only')) {
return $next($request);
}

Expand Down
4 changes: 2 additions & 2 deletions app/Http/Resources/Models/PhotoResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ private function preformatted(?SizeVariant $original): array
'resolution' => $original?->width . ' x ' . $original?->height,
'latitude' => Helpers::decimalToDegreeMinutesSeconds($this->resource->latitude, true),
'longitude' => Helpers::decimalToDegreeMinutesSeconds($this->resource->longitude, false),
'altitude' => round($this->resource->altitude, 1) . 'm',
'altitude' => $this->resource->altitude !== null ? round($this->resource->altitude, 1) . 'm' : '',
'license' => $this->resource->license !== LicenseType::NONE ? $this->resource->license->localization() : '',
'description' => ($this->resource->description ?? '') === '' ? '' : Markdown::convert($this->resource->description)->getContent(),
];
}

/**
* @return array<string,mixed>
* @return array<string,bool>
*/
private function precomputed(): array
{
Expand Down
2 changes: 1 addition & 1 deletion app/Livewire/Components/Menus/LeftMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,6 @@ private function loadDevMenu(): void
$this->doc_api_url = Route::has('scramble.docs.api') ? route('scramble.docs.api') : null;

// Double check to avoid showing an empty section.
$this->has_dev_tools = $this->doc_api_url !== null && $this->clockwork_url !== null;
$this->has_dev_tools = $this->doc_api_url !== null || $this->clockwork_url !== null;
}
}
6 changes: 1 addition & 5 deletions app/Livewire/Components/Pages/Gallery/Album.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\View\View;
use Livewire\Attributes\Locked;
use Livewire\Attributes\On;
use Livewire\Attributes\Renderless;

Expand All @@ -42,8 +41,6 @@ class Album extends BaseAlbumComponent implements Reloadable
{
private AlbumFactory $albumFactory;
private AlbumFormatted $formatted;

#[Locked] public bool $is_search_accessible = false;
public ?AbstractAlbum $album = null;

/**
Expand Down Expand Up @@ -134,8 +131,7 @@ public function reloadPage(): void
{
$this->album = $this->albumFactory->findAbstractAlbumOrFail($this->albumId);
$this->flags->is_base_album = $this->album instanceof BaseAlbum;
$this->is_search_accessible = Auth::check() || Configs::getValueAsBool('search_public');
$this->is_search_accessible = $this->is_search_accessible && $this->album instanceof ModelsAlbum;
$this->flags->is_search_accessible = $this->flags->is_search_accessible && $this->album instanceof ModelsAlbum;
$this->flags->is_accessible = Gate::check(AlbumPolicy::CAN_ACCESS, [ModelsAlbum::class, $this->album]);

if (!$this->flags->is_accessible) {
Expand Down
2 changes: 2 additions & 0 deletions app/Livewire/DTO/AlbumFlags.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ public function __construct(
public bool $is_map_accessible = false,
public bool $is_base_album = false,
public bool $is_mod_frame_enabled = false,
public bool $is_search_accessible = false,
public string $album_thumb_css_aspect_ratio = '',
public string|null $cover_id = null,
) {
$this->is_map_accessible = Configs::getValueAsBool('map_display');
$this->is_map_accessible = $this->is_map_accessible && (Auth::check() || Configs::getValueAsBool('map_display_public'));
$this->is_mod_frame_enabled = Configs::getValueAsBool('mod_frame_enabled');
$this->album_thumb_css_aspect_ratio = Configs::getValueAsEnum('default_album_thumb_aspect_ratio', AspectRatioType::class)->css();
$this->is_search_accessible = Auth::check() || Configs::getValueAsBool('search_public');
}
}
2 changes: 1 addition & 1 deletion app/Models/Album.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public function newEloquentBuilder($query): AlbumBuilder
*/
protected function getAlbumThumbAspectRatioAttribute(): ?AspectRatioType
{
return AspectRatioType::tryFrom($this->attributes['album_thumb_aspect_ratio']);
return AspectRatioType::tryFrom($this->attributes['album_thumb_aspect_ratio'] ?? '1/1');
}

/**
Expand Down
3 changes: 0 additions & 3 deletions app/Models/Extensions/ToArrayThrowsNotImplemented.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ abstract protected function _toArray(): array;
*/
final public function toArray(): array
{
if (Route::is('livewire_index') || Route::is('livewire.message')) {
return $this->_toArray();
}
$details = Route::getCurrentRoute()?->getName() ?? '';
$details .= ($details !== '' ? ':' : '') . get_called_class();
throw new NotImplementedException($details . '->toArray() is deprecated, use Resources instead.');
Expand Down
1 change: 1 addition & 0 deletions app/Models/Photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ protected function getLivePhotoUrlAttribute(): ?string
$path = $this->live_photo_short_path;
$disk_name = $this->size_variants->getOriginal()?->storage_disk?->value ?? StorageDiskType::LOCAL->value;

/** @disregard P1013 */
return ($path === null || $path === '') ? null : Storage::disk($disk_name)->url($path);
}

Expand Down
5 changes: 5 additions & 0 deletions app/Models/SizeVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,11 @@ public function getUrlAttribute(): string
!Configs::getValueAsBool('SL_enable') ||
(!Configs::getValueAsBool('SL_for_admin') && Auth::user()?->may_administrate === true)
) {
/** @disregard P1013 */
return $imageDisk->url($this->short_path);
}

/** @disregard P1013 */
$storageAdapter = $imageDisk->getAdapter();
if ($storageAdapter instanceof AwsS3V3Adapter) {
return $this->getAwsUrl();
Expand All @@ -218,9 +220,11 @@ private function getAwsUrl(): string
// Return the public URL in case the S3 bucket is set to public, otherwise generate a temporary URL
$visibility = config('filesystems.disks.s3.visibility', 'private');
if ($visibility === 'public') {
/** @disregard P1013 */
return $imageDisk->url($this->short_path);
}

/** @disregard P1013 */
return $imageDisk->temporaryUrl($this->short_path, now()->addSeconds($maxLifetime));
}

Expand Down Expand Up @@ -260,6 +264,7 @@ private function getSymLinkUrl(): string
*/
public function getFullPathAttribute(): string
{
/** @disregard P1013 */
return Storage::disk($this->storage_disk->value)->path($this->short_path);
}

Expand Down
7 changes: 3 additions & 4 deletions app/View/Components/Gallery/Album/SharingLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\View\Components\Gallery\Album;

use App\Contracts\Models\AbstractAlbum;
use Illuminate\View\Component;
use Illuminate\View\View;

Expand All @@ -14,11 +13,11 @@ class SharingLinks extends Component
public string $url;
public string $rawUrl;

public function __construct(AbstractAlbum $album)
public function __construct(string $albumId, string $albumTitle)
{
$this->url = route('livewire-gallery-album', ['albumId' => $album->id]);
$this->url = route('livewire-gallery-album', ['albumId' => $albumId]);
$this->rawUrl = rawurlencode($this->url);
$raw_title = rawurlencode($album->title);
$raw_title = rawurlencode($albumTitle);
$this->twitter_link = 'https://twitter.com/share?url=' . $this->rawUrl;
$this->facebook_link = 'https://www.facebook.com/sharer.php?u=' . $this->rawUrl . '?t=' . $raw_title;
$this->mailTo_link = 'mailto:?subject=' . $raw_title . '&body=' . $this->rawUrl;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use App\Models\Extensions\BaseConfigMigration;

return new class() extends BaseConfigMigration {
public const GALLERY = 'Gallery';

public function getConfigs(): array
{
return [
[
'key' => 'login_required_root_only',
'value' => '1',
'is_secret' => false,
'cat' => self::GALLERY,
'type_range' => self::BOOL,
'description' => 'Require user to login only on root. A user with a direct link to an album can still access it.',
],
];
}
};
2 changes: 1 addition & 1 deletion public/vendor/log-viewer/app.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/vendor/log-viewer/app.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions public/vendor/log-viewer/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"/app.js": "/app.js?id=735ecf8ed11308c93eed8ab901094929",
"/app.css": "/app.css?id=4d9629388ead48c1e8115f0f9d44caaf",
"/app.js": "/app.js?id=035385b07dcacbe9110f48c4eecdea37",
"/app.css": "/app.css?id=66a27926e151e853bb42c8fe0f587a17",
"/img/log-viewer-128.png": "/img/log-viewer-128.png?id=d576c6d2e16074d3f064e60fe4f35166",
"/img/log-viewer-32.png": "/img/log-viewer-32.png?id=f8ec67d10f996aa8baf00df3b61eea6d",
"/img/log-viewer-64.png": "/img/log-viewer-64.png?id=8902d596fc883ca9eb8105bb683568c6"
Expand Down
4 changes: 2 additions & 2 deletions resources/views/components/gallery/view/photo.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ class="absolute top-0 h-1/4 w-full sm:w-1/2 left-1/2 -translate-x-1/2 opacity-50
</div>
<template x-if="photo.rights.can_edit">
<div class="h-full relative overflow-clip w-0 bg-bg-800 transition-all"
:class="photoFlags.isEditOpen ? 'w-full' : 'w-0 translate-x-full'">
x-bind:class="photoFlags.isEditOpen ? 'w-full' : 'w-0 translate-x-full'">
<x-gallery.photo.properties />
</div>
</template>
<aside id="lychee_sidebar_container" class="h-full relative transition-all overflow-x-clip overflow-y-scroll bg-bg-800"
:class="photoFlags.isDetailsOpen ? 'w-[360px]' : 'w-0 translate-x-full'">
x-bind:class="photoFlags.isDetailsOpen ? 'w-[360px]' : 'w-0 translate-x-full'">
<x-gallery.photo.sidebar />
</aside>
</div>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/components/header/actions-menus.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<x-header.button href="{{ route('livewire-frame', ['albumId' => $this->albumId]) }}" wire:navigate icon="monitor" />
@endif
{{-- No selection --}}
@can(App\Policies\AlbumPolicy::CAN_UPLOAD, [App\Contracts\Models\AbstractAlbum::class, $this->album ?? null])
@if($this->rights->can_upload)
<x-header.button x-show='select.selectedPhotos.length === 0 && select.selectedAlbums.length === 0 ' wire:click='openContextMenu' icon='plus' />
@endcan
@endif
@if($this->rights->can_edit)
{{-- Albums selection --}}
<x-header.button x-cloak x-show='select.selectedAlbums.length > 0' fill='fill-primary-400 hover:fill-primary-200' x-on:click='renameAlbums' icon='pencil' />
Expand Down
4 changes: 2 additions & 2 deletions resources/views/livewire/forms/album/set-header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class="my-[1px] text-text-main-0 rounded bg-black/30 inline-block text-2xs align
<div class=" translate-x-[8.75rem] z-50 absolute bg-bg-900 text-xs rounded"
x-show.transition.opacity="isSearchPhotoOpen">
<ul class="max-h-[50vh] overflow-y-auto">
@if(strlen( $search ) === 0)
@if(strlen( $search ?? '' ) === 0)
<li class="border-b border-bg-800 cursor-pointer transition-all ease-in-out duration-300
hover:bg-gradient-to-b hover:from-primary-500 hover:to-primary-600 hover:text-text-main-0"
wire:click="select('{{ \App\Livewire\Components\Forms\Album\SetHeader::COMPACT_HEADER }}','{{ __('lychee.SET_COMPACT_HEADER') }}')">
Expand All @@ -48,7 +48,7 @@ class="my-[1px] text-text-main-0 rounded bg-black/30 inline-block text-2xs align
</a>
</li>
@empty
@if(strlen( $search ) > 0)
@if(strlen( $search ?? '' ) > 0)
<li class="border-b border-bg-800 cursor-pointer transition-all ease-in-out duration-300
hover:bg-gradient-to-b hover:from-primary-500 hover:to-primary-600 hover:text-text-main-0">
<a class="px-3 py-1 flex items-center">No results for "{{ $search }}"</a>
Expand Down
22 changes: 11 additions & 11 deletions resources/views/livewire/pages/gallery/album.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
@entangle('sessionFlags.is_fullscreen'),
@entangle('sessionFlags.are_photo_details_open'),
@js($this->rights),
@js($album->id),
@js($this->album->id),
@js($this->albumIDs),
@js($this->photosResource),
@js($photoId),
@js($this->photoId),
@js($this->overlayType),
@js($this->layouts)
)" @keydown.window="handleKeydown(event)" @popstate.window="handlePopState(event)">
<!-- toolbar -->
<x-header.bar class="opacity-0" x-bind:class="isFullscreen ? 'opacity-0 h-0' : 'opacity-100 h-14'">
<x-header.back wire:navigate href="{{ $this->back }}" />
<x-header.title>{{ $this->title }}</x-header.title>
@if($is_search_accessible)
@if($this->flags->is_search_accessible)
<x-header.button class="flex flex-grow justify-end" href="{{ route('livewire-search', ['albumId' => $this->album->id]) }}" wire:navigate icon="magnifying-glass" />
@endif
<x-header.actions-menus />
Expand All @@ -26,9 +26,9 @@
x-show="!albumFlags.isDetailsOpen" />
@endif
</x-header.bar>
@if ($flags->is_password_protected)
@if ($this->flags->is_password_protected)
<livewire:forms.album.unlock-album :albumID="$albumId" :back="$this->back" />
@elseif(!$flags->is_accessible)
@elseif(!$this->flags->is_accessible)
<x-gallery.album.login-dialog />
@else
<div id="lychee_view_content"
Expand All @@ -37,26 +37,26 @@ class="relative flex flex-wrap content-start w-full justify-start overflow-y-aut
@if ($this->rights->can_edit)
<x-gallery.album.menu.menu />
@endif
@if ($this->albumFormatted !== null && ($num_albums > 0 || $num_photos > 0))
@if ($this->albumFormatted !== null && ($this->num_albums > 0 || $this->num_photos > 0))
<x-gallery.album.hero x-show="! albumFlags.isDetailsOpen" />
<x-gallery.album.details x-show="! albumFlags.isDetailsOpen" />
@endif
@if ($num_albums === 0 && $num_photos === 0)
@if ($this->num_albums === 0 && $this->num_photos === 0)
<div class="flex w-full h-full items-center justify-center text-xl text-text-main-400">
<span class="block">
{{ $this->noImagesAlbumsMessage }}
</span>
</div>
@endif
@if ($num_albums > 0 && $num_photos > 0)
@if ($this->num_albums > 0 && $this->num_photos > 0)
<x-gallery.divider title="{{ __('lychee.ALBUMS') }}" />
@endif
@if ($num_albums > 0)
@if ($this->num_albums > 0)
@foreach ($this->albums as $data)
<x-gallery.album.thumbs.album :data="$data" :str-aspect-ratio-class="$this->flags->album_thumb_css_aspect_ratio" :cover-id="$this->flags->cover_id" />
@endforeach
@endif
@if ($num_albums > 0 && $num_photos > 0)
@if ($this->num_albums > 0 && $this->num_photos > 0)
<x-gallery.divider title="{{ __('lychee.PHOTOS') }}" />
@endif
<x-gallery.view.photo-listing />
Expand All @@ -65,7 +65,7 @@ class="relative flex flex-wrap content-start w-full justify-start overflow-y-aut
@endif
</div>
<x-gallery.view.photo />
<x-gallery.album.sharing-links :album="$this->album" x-show="albumFlags.isSharingLinksOpen" />
<x-gallery.album.sharing-links :album-id="$this->album->id" :album-title="$this->album->title" x-show="albumFlags.isSharingLinksOpen" />
@endif
@auth
<livewire:modules.jobs.feedback />
Expand Down
Empty file.
31 changes: 31 additions & 0 deletions resources/views/vendor/log-viewer/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}" class="h-full">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="csrf-token" content="{{ csrf_token() }}">
<link rel="shortcut icon" href="{{ asset(mix('img/log-viewer-32.png', 'vendor/log-viewer')) }}">

<title>Log Viewer{{ config('app.name') ? ' - ' . config('app.name') : '' }}</title>

<!-- Style sheets-->
<link href="{{ asset(mix('app.css', 'vendor/log-viewer')) }}" rel="stylesheet" onerror="alert('app.css failed to load. Please refresh the page, re-publish Log Viewer assets, or fix routing for vendor assets.')">
</head>

<body class="h-full px-3 lg:px-5 bg-gray-100 dark:bg-gray-900">
<div id="log-viewer" class="flex h-full max-h-screen max-w-full">
<router-view></router-view>
</div>

<!-- Global LogViewer Object -->
<script>
window.LogViewer = @json($logViewerScriptVariables);
// Add additional headers for LogViewer requests like so:
// window.LogViewer.headers['Authorization'] = 'Bearer xxxxxxx';
</script>
<script src="{{ asset(mix('app.js', 'vendor/log-viewer')) }}" onerror="alert('app.js failed to load. Please refresh the page, re-publish Log Viewer assets, or fix routing for vendor assets.')"></script>
</body>
</html>
Loading

0 comments on commit d347d42

Please sign in to comment.