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 all 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
19 changes: 12 additions & 7 deletions app/Actions/Photo/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use App\SmartAlbums\PublicAlbum;
use App\SmartAlbums\StarredAlbum;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use function Safe\filemtime;

class Create
{
Expand All @@ -48,24 +49,27 @@ 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|null $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): Photo
public function add(NativeLocalFile $sourceFile, ?AbstractAlbum $album, ?int $fileLastModifiedTime = null): Photo
{
$fileLastModifiedTime ??= filemtime($sourceFile->getRealPath());

$sourceFile->assertIsSupportedMediaOrAcceptedRaw();

// Fill in information about targeted parent album
// throws InvalidPropertyException
$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 +119,18 @@ public function add(NativeLocalFile $sourceFile, ?AbstractAlbum $album): Photo
* 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
8 changes: 8 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,18 @@ protected function processValidatedValues(array $values, array $files): void
$this->album = $albumID === null ?
null :
$this->albumFactory->findAbstractAlbumOrFail($albumID);
// Convert the File Last Modified to seconds instead of milliseconds
$this->fileLastModifiedTime = $values[RequestAttribute::FILE_LAST_MODIFIED_TIME] / 1000;
$this->file = $files[RequestAttribute::FILE_ATTRIBUTE];
}

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

public function fileLastModifiedTime(): int
{
return $this->fileLastModifiedTime;
}
}
1 change: 1 addition & 0 deletions app/Http/Resources/ConfigurationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,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'),
'smart_album_visibilty' => [
'recent' => RecentAlbum::getInstance()->public_permissions !== null,
'starred' => StarredAlbum::getInstance()->public_permissions !== null,
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, $album, $this->fileLastModifiedTime);

// 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();
}
};
84 changes: 84 additions & 0 deletions tests/Feature/BasePhotosAddHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,4 +528,88 @@ public function testUploadMultibyteTitleWithoutExifTool(): void
$this->testUploadMultibyteTitle();
Configs::set(TestConstants::CONFIG_HAS_EXIF_TOOL, $hasExifTool);
}

/**
* Test the upload of a photo without Exif when the use of file last modified time is enabled.
* Expected result is that import succeeds and taken at property has the correct value.
*
* @return void
*/
public function testTakenAtForPhotoUploadWithoutExif(): void
{
$useLastModifiedDate = Configs::getValueAsBool(TestConstants::CONFIG_USE_LAST_MODIFIED_DATE_WHEN_NO_EXIF);
Configs::set(TestConstants::CONFIG_USE_LAST_MODIFIED_DATE_WHEN_NO_EXIF, true);

$response = $this->photos_tests->upload(
AbstractTestCase::createUploadedFile(TestConstants::SAMPLE_FILE_WITHOUT_EXIF)
);
$response->assertJson([
'taken_at' => '2023-03-14T20:05:03+00:00',
'taken_at_orig_tz' => '+00:00',
]);

Configs::set(TestConstants::CONFIG_USE_LAST_MODIFIED_DATE_WHEN_NO_EXIF, $useLastModifiedDate);
}

/**
* Test the upload of a photo without Exif when the use of file last modified time is enabled and the value is set to 0.
* Expected result is that import proceeds and taken at is set to 1st Jan 1970.
*
* @return void
*/
public function testTakenAtForPhotoUploadWithoutExif2(): void
{
$useLastModifiedDate = Configs::getValueAsBool(TestConstants::CONFIG_USE_LAST_MODIFIED_DATE_WHEN_NO_EXIF);
Configs::set(TestConstants::CONFIG_USE_LAST_MODIFIED_DATE_WHEN_NO_EXIF, true);

$response = $this->photos_tests->upload(
file: AbstractTestCase::createUploadedFile(TestConstants::SAMPLE_FILE_WITHOUT_EXIF),
fileLastModifiedTime: 0
);
$response->assertJson([
'taken_at' => '1970-01-01T00:00:00+00:00',
'taken_at_orig_tz' => '+00:00',
]);

Configs::set(TestConstants::CONFIG_USE_LAST_MODIFIED_DATE_WHEN_NO_EXIF, $useLastModifiedDate);
}

/**
* Test the upload of a photo without Exif when the use of file last modified time is disabled.
* Expected result is that import proceeds and taken at has no value.
*
* @return void
*/
public function testTakenAtForPhotoUploadWithoutExif3(): void
{
$response = $this->photos_tests->upload(
AbstractTestCase::createUploadedFile(TestConstants::SAMPLE_FILE_WITHOUT_EXIF)
);
$response->assertJson([
'taken_at' => null,
'taken_at_orig_tz' => null,
]);
}

/**
* Test the upload of a photo with Exif when the use of file last modified time is enabled.
* Expected result is that import succeeds and taken at is set to the value from Exif.
*
* @return void
*/
public function testTakenAtForPhotoUploadWithExif(): void
{
$useLastModifiedDate = Configs::getValueAsBool(TestConstants::CONFIG_USE_LAST_MODIFIED_DATE_WHEN_NO_EXIF);
Configs::set(TestConstants::CONFIG_USE_LAST_MODIFIED_DATE_WHEN_NO_EXIF, true);

$response = $this->photos_tests->upload(
AbstractTestCase::createUploadedFile(TestConstants::SAMPLE_FILE_NIGHT_IMAGE)
);
$response->assertJson([
'taken_at' => '2019-06-01T01:28:25+02:00',
'taken_at_orig_tz' => '+02:00',
]);

Configs::set(TestConstants::CONFIG_USE_LAST_MODIFIED_DATE_WHEN_NO_EXIF, $useLastModifiedDate);
}
}
3 changes: 3 additions & 0 deletions tests/Feature/Constants/TestConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class TestConstants
public const SAMPLE_FILE_TRAIN_VIDEO = 'tests/Samples/train.mov';
public const SAMPLE_FILE_UNDEFINED_EXIF_TAG = 'tests/Samples/undefined-exif-tag.jpg';
public const SAMPLE_FILE_WEBP = 'tests/Samples/webp.webp';
public const SAMPLE_FILE_WITHOUT_EXIF = 'tests/Samples/without_exif.jpg';
public const SAMPLE_FILE_XCF = 'tests/Samples/xcf.xcf';

public const SAMPLE_FILES_2_MIME = [
Expand All @@ -66,6 +67,7 @@ class TestConstants
self::SAMPLE_FILE_TRAIN_VIDEO => self::MIME_TYPE_VID_QUICKTIME,
self::SAMPLE_FILE_UNDEFINED_EXIF_TAG => self::MIME_TYPE_IMG_JPEG,
self::SAMPLE_FILE_WEBP => self::MIME_TYPE_IMG_WEBP,
self::SAMPLE_FILE_WITHOUT_EXIF => self::MIME_TYPE_IMG_JPEG,
self::SAMPLE_FILE_XCF => self::MIME_TYPE_IMG_XCF,
];

Expand All @@ -88,6 +90,7 @@ class TestConstants
public const CONFIG_PUBLIC_ON_THIS_DAY = 'public_on_this_day';
public const CONFIG_RAW_FORMATS = 'raw_formats';
public const CONFIG_USE_JOB_QUEUES = 'use_job_queues';
public const CONFIG_USE_LAST_MODIFIED_DATE_WHEN_NO_EXIF = 'use_last_modified_date_when_no_exif_date';

public const PHOTO_NIGHT_TITLE = 'night';
public const PHOTO_MONGOLIA_TITLE = 'mongolia';
Expand Down
Loading