Skip to content

Commit

Permalink
License as enum type (#2025)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Sep 23, 2023
1 parent 5f00cbb commit 4c4745f
Show file tree
Hide file tree
Showing 24 changed files with 218 additions and 124 deletions.
2 changes: 1 addition & 1 deletion app/Actions/Diagnostics/Pipes/Checks/ImageOptCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function handle(array &$data, \Closure $next): array
$binaryPath = config('image-optimizer.binary_path');

if ($binaryPath !== '' && substr($binaryPath, -1) !== DIRECTORY_SEPARATOR) {
$binaryPath = $binaryPath . DIRECTORY_SEPARATOR;
$binaryPath .= DIRECTORY_SEPARATOR;
}

if (Helpers::isExecAvailable()) {
Expand Down
42 changes: 0 additions & 42 deletions app/Assets/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,48 +153,6 @@ public function gcd(int $a, int $b): int
return ($a % $b) !== 0 ? $this->gcd($b, $a % $b) : $b;
}

/**
* Returns the available licenses.
*/
public function get_all_licenses(): array
{
return [
'none',
'reserved',
'CC0',
'CC-BY-1.0',
'CC-BY-2.0',
'CC-BY-2.5',
'CC-BY-3.0',
'CC-BY-4.0',
'CC-BY-NC-1.0',
'CC-BY-NC-2.0',
'CC-BY-NC-2.5',
'CC-BY-NC-3.0',
'CC-BY-NC-4.0',
'CC-BY-NC-ND-1.0',
'CC-BY-NC-ND-2.0',
'CC-BY-NC-ND-2.5',
'CC-BY-NC-ND-3.0',
'CC-BY-NC-ND-4.0',
'CC-BY-NC-SA-1.0',
'CC-BY-NC-SA-2.0',
'CC-BY-NC-SA-2.5',
'CC-BY-NC-SA-3.0',
'CC-BY-NC-SA-4.0',
'CC-BY-ND-1.0',
'CC-BY-ND-2.0',
'CC-BY-ND-2.5',
'CC-BY-ND-3.0',
'CC-BY-ND-4.0',
'CC-BY-SA-1.0',
'CC-BY-SA-2.0',
'CC-BY-SA-2.5',
'CC-BY-SA-3.0',
'CC-BY-SA-4.0',
];
}

/**
* Return incrementing numbers.
*/
Expand Down
6 changes: 4 additions & 2 deletions app/Contracts/Http/Requests/HasLicense.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace App\Contracts\Http\Requests;

use App\Enum\LicenseType;

interface HasLicense
{
/**
* @return string|null
* @return LicenseType|null
*/
public function license(): ?string;
public function license(): ?LicenseType;
}
101 changes: 101 additions & 0 deletions app/Enum/LicenseType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace App\Enum;

use App\Enum\Traits\DecorateBackedEnum;

/**
* Defines the possible licenses available by Lychee.
*/
enum LicenseType: string
{
use DecorateBackedEnum;

case NONE = 'none';
case RESERVED = 'reserved';
case CC0 = 'CC0';
case CC_BY_1_0 = 'CC-BY-1.0';
case CC_BY_2_0 = 'CC-BY-2.0';
case CC_BY_2_5 = 'CC-BY-2.5';
case CC_BY_3_0 = 'CC-BY-3.0';
case CC_BY_4_0 = 'CC-BY-4.0';
case CC_BY_ND_1_0 = 'CC-BY-ND-1.0';
case CC_BY_ND_2_0 = 'CC-BY-ND-2.0';
case CC_BY_ND_2_5 = 'CC-BY-ND-2.5';
case CC_BY_ND_3_0 = 'CC-BY-ND-3.0';
case CC_BY_ND_4_0 = 'CC-BY-ND-4.0';
case CC_BY_SA_1_0 = 'CC-BY-SA-1.0';
case CC_BY_SA_2_0 = 'CC-BY-SA-2.0';
case CC_BY_SA_2_5 = 'CC-BY-SA-2.5';
case CC_BY_SA_3_0 = 'CC-BY-SA-3.0';
case CC_BY_SA_4_0 = 'CC-BY-SA-4.0';
case CC_BY_NC_1_0 = 'CC-BY-NC-1.0';
case CC_BY_NC_2_0 = 'CC-BY-NC-2.0';
case CC_BY_NC_2_5 = 'CC-BY-NC-2.5';
case CC_BY_NC_3_0 = 'CC-BY-NC-3.0';
case CC_BY_NC_4_0 = 'CC-BY-NC-4.0';
case CC_BY_NC_ND_1_0 = 'CC-BY-NC-ND-1.0';
case CC_BY_NC_ND_2_0 = 'CC-BY-NC-ND-2.0';
case CC_BY_NC_ND_2_5 = 'CC-BY-NC-ND-2.5';
case CC_BY_NC_ND_3_0 = 'CC-BY-NC-ND-3.0';
case CC_BY_NC_ND_4_0 = 'CC-BY-NC-ND-4.0';
case CC_BY_NC_SA_1_0 = 'CC-BY-NC-SA-1.0';
case CC_BY_NC_SA_2_0 = 'CC-BY-NC-SA-2.0';
case CC_BY_NC_SA_2_5 = 'CC-BY-NC-SA-2.5';
case CC_BY_NC_SA_3_0 = 'CC-BY-NC-SA-3.0';
case CC_BY_NC_SA_4_0 = 'CC-BY-NC-SA-4.0';

/**
* Given return the array of localized name.
*
* @return array
*/
public static function localized(): array
{
return [
self::NONE->value => 'None',
self::RESERVED->value => __('lychee.ALBUM_RESERVED'),
self::CC0->value => 'CC0 - Public Domain',
self::CC_BY_1_0->value => 'CC Attribution 1.0',
self::CC_BY_2_0->value => 'CC Attribution 2.0',
self::CC_BY_2_5->value => 'CC Attribution 2.5',
self::CC_BY_3_0->value => 'CC Attribution 3.0',
self::CC_BY_4_0->value => 'CC Attribution 4.0',
self::CC_BY_ND_1_0->value => 'CC Attribution-NoDerivatives 1.0',
self::CC_BY_ND_2_0->value => 'CC Attribution-NoDerivatives 2.0',
self::CC_BY_ND_2_5->value => 'CC Attribution-NoDerivatives 2.5',
self::CC_BY_ND_3_0->value => 'CC Attribution-NoDerivatives 3.0',
self::CC_BY_ND_4_0->value => 'CC Attribution-NoDerivatives 4.0',
self::CC_BY_SA_1_0->value => 'CC Attribution-ShareAlike 1.0',
self::CC_BY_SA_2_0->value => 'CC Attribution-ShareAlike 2.0',
self::CC_BY_SA_2_5->value => 'CC Attribution-ShareAlike 2.5',
self::CC_BY_SA_3_0->value => 'CC Attribution-ShareAlike 3.0',
self::CC_BY_SA_4_0->value => 'CC Attribution-ShareAlike 4.0',
self::CC_BY_NC_1_0->value => 'CC Attribution-NonCommercial 1.0',
self::CC_BY_NC_2_0->value => 'CC Attribution-NonCommercial 2.0',
self::CC_BY_NC_2_5->value => 'CC Attribution-NonCommercial 2.5',
self::CC_BY_NC_3_0->value => 'CC Attribution-NonCommercial 3.0',
self::CC_BY_NC_4_0->value => 'CC Attribution-NonCommercial 4.0',
self::CC_BY_NC_ND_1_0->value => 'CC Attribution-NonCommercial-NoDerivatives 1.0',
self::CC_BY_NC_ND_2_0->value => 'CC Attribution-NonCommercial-NoDerivatives 2.0',
self::CC_BY_NC_ND_2_5->value => 'CC Attribution-NonCommercial-NoDerivatives 2.5',
self::CC_BY_NC_ND_3_0->value => 'CC Attribution-NonCommercial-NoDerivatives 3.0',
self::CC_BY_NC_ND_4_0->value => 'CC Attribution-NonCommercial-NoDerivatives 4.0',
self::CC_BY_NC_SA_1_0->value => 'CC Attribution-NonCommercial-ShareAlike 1.0',
self::CC_BY_NC_SA_2_0->value => 'CC Attribution-NonCommercial-ShareAlike 2.0',
self::CC_BY_NC_SA_2_5->value => 'CC Attribution-NonCommercial-ShareAlike 2.5',
self::CC_BY_NC_SA_3_0->value => 'CC Attribution-NonCommercial-ShareAlike 3.0',
self::CC_BY_NC_SA_4_0->value => 'CC Attribution-NonCommercial-ShareAlike 4.0',
];
}

/**
* Return the localization string of current.
*
* @return string
*/
public function localization(): string
{
return self::localized()[$this->value];
}
}
2 changes: 1 addition & 1 deletion app/Enum/SizeVariantType.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function name(): string
*
* @return string
*/
public function localized(): string
public function localization(): string
{
return match ($this) {
self::THUMB => __('lychee.PHOTO_THUMB'),
Expand Down
1 change: 0 additions & 1 deletion app/Facades/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* @method static int data_index()
* @method static int data_index_r()
* @method static void data_index_set(int $idx = 0)
* @method static array get_all_licenses()
* @method static bool isExecAvailable()
* @method static string secondsToHMS(int|float $d)
*/
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Requests/Album/SetAlbumLicenseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Contracts\Http\Requests\HasAlbum;
use App\Contracts\Http\Requests\HasLicense;
use App\Contracts\Http\Requests\RequestAttribute;
use App\Enum\LicenseType;
use App\Http\Requests\BaseApiRequest;
use App\Http\Requests\Traits\Authorize\AuthorizeCanEditAlbumTrait;
use App\Http\Requests\Traits\HasAlbumTrait;
Expand Down Expand Up @@ -32,6 +33,6 @@ public function rules(): array
protected function processValidatedValues(array $values, array $files): void
{
$this->album = Album::query()->findOrFail($values[RequestAttribute::ALBUM_ID_ATTRIBUTE]);
$this->license = $values[RequestAttribute::LICENSE_ATTRIBUTE];
$this->license = LicenseType::tryFrom($values[RequestAttribute::LICENSE_ATTRIBUTE]);
}
}
3 changes: 2 additions & 1 deletion app/Http/Requests/Photo/SetPhotoLicenseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Contracts\Http\Requests\HasLicense;
use App\Contracts\Http\Requests\HasPhoto;
use App\Contracts\Http\Requests\RequestAttribute;
use App\Enum\LicenseType;
use App\Http\Requests\BaseApiRequest;
use App\Http\Requests\Traits\Authorize\AuthorizeCanEditPhotoTrait;
use App\Http\Requests\Traits\HasLicenseTrait;
Expand Down Expand Up @@ -34,6 +35,6 @@ protected function processValidatedValues(array $values, array $files): void
/** @var ?string $photoID */
$photoID = $values[RequestAttribute::PHOTO_ID_ATTRIBUTE];
$this->photo = Photo::query()->findOrFail($photoID);
$this->license = $values[RequestAttribute::LICENSE_ATTRIBUTE];
$this->license = LicenseType::tryFrom($values[RequestAttribute::LICENSE_ATTRIBUTE]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

namespace App\Http\Requests\Settings;

use App\Rules\LicenseRule;
use App\Enum\LicenseType;
use Illuminate\Validation\Rules\Enum;

class SetDefaultLicenseSettingRequest extends AbstractSettingRequest
{
public function rules(): array
{
return ['license' => ['required', new LicenseRule()]];
return ['license' => ['required', new Enum(LicenseType::class)]];
}

protected function processValidatedValues(array $values, array $files): void
{
$this->name = 'default_license';
$this->value = $values['license'];
$this->value = LicenseType::from($values['license']);
}
}
8 changes: 5 additions & 3 deletions app/Http/Requests/Traits/HasLicenseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace App\Http\Requests\Traits;

use App\Enum\LicenseType;

trait HasLicenseTrait
{
protected string $license = 'none';
protected LicenseType $license = LicenseType::NONE;

/**
* @return string
* @return LicenseType
*/
public function license(): string
public function license(): LicenseType
{
return $this->license;
}
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Resources/ConfigurationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Enum\AlbumLayoutType;
use App\Enum\DefaultAlbumProtectionType;
use App\Enum\ImageOverlayType;
use App\Enum\LicenseType;
use App\Enum\ThumbAlbumSubtitleType;
use App\Exceptions\Handler;
use App\Metadata\Versions\InstalledVersion;
Expand Down Expand Up @@ -84,7 +85,7 @@ public function toArray($request): array
'allow_online_git_pull' => Configs::getValueAsBool('allow_online_git_pull'),
'apply_composer_update' => Configs::getValueAsBool('apply_composer_update'),
'compression_quality' => Configs::getValueAsInt('compression_quality'),
'default_license' => Configs::getValueAsString('default_license'),
'default_license' => Configs::getValueAsEnum('default_license', LicenseType::class),
'delete_imported' => Configs::getValueAsBool('delete_imported'),
'dropbox_key' => Configs::getValueAsString('dropbox_key'),
'editor_enabled' => Configs::getValueAsBool('editor_enabled'),
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/Models/AlbumResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function toArray($request)
// attributes
'description' => $this->resource->description,
'track_url' => $this->resource->track_url,
'license' => $this->resource->license,
'license' => $this->resource->license->localization(),
'sorting' => $this->resource->sorting,

// children
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/Models/PhotoResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function toArray($request)
'iso' => $this->resource->iso,
'latitude' => $this->resource->latitude,
'lens' => $this->resource->lens,
'license' => $this->resource->license,
'license' => $this->resource->license->localization(),
'live_photo_checksum' => $this->resource->live_photo_checksum,
'live_photo_content_id' => $this->resource->live_photo_content_id,
'live_photo_url' => $this->resource->live_photo_url,
Expand Down
5 changes: 3 additions & 2 deletions app/Http/RuleSets/Album/SetAlbumLicenseRuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use App\Contracts\Http\Requests\RequestAttribute;
use App\Contracts\Http\RuleSet;
use App\Rules\LicenseRule;
use App\Enum\LicenseType;
use App\Rules\RandomIDRule;
use Illuminate\Validation\Rules\Enum;

/**
* Rules applied when changing the license of an album.
Expand All @@ -19,7 +20,7 @@ public static function rules(): array
{
return [
RequestAttribute::ALBUM_ID_ATTRIBUTE => ['required', new RandomIDRule(false)],
RequestAttribute::LICENSE_ATTRIBUTE => ['required', new LicenseRule()],
RequestAttribute::LICENSE_ATTRIBUTE => ['required', new Enum(LicenseType::class)],
];
}
}
5 changes: 3 additions & 2 deletions app/Http/RuleSets/Photo/SetPhotoLicenseRuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use App\Contracts\Http\Requests\RequestAttribute;
use App\Contracts\Http\RuleSet;
use App\Rules\LicenseRule;
use App\Enum\LicenseType;
use App\Rules\RandomIDRule;
use Illuminate\Validation\Rules\Enum;

/**
* Rule applied when changing the license of a photo.
Expand All @@ -19,7 +20,7 @@ public static function rules(): array
{
return [
RequestAttribute::PHOTO_ID_ATTRIBUTE => ['required', new RandomIDRule(false)],
RequestAttribute::LICENSE_ATTRIBUTE => ['required', new LicenseRule()],
RequestAttribute::LICENSE_ATTRIBUTE => ['required', new Enum(LicenseType::class)],
];
}
}
2 changes: 1 addition & 1 deletion app/Metadata/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public static function createFromFile(NativeLocalFile $file, int $fileLastModifi
if ($metadata->shutter !== null && $metadata->shutter !== '') {
// TODO: If we add the suffix " s" here, we should also normalize the fraction here.
// It does not make any sense to strip-off the suffix again in Photo and re-add it again.
$metadata->shutter = $metadata->shutter . self::SUFFIX_SEC_UNIT;
$metadata->shutter .= self::SUFFIX_SEC_UNIT;
}

// Decode location data, it can be longer than is acceptable for DB that's the reason for substr
Expand Down
Loading

0 comments on commit 4c4745f

Please sign in to comment.