Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add backend implementation to use file's last modified time #1821

Merged
merged 12 commits into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/Actions/Import/Exec.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Safe\Exceptions\InfoException;
use Safe\Exceptions\StringsException;
use function Safe\file;
use function Safe\filemtime;
use function Safe\glob;
use function Safe\ini_get;
use function Safe\ob_flush;
Expand Down Expand Up @@ -320,7 +321,7 @@ public function do(
$filesCount++;

try {
$this->photoCreate->add(new NativeLocalFile($file), $parentAlbum);
$this->photoCreate->add(new NativeLocalFile($file), filemtime($file), $parentAlbum);
} catch (\Throwable $e) {
$this->report(ImportEventReport::createFromException($e, $file));
}
Expand Down
3 changes: 2 additions & 1 deletion app/Actions/Import/FromUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Models\Photo;
use Illuminate\Support\Collection;
use Safe\Exceptions\InfoException;
use function Safe\filemtime;
use function Safe\ini_get;
use function Safe\parse_url;
use function Safe\set_time_limit;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function do(array $urls, ?Album $album, int $intendedOwnerId): Collection
$downloadedFile = new DownloadedFile($url);

// Import photo/video/raw
$result->add($create->add($downloadedFile, $album));
$result->add($create->add($downloadedFile, filemtime($downloadedFile->getRealPath()), $album));
} catch (\Throwable $e) {
$exceptions[] = $e;
Handler::reportSafely($e);
Expand Down
16 changes: 9 additions & 7 deletions app/Actions/Photo/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ public function __construct(?ImportMode $importMode, int $intendedOwnerId)
* This method may create a new database entry or update an existing
* database entry.
*
* @param NativeLocalFile $sourceFile the source file
* @param AbstractAlbum|null $album the targeted parent album
* @param NativeLocalFile $sourceFile the source file
* @param int $fileLastModifiedTime the timestamp to use if there's no creation date in Exif
* @param AbstractAlbum|null $album the targeted parent album
*
* @return Photo the newly created or updated photo
*
* @throws ModelNotFoundException
* @throws LycheeException
*/
public function add(NativeLocalFile $sourceFile, ?AbstractAlbum $album = null): Photo
public function add(NativeLocalFile $sourceFile, int $fileLastModifiedTime, ?AbstractAlbum $album = null): Photo
wladif marked this conversation as resolved.
Show resolved Hide resolved
{
$sourceFile->assertIsSupportedMediaOrAcceptedRaw();

Expand All @@ -65,7 +66,7 @@ public function add(NativeLocalFile $sourceFile, ?AbstractAlbum $album = null):
$this->initParentAlbum($album);

// Fill in metadata extracted from source file
$this->loadFileMetadata($sourceFile);
$this->loadFileMetadata($sourceFile, $fileLastModifiedTime);

// Look up potential duplicates/partners in order to select the
// proper strategy
Expand Down Expand Up @@ -115,17 +116,18 @@ public function add(NativeLocalFile $sourceFile, ?AbstractAlbum $album = null):
* Extracts the meta-data of the source file and initializes
* {@link AddStrategyParameters::$exifInfo} of {@link Create::$strategyParameters}.
*
* @param NativeLocalFile $sourceFile the source file
* @param NativeLocalFile $sourceFile the source file
* @param int $fileLastModifiedTime the timestamp to use if there's no creation date in Exif
*
* @return void
*
* @throws ExternalComponentMissingException
* @throws MediaFileOperationException
* @throws ExternalComponentFailedException
*/
protected function loadFileMetadata(NativeLocalFile $sourceFile): void
protected function loadFileMetadata(NativeLocalFile $sourceFile, int $fileLastModifiedTime): void
{
$this->strategyParameters->exifInfo = Extractor::createFromFile($sourceFile);
$this->strategyParameters->exifInfo = Extractor::createFromFile($sourceFile, $fileLastModifiedTime);

// Use basename of file if IPTC title missing
if (
Expand Down
3 changes: 2 additions & 1 deletion app/Console/Commands/ExifLens.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Safe\Exceptions\InfoException;
use function Safe\filemtime;
use function Safe\set_time_limit;

class ExifLens extends Command
Expand Down Expand Up @@ -70,7 +71,7 @@ public function handle(): int
foreach ($photos as $photo) {
try {
$localFile = $photo->size_variants->getOriginal()->getFile()->toLocalFile();
$info = Extractor::createFromFile($localFile);
$info = Extractor::createFromFile($localFile, filemtime($localFile->getRealPath()));
$updated = false;
if ($photo->size_variants->getOriginal()->filesize === 0) {
$photo->size_variants->getOriginal()->filesize = $localFile->getFilesize();
Expand Down
3 changes: 2 additions & 1 deletion app/Console/Commands/Takedate.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Carbon;
use Safe\Exceptions\InfoException;
use function Safe\filemtime;
use function Safe\set_time_limit;
use Symfony\Component\Console\Exception\ExceptionInterface as SymfonyConsoleException;
use Symfony\Component\Console\Helper\ProgressBar;
Expand Down Expand Up @@ -144,7 +145,7 @@ public function handle(): int
$this->progressBar->advance();
$localFile = $photo->size_variants->getOriginal()->getFile()->toLocalFile();

$info = Extractor::createFromFile($localFile);
$info = Extractor::createFromFile($localFile, filemtime($localFile->getRealPath()));
if ($info->taken_at !== null) {
// Note: `equalTo` only checks if two times indicate the same
// instant of time on the universe's timeline, i.e. equality
Expand Down
3 changes: 2 additions & 1 deletion app/Console/Commands/VideoData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
use Safe\Exceptions\InfoException;
use function Safe\filemtime;
use function Safe\set_time_limit;
use Symfony\Component\Console\Exception\ExceptionInterface as SymfonyConsoleException;

Expand Down Expand Up @@ -81,7 +82,7 @@ public function handle(): int
$originalSizeVariant = $photo->size_variants->getOriginal();
$file = $originalSizeVariant->getFile()->toLocalFile();

$info = Extractor::createFromFile($file);
$info = Extractor::createFromFile($file, filemtime($file->getRealPath()));

if ($originalSizeVariant->width === 0 && $info->width !== 0) {
$originalSizeVariant->width = $info->width;
Expand Down
2 changes: 2 additions & 0 deletions app/Contracts/Http/Requests/RequestAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,6 @@ class RequestAttribute
public const SKIP_DUPLICATES_ATTRIBUTE = 'skip_duplicates';
public const IMPORT_VIA_SYMLINK_ATTRIBUTE = 'import_via_symlink';
public const RESYNC_METADATA_ATTRIBUTE = 'resync_metadata';

public const FILE_LAST_MODIFIED_TIME = 'fileLastModifiedTime';
}
4 changes: 2 additions & 2 deletions app/Http/Controllers/PhotoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ public function add(AddPhotoRequest $request): PhotoResource|JsonResponse
// End of work-around

if (Configs::getValueAsBool('use_job_queues')) {
ProcessImageJob::dispatch($processableFile, $request->album());
ProcessImageJob::dispatch($processableFile, $request->album(), $request->fileLastModifiedTime());

return new JsonResponse(null, 201);
}

$job = new ProcessImageJob($processableFile, $request->album());
$job = new ProcessImageJob($processableFile, $request->album(), $request->fileLastModifiedTime());
$photo = $job->handle($this->albumFactory);
$isNew = $photo->created_at->toIso8601String() === $photo->updated_at->toIso8601String();

Expand Down
12 changes: 12 additions & 0 deletions app/Http/Requests/Photo/AddPhotoRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class AddPhotoRequest extends BaseApiRequest implements HasAbstractAlbum
use HasAbstractAlbumTrait;
use AuthorizeCanEditAlbumTrait;

protected int $fileLastModifiedTime;
protected UploadedFile $file;

/**
Expand All @@ -34,11 +35,22 @@ protected function processValidatedValues(array $values, array $files): void
$this->album = $albumID === null ?
null :
$this->albumFactory->findAbstractAlbumOrFail($albumID);
$this->fileLastModifiedTime = $this->getFileLastModifiedTimeInSeconds($values);
wladif marked this conversation as resolved.
Show resolved Hide resolved
$this->file = $files[RequestAttribute::FILE_ATTRIBUTE];
}

public function uploadedFile(): UploadedFile
{
return $this->file;
}

public function fileLastModifiedTime(): int
{
return $this->fileLastModifiedTime;
}

private function getFileLastModifiedTimeInSeconds(array $values): int
wladif marked this conversation as resolved.
Show resolved Hide resolved
{
return $values[RequestAttribute::FILE_LAST_MODIFIED_TIME] / 1000;
}
}
1 change: 1 addition & 0 deletions app/Http/Resources/ConfigurationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public function toArray($request): array
'small_max_width' => Configs::getValueAsInt('small_max_width'),
'thumb_2x' => Configs::getValueAsBool('thumb_2x'),
'unlock_password_photos_with_url_param' => Configs::getValueAsBool('unlock_password_photos_with_url_param'),
'use_last_modified_date_when_no_exif_date' => Configs::getValueAsBool('use_last_modified_date_when_no_exif_date'),
]),

'album_subtitle_type' => Configs::getValueAsString('album_subtitle_type'),
Expand Down
1 change: 1 addition & 0 deletions app/Http/RuleSets/Photo/AddPhotoRuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static function rules(): array
{
return [
RequestAttribute::ALBUM_ID_ATTRIBUTE => ['present', new AlbumIDRule(true)],
RequestAttribute::FILE_LAST_MODIFIED_TIME => 'required|numeric',
RequestAttribute::FILE_ATTRIBUTE => 'required|file',
];
}
Expand Down
5 changes: 4 additions & 1 deletion app/Jobs/ProcessImageJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,21 @@ class ProcessImageJob implements ShouldQueue
public string $originalBaseName;
public ?string $albumId;
public int $userId;
public int $fileLastModifiedTime;

/**
* Create a new job instance.
*/
public function __construct(
ProcessableJobFile $file,
?AbstractAlbum $albumId,
int $fileLastModifiedTime,
) {
$this->filePath = $file->getPath();
$this->originalBaseName = $file->getOriginalBasename();
$this->albumId = $albumId?->id;
$this->userId = Auth::user()->id;
$this->fileLastModifiedTime = $fileLastModifiedTime;

// Set up our new history record.
$this->history = new JobHistory();
Expand Down Expand Up @@ -83,7 +86,7 @@ public function handle(AlbumFactory $albumFactory): Photo
$album = $albumFactory->findAbstractAlbumOrFail($this->albumId);
}

$photo = $create->add($copiedFile, $album);
$photo = $create->add($copiedFile, $this->fileLastModifiedTime, $album);

// Once the job has finished, set history status to 1.
$this->history->status = JobStatus::SUCCESS;
Expand Down
13 changes: 10 additions & 3 deletions app/Metadata/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
use Carbon\Exceptions\InvalidTimeZoneException;
use Illuminate\Support\Carbon;
use PHPExif\Enum\ReaderType;
use PHPExif\Exif;
use PHPExif\Reader\PhpExifReaderException;
use PHPExif\Reader\Reader;
use Safe\DateTime;
use Safe\Exceptions\StringsException;

/**
Expand Down Expand Up @@ -58,14 +58,15 @@ class Extractor
/**
* Extracts metadata from a file.
*
* @param NativeLocalFile $file the file
* @param NativeLocalFile $file the file
* @param int $fileLastModifiedTime the timestamp to use if there's no creation date in Exif
*
* @return Extractor
*
* @throws ExternalComponentMissingException
* @throws MediaFileOperationException
*/
public static function createFromFile(NativeLocalFile $file): self
public static function createFromFile(NativeLocalFile $file, int $fileLastModifiedTime): self
{
$metadata = new self();
$isSupportedVideo = $file->isSupportedVideo();
Expand Down Expand Up @@ -156,6 +157,12 @@ public static function createFromFile(NativeLocalFile $file): self
$metadata->microVideoOffset = ($exif->getMicroVideoOffset() !== false) ? (int) $exif->getMicroVideoOffset() : 0;

$taken_at = $exif->getCreationDate();

if ($taken_at === false &&
Configs::getValueAsBool('use_last_modified_date_when_no_exif_date')) {
$taken_at = DateTime::createFromFormat('U', "$fileLastModifiedTime");
}

if ($taken_at !== false) {
try {
$taken_at = Carbon::instance($taken_at);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;

return new class() extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
defined('BOOL') or define('BOOL', '0|1');

DB::table('configs')->insert([
'key' => 'use_last_modified_date_when_no_exif_date',
'value' => '0',
'cat' => 'Image Processing',
'type_range' => BOOL,
'confidentiality' => '0',
'description' => 'Use the file\'s last modified time when Exif data has no creation date',
]);
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::table('configs')->where('key', '=', 'use_last_modified_date_when_no_exif_date')->delete();
}
};
1 change: 1 addition & 0 deletions tests/Feature/LibUnitTests/PhotosUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function upload(
$response = $this->testCase->post(
'/api/Photo::add', [
'albumID' => $albumID,
'fileLastModifiedTime' => 1678824303000,
wladif marked this conversation as resolved.
Show resolved Hide resolved
'file' => $file,
], [
'CONTENT_TYPE' => 'multipart/form-data',
Expand Down