Skip to content

Commit

Permalink
Allow to edit taken_at date (#2954)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Jan 28, 2025
1 parent 0bc5ab6 commit 5f4913c
Show file tree
Hide file tree
Showing 39 changed files with 434 additions and 30 deletions.
1 change: 1 addition & 0 deletions app/Actions/Photo/Pipes/Shared/HydrateMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function handle(DuplicateDTO|StandaloneDTO $state, \Closure $next): Dupli
}
if ($state->photo->taken_at === null) {
$state->photo->taken_at = $state->exifInfo->taken_at;
$state->photo->initial_taken_at = $state->exifInfo->taken_at;
}
if ($state->photo->latitude === null) {
$state->photo->latitude = $state->exifInfo->latitude;
Expand Down
19 changes: 19 additions & 0 deletions app/Contracts/Http/Requests/HasTakenAt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Contracts\Http\Requests;

use Illuminate\Support\Carbon;

interface HasTakenAt
{
/**
* @return Carbon|null
*/
public function takenAt(): ?Carbon;
}
1 change: 1 addition & 0 deletions app/Contracts/Http/Requests/RequestAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class RequestAttribute

public const TITLE_ATTRIBUTE = 'title';
public const UPLOAD_DATE_ATTRIBUTE = 'upload_date';
public const TAKEN_DATE_ATTRIBUTE = 'taken_at';
public const DESCRIPTION_ATTRIBUTE = 'description';
public const LICENSE_ATTRIBUTE = 'license';
public const ASPECT_RATIO_ATTRIBUTE = 'aspect_ratio';
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/Gallery/PhotoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ public function update(EditPhotoRequest $request): PhotoResource
$photo->created_at = $request->uploadDate();
$photo->tags = $request->tags();
$photo->license = $request->license()->value;

// if the request takenAt is null, then we set the initial value back.
$photo->taken_at = $request->takenAt() ?? $photo->initial_taken_at;

$photo->save();

return PhotoResource::fromModel($photo);
Expand Down
11 changes: 10 additions & 1 deletion app/Http/Requests/Photo/EditPhotoRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Contracts\Http\Requests\HasLicense;
use App\Contracts\Http\Requests\HasPhoto;
use App\Contracts\Http\Requests\HasTags;
use App\Contracts\Http\Requests\HasTakenAt;
use App\Contracts\Http\Requests\HasTitle;
use App\Contracts\Http\Requests\HasUploadDate;
use App\Contracts\Http\Requests\RequestAttribute;
Expand All @@ -21,6 +22,7 @@
use App\Http\Requests\Traits\HasLicenseTrait;
use App\Http\Requests\Traits\HasPhotoTrait;
use App\Http\Requests\Traits\HasTagsTrait;
use App\Http\Requests\Traits\HasTakenAtDateTrait;
use App\Http\Requests\Traits\HasTitleTrait;
use App\Http\Requests\Traits\HasUploadDateTrait;
use App\Models\Photo;
Expand All @@ -32,14 +34,15 @@
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rules\Enum;

class EditPhotoRequest extends BaseApiRequest implements HasPhoto, HasTags, HasUploadDate, HasDescription, HasLicense, HasTitle
class EditPhotoRequest extends BaseApiRequest implements HasPhoto, HasTags, HasUploadDate, HasDescription, HasLicense, HasTitle, HasTakenAt
{
use HasPhotoTrait;
use HasTitleTrait;
use HasDescriptionTrait;
use HasTagsTrait;
use HasUploadDateTrait;
use HasLicenseTrait;
use HasTakenAtDateTrait;

/**
* {@inheritDoc}
Expand All @@ -62,6 +65,7 @@ public function rules(): array
RequestAttribute::TAGS_ATTRIBUTE . '.*' => ['required', 'string', 'min:1'],
RequestAttribute::LICENSE_ATTRIBUTE => ['required', new Enum(LicenseType::class)],
RequestAttribute::UPLOAD_DATE_ATTRIBUTE => ['required', 'date'],
RequestAttribute::TAKEN_DATE_ATTRIBUTE => ['nullable', 'date'],
];
}

Expand All @@ -82,5 +86,10 @@ protected function processValidatedValues(array $values, array $files): void
$this->tags = $values[RequestAttribute::TAGS_ATTRIBUTE];
$this->upload_date = Carbon::parse($values[RequestAttribute::UPLOAD_DATE_ATTRIBUTE]);
$this->license = LicenseType::tryFrom($values[RequestAttribute::LICENSE_ATTRIBUTE]);

// We only set this one if it is not null
if (isset($values[RequestAttribute::TAKEN_DATE_ATTRIBUTE])) {
$this->taken_at = Carbon::parse($values[RequestAttribute::TAKEN_DATE_ATTRIBUTE]);
}
}
}
24 changes: 24 additions & 0 deletions app/Http/Requests/Traits/HasTakenAtDateTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Http\Requests\Traits;

use Illuminate\Support\Carbon;

trait HasTakenAtDateTrait
{
protected ?Carbon $taken_at = null;

/**
* @return Carbon|null
*/
public function takenAt(): ?Carbon
{
return $this->taken_at;
}
}
5 changes: 5 additions & 0 deletions app/Http/Resources/Models/Utils/PreComputedPhotoData.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class PreComputedPhotoData extends Data
public bool $is_camera_date;
public bool $has_exif;
public bool $has_location;
public bool $is_taken_at_modified;

public function __construct(Photo $photo)
{
Expand All @@ -30,6 +31,10 @@ public function __construct(Photo $photo)
$this->is_camera_date = $photo->taken_at !== null;
$this->has_exif = $this->genExifHash($photo) !== '';
$this->has_location = $this->has_location($photo);
// if taken_at is null, it is for sure not modified.
// if taken_at is not null, then it is modified if initial_taken_at is null or if taken_at is different from initial_taken_at.
// dd($photo->taken_at, $photo->initial_taken_at, $photo->taken_at->notEqualTo($photo->initial_taken_at));
$this->is_taken_at_modified = $photo->taken_at !== null && ($photo->initial_taken_at === null || $photo->taken_at->notEqualTo($photo->initial_taken_at));
}

private function has_location(Photo $photo): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ protected function hydrateMetadata(): void
}
if ($this->photo->taken_at === null) {
$this->photo->taken_at = $this->parameters->exifInfo->taken_at;
$this->photo->initial_taken_at = $this->parameters->exifInfo->taken_at;
}
if ($this->photo->latitude === null) {
$this->photo->latitude = $this->parameters->exifInfo->latitude;
Expand Down
4 changes: 4 additions & 0 deletions app/Models/Photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
* @property string|null $location
* @property Carbon|null $taken_at
* @property string|null $taken_at_orig_tz
* @property Carbon|null $initial_taken_at
* @property string|null $initial_taken_at_orig_tz
* @property bool $is_starred
* @property string|null $live_photo_short_path
* @property string|null $live_photo_url
Expand Down Expand Up @@ -153,7 +155,9 @@ class Photo extends Model
'created_at' => 'datetime',
'updated_at' => 'datetime',
'taken_at' => DateTimeWithTimezoneCast::class,
'initial_taken_at' => DateTimeWithTimezoneCast::class,
'live_photo_url' => MustNotSetCast::class . ':live_photo_short_path',
'taken_at_mod' => 'datetime',
'owner_id' => 'integer',
'is_starred' => 'boolean',
'tags' => ArrayCast::class,
Expand Down
6 changes: 5 additions & 1 deletion database/factories/PhotoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class PhotoFactory extends Factory
*/
public function definition(): array
{
$now = now();

return [
'title' => 'CR_' . fake()->numerify('####'),
'description' => null,
Expand All @@ -50,8 +52,10 @@ public function definition(): array
'lens' => 'EF200mm f/2L IS',
'shutter' => '1/320 s',
'focal' => '200mm',
'taken_at' => now(),
'initial_taken_at' => $now,
'taken_at_orig_tz' => null,
'taken_at' => $now,
'initial_taken_at_orig_tz' => null,
'is_starred' => false,
'album_id' => null,
'checksum' => sha1(rand()),
Expand Down
43 changes: 43 additions & 0 deletions database/migrations/2025_01_24_200235_add_initial_taken_at.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('photos', function (Blueprint $table): void {
$table->dateTime('initial_taken_at', 0)->nullable(true)->default(null)->after('taken_at_orig_tz')->comment('backup of the original taken_at value');
$table->string('initial_taken_at_orig_tz', 31)->nullable(true)->default(null)->after('initial_taken_at')->comment('backup of the timezone at which the photo has originally been taken');
});

// Set initial values
DB::table('photos')->update([
'initial_taken_at' => DB::raw('taken_at'),
'initial_taken_at_orig_tz' => DB::raw('taken_at_orig_tz'),
]);
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('photos', function (Blueprint $table) {
$table->dropColumn('initial_taken_at_orig_tz');
});
Schema::table('photos', function (Blueprint $table) {
$table->dropColumn('initial_taken_at');
});
}
};
2 changes: 2 additions & 0 deletions lang/cz/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
2 changes: 2 additions & 0 deletions lang/de/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
2 changes: 2 additions & 0 deletions lang/el/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
2 changes: 2 additions & 0 deletions lang/en/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
2 changes: 2 additions & 0 deletions lang/es/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
2 changes: 2 additions & 0 deletions lang/fr/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
2 changes: 2 additions & 0 deletions lang/hu/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
2 changes: 2 additions & 0 deletions lang/it/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
2 changes: 2 additions & 0 deletions lang/ja/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
2 changes: 2 additions & 0 deletions lang/nl/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
2 changes: 2 additions & 0 deletions lang/no/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
2 changes: 2 additions & 0 deletions lang/pl/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
2 changes: 2 additions & 0 deletions lang/pt/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
2 changes: 2 additions & 0 deletions lang/ru/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
2 changes: 2 additions & 0 deletions lang/sk/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
2 changes: 2 additions & 0 deletions lang/sv/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
2 changes: 2 additions & 0 deletions lang/vi/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
2 changes: 2 additions & 0 deletions lang/zh_CN/gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@
'no_tags' => 'No Tags',
'set_tags' => 'Set Tags',
'set_created_at' => 'Set Upload Date',
'set_taken_at' => 'Set Taken Date',
'set_taken_at_info' => 'When set, a star %s will be displayed to indicate that this date is not the original EXIF date.<br>Untick the checkbox and save to reset to the original date.',
],
],

Expand Down
Loading

0 comments on commit 5f4913c

Please sign in to comment.