Skip to content

Commit

Permalink
sqash
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria committed Jan 16, 2023
1 parent 1dd61c1 commit 77452e8
Show file tree
Hide file tree
Showing 124 changed files with 7,840 additions and 7,937 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ APP_FORCE_HTTPS=false

# enable or disable debug bar. By default it is disabled.
DEBUGBAR_ENABLED=false
LIVEWIRE_ENABLED=false

##############################################################################
# IMPORTANT: To migrate from Lychee v3 you *MUST* use the same MySQL/MariaDB #
Expand Down
25 changes: 25 additions & 0 deletions app/Actions/User/TokenDisable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Actions\User;

use App\Exceptions\InvalidPropertyException;
use App\Exceptions\ModelDBException;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

class TokenDisable
{
/**
* @throws InvalidPropertyException
* @throws ModelDBException
*/
public function do(): User
{
/** @var User $user */
$user = Auth::user();
$user->token = null;
$user->save();

return $user;
}
}
26 changes: 26 additions & 0 deletions app/Actions/User/TokenReset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Actions\User;

use App\Exceptions\InvalidPropertyException;
use App\Exceptions\ModelDBException;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

class TokenReset
{
/**
* @throws InvalidPropertyException
* @throws ModelDBException
*/
public function do(): User
{
/** @var User $user */
$user = Auth::user();
$token = strtr(base64_encode(random_bytes(16)), '+/', '-_');
$user->token = hash('SHA512', $token);
$user->save();

return $user;
}
}
22 changes: 22 additions & 0 deletions app/Assets/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,28 @@ public function data_index_set(int $idx = 0): void
$this->numTab = $idx;
}

/**
* From https://www.php.net/manual/en/function.disk-total-space.php.
*
* @param float $bytes
*
* @return string
*/
public function getSymbolByQuantity(float $bytes): string
{
$symbols = [
'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB',
];
$exp = intval(floor(log($bytes) / log(1024.0)));

if ($exp >= sizeof($symbols)) {
// if the number is too large, we fall back to the largest available symbol
$exp = sizeof($symbols) - 1;
}

return sprintf('%.2f %s', ($bytes / pow(1024, $exp)), $symbols[$exp]);
}

/**
* Check if the `exec` function is available.
*
Expand Down
20 changes: 20 additions & 0 deletions app/Contracts/Http/RuleSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Contracts\Http;

/**
* In order to avoid code duplication, we centralize the rule sets
* used during the validation of requests as they are used both
* in Livewire and in the Requests class.
*/
interface RuleSet
{
/**
* Return an array containing the rules to be applied to the request attributes.
*
* @return array
*/
public static function rules(): array;

// TODO: Associate error message to above rules.
}
3 changes: 2 additions & 1 deletion app/Contracts/Models/AbstractAlbum.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Relation;
use Livewire\Wireable;

/**
* Interface AbsractAlbum.
Expand All @@ -30,7 +31,7 @@
* @property bool $grants_download
* @property bool $grants_full_photo_access
*/
interface AbstractAlbum extends \JsonSerializable, Arrayable, Jsonable
interface AbstractAlbum extends \JsonSerializable, Arrayable, Jsonable, Wireable
{
/**
* @return Relation|Builder
Expand Down
3 changes: 1 addition & 2 deletions app/DTO/AlbumProtectionPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\DTO;

use App\Models\Album;
use App\Models\BaseAlbumImpl;
use App\Models\Configs;
use App\Models\Extensions\BaseAlbum;
Expand Down Expand Up @@ -56,7 +55,7 @@ public static function ofBaseAlbumImplementation(BaseAlbumImpl $baseAlbum): self
/**
* Given a {@link BaseAlbum}, returns the Protection Policy associated to it.
*
* @param Album $baseAlbum
* @param BaseAlbum $baseAlbum
*
* @return AlbumProtectionPolicy
*/
Expand Down
26 changes: 26 additions & 0 deletions app/Enum/Livewire/AlbumMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Enum\Livewire;

use App\Enum\Traits\WireableEnumTrait;
use Illuminate\Support\Str;
use Livewire\Wireable;

enum AlbumMode: int implements Wireable
{
use WireableEnumTrait;

case FLKR = 0;
case MASONRY = 1;
case SQUARE = 2;

/**
* get the name as a string instead of the value.
*
* @return string
*/
public function toCss(): string
{
return Str::lower($this->name);
}
}
16 changes: 16 additions & 0 deletions app/Enum/Livewire/GalleryMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Enum\Livewire;

use App\Enum\Traits\WireableEnumTrait;
use Livewire\Wireable;

enum GalleryMode: string implements Wireable
{
use WireableEnumTrait;

case ALBUM = 'album';
case ALBUMS = 'albums';
case PHOTO = 'photo';
case MAP = 'map';
}
17 changes: 17 additions & 0 deletions app/Enum/Livewire/PageMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Enum\Livewire;

use App\Enum\Traits\WireableEnumTrait;
use Livewire\Wireable;

enum PageMode: string implements Wireable
{
use WireableEnumTrait;

case GALLERY = 'gallery';
case MAP = 'map';
case SETTINGS = 'settings';
case LOGS = 'logs';
case DIAGNOSTICS = 'diagnostics';
}
42 changes: 42 additions & 0 deletions app/Enum/Livewire/PhotoOverlayMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Enum\Livewire;

use App\Enum\Traits\WireableEnumTrait;
use Livewire\Wireable;

enum PhotoOverlayMode: string implements Wireable
{
use WireableEnumTrait;

case NONE = 'none';
case DESC = 'desc';
case EXIF = 'exif';
case DATE = 'date';

/**
* Iterate to the next OverlayMode.
*
* @return PhotoOverlayMode
*/
public function next(): PhotoOverlayMode
{
return match ($this) {
self::NONE => self::DESC,
self::DESC => self::DATE,
self::DATE => self::EXIF,
self::EXIF => self::NONE,
default => self::NONE
};
}

/**
* Number of valid values.
*
* @return int
*/
public static function count(): int
{
return 4;
}
}
35 changes: 35 additions & 0 deletions app/Enum/Traits/WireableEnumTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Enum\Traits;

use App\Exceptions\Internal\LycheeLogicException;
use Closure;

trait WireableEnumTrait
{
public function toLivewire(): string|int
{
return $this->value;
}

public static function fromLivewire(mixed $value): self
{
if (!is_string($value) && !is_int($value)) {
throw new LycheeLogicException('Enum could not be instanciated from ' . strval($value), null);
}

return self::from($value);
}

/**
* @return string[]|int[]|\Closure
*
* @psalm-return array<string, string|int> | Closure(string):(int|string)
*/
protected static function values()
{
return function (string $name): string {
return mb_strtolower($name);
};
}
}
20 changes: 7 additions & 13 deletions app/Http/Controllers/Administration/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Http\Controllers\Administration;

use App\Actions\Settings\UpdateLogin;
use App\Actions\User\TokenDisable;
use App\Actions\User\TokenReset;
use App\Contracts\Exceptions\InternalLycheeException;
use App\Exceptions\Internal\FrameworkException;
use App\Exceptions\ModelDBException;
Expand Down Expand Up @@ -32,7 +34,6 @@ public function updateLogin(ChangeLoginRequest $request, UpdateLogin $updateLogi
$request->oldPassword(),
$request->ip()
);

// Update the session with the new credentials of the user.
// Otherwise, the session is out-of-sync and falsely assumes the user
// to be unauthenticated upon the next request.
Expand Down Expand Up @@ -94,15 +95,11 @@ public function getAuthenticatedUser(): ?User
* @throws ModelDBException
* @throws \Exception
*/
public function resetToken(ChangeTokenRequest $request): array
public function resetToken(ChangeTokenRequest $request, TokenReset $tokenReset): array
{
/** @var User $user */
$user = Auth::user();
$token = strtr(base64_encode(random_bytes(16)), '+/', '-_');
$user->token = hash('SHA512', $token);
$user->save();
$user = $tokenReset->do();

return ['token' => $token];
return ['token' => $user->token];
}

/**
Expand All @@ -113,11 +110,8 @@ public function resetToken(ChangeTokenRequest $request): array
* @throws UnauthenticatedException
* @throws ModelDBException
*/
public function unsetToken(ChangeTokenRequest $request): void
public function unsetToken(ChangeTokenRequest $request, TokenDisable $tokenDisable): void
{
/** @var User $user */
$user = Auth::user();
$user->token = null;
$user->save();
$tokenDisable->do();
}
}
6 changes: 3 additions & 3 deletions app/Http/Controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function show(): View
'infos' => $infos,
'page_config' => $page_config,
'rss_enable' => $rss_enable,
'user_css_url' => $this->getUserCss(),
'user_css_url' => self::getUserCss(),
]);
}

Expand Down Expand Up @@ -190,7 +190,7 @@ protected function frontend(?string $title = null, ?string $description = null,
'pageUrl' => url()->current(),
'rssEnable' => Configs::getValueAsBool('rss_enable'),
'bodyHtml' => file_get_contents(public_path('dist/frontend.html')),
'userCssUrl' => $this->getUserCss(),
'userCssUrl' => self::getUserCss(),
]);
} catch (BindingResolutionException $e) {
throw new FrameworkException('Laravel\'s container component', $e);
Expand All @@ -202,7 +202,7 @@ protected function frontend(?string $title = null, ?string $description = null,
*
* @return string
*/
private function getUserCss(): string
public static function getUserCss(): string
{
$cssCacheBusting = '';
if (Storage::disk('dist')->fileExists('user.css')) {
Expand Down
Loading

0 comments on commit 77452e8

Please sign in to comment.