get exif data refactoring

This commit is contained in:
Čarodej
2022-04-26 10:08:11 +02:00
parent 43368c7555
commit 92561d137d
11 changed files with 111 additions and 96 deletions

View File

@@ -1,6 +1,6 @@
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:/yb14wOXR+PqqMWnzqE2YZxcvqwSYYKDHtrF0BpT5fM=
APP_KEY=base64:fzZomRn3E8Rb6UDR8oqFiNo8Y4L5RyX4oVVli3KiWQA=
APP_DEBUG=true
APP_URL=http://localhost
APP_DEMO=false

View File

@@ -67,5 +67,6 @@ return [
'2_0_13',
'2_0_14',
'2_0_16',
'2_1_1',
],
];

View File

@@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('exifs', function (Blueprint $table) {
$table->uuid('file_id')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
};

View File

@@ -13,7 +13,7 @@ class ProcessFileAction
public function __construct(
public RecordUploadAction $recordUpload,
public GetFileParentId $getFileParentId,
public StoreFileExifMetadataAction $storeExifMetadata,
public StoreExifDataAction $storeExifData,
public MoveFileToFTPStorageAction $moveFileToFTPStorage,
public ProcessImageThumbnailAction $createImageThumbnail,
public MoveFileToExternalStorageAction $moveFileToExternalStorage,
@@ -65,6 +65,9 @@ class ProcessFileAction
// Create multiple image thumbnails
($this->createImageThumbnail)($name, $user->id);
// Store exif data if exists
$exif = ($this->storeExifData)("files/$user->id/$name");
// Move file to external storage
match (config('filesystems.default')) {
's3' => ($this->moveFileToExternalStorage)($name, $user->id),
@@ -84,8 +87,8 @@ class ProcessFileAction
'creator_id' => auth()->check() ? auth()->id() : $user->id,
]);
// Store file exif data
($this->storeExifMetadata)($file);
// Attach file into the exif data
$exif?->update(['file_id' => $file->id]);
// Return new file
return $file;

View File

@@ -0,0 +1,61 @@
<?php
namespace Domain\Files\Actions;
use Log;
use Str;
use Storage;
use Exception;
use Domain\Files\Models\Exif;
use Intervention\Image\ImageManagerStatic as Image;
class StoreExifDataAction
{
public function __invoke(string $path): Exif|null
{
try {
$mimetype = Storage::mimeType($path);
// Check if file is jpeg
if (explode('/', $mimetype)[1] !== 'jpeg') {
return null;
}
// Get Exif data
$exifRaw = mb_convert_encoding(
Image::make(Storage::path($path))->exif(),
'UTF8',
'UTF8'
);
// Convert to object
$exif = json_decode(json_encode($exifRaw));
return Exif::create([
'file_id' => Str::uuid(), // TODO: temporary store to prevent crash before app will be successfully upgraded
'date_time_original' => $exif->DateTimeOriginal ?? null,
'artist' => $exif->OwnerName ?? null,
'width' => $exif->COMPUTED->Width ?? null,
'height' => $exif->COMPUTED->Height ?? null,
'x_resolution' => $exif->XResolution ?? null,
'y_resolution' => $exif->YResolution ?? null,
'color_space' => $exif->ColorSpace ?? null,
'camera' => $exif->Make ?? null,
'model' => $exif->Model ?? null,
'aperture_value' => $exif->ApertureValue ?? null,
'exposure_time' => $exif->ExposureTime ?? null,
'focal_length' => $exif->FocalLength ?? null,
'iso' => $exif->ISOSpeedRatings ?? null,
'aperture_f_number' => $exif->COMPUTED->ApertureFNumber ?? null,
'ccd_width' => $exif->COMPUTED->CCDWidth ?? null,
'longitude' => $exif->GPSLongitude ?? null,
'latitude' => $exif->GPSLatitude ?? null,
'longitude_ref' => $exif->GPSLongitudeRef ?? null,
'latitude_ref' => $exif->GPSLatitudeRef ?? null,
]);
} catch (Exception $error) {
Log::error('Unable to get exif data');
return null;
}
}
}

View File

@@ -1,41 +0,0 @@
<?php
namespace Domain\Files\Actions;
use Domain\Files\Models\File;
class StoreFileExifMetadataAction
{
public function __invoke(File $file)
{
// Get exif metadata
$data = readExifData("files/$file->user_id/$file->basename");
if (is_null($data)) {
return;
}
$file
->exif()
->create([
'date_time_original' => $data->DateTimeOriginal ?? null,
'artist' => $data->OwnerName ?? null,
'width' => $data->COMPUTED->Width ?? null,
'height' => $data->COMPUTED->Height ?? null,
'x_resolution' => $data->XResolution ?? null,
'y_resolution' => $data->YResolution ?? null,
'color_space' => $data->ColorSpace ?? null,
'camera' => $data->Make ?? null,
'model' => $data->Model ?? null,
'aperture_value' => $data->ApertureValue ?? null,
'exposure_time' => $data->ExposureTime ?? null,
'focal_length' => $data->FocalLength ?? null,
'iso' => $data->ISOSpeedRatings ?? null,
'aperture_f_number' => $data->COMPUTED->ApertureFNumber ?? null,
'ccd_width' => $data->COMPUTED->CCDWidth ?? null,
'longitude' => $data->GPSLongitude ?? null,
'latitude' => $data->GPSLatitude ?? null,
'longitude_ref' => $data->GPSLongitudeRef ?? null,
'latitude_ref' => $data->GPSLatitudeRef ?? null,
]);
}
}

View File

@@ -12,9 +12,9 @@ use Illuminate\Support\Facades\Storage;
use Domain\Files\Resources\FileResource;
use Domain\Files\Actions\ProcessFileAction;
use Spatie\QueueableAction\QueueableAction;
use Domain\Files\Actions\StoreExifDataAction;
use Domain\Files\Actions\MoveFileToFTPStorageAction;
use Domain\Files\Actions\ProcessImageThumbnailAction;
use Domain\Files\Actions\StoreFileExifMetadataAction;
use Domain\RemoteUpload\Events\RemoteFileCreatedEvent;
use Domain\Files\Actions\MoveFileToExternalStorageAction;
@@ -24,7 +24,7 @@ class GetContentFromExternalSource
public function __construct(
public ProcessFileAction $processFile,
public StoreFileExifMetadataAction $storeExifMetadata,
public StoreExifDataAction $storeExifData,
public MoveFileToFTPStorageAction $moveFileToFTPStorage,
public ProcessImageThumbnailAction $createImageThumbnail,
public MoveFileToExternalStorageAction $moveFileToExternalStorage,
@@ -70,6 +70,9 @@ class GetContentFromExternalSource
// Create multiple image thumbnails
($this->createImageThumbnail)($basename, $user->id);
// Store file exif information
$exif = ($this->storeExifData)($path);
// Create new file
$file = File::create([
'mimetype' => $extension,
@@ -82,8 +85,8 @@ class GetContentFromExternalSource
'creator_id' => auth()->id(),
]);
// Store file exif information
($this->storeExifMetadata)($file);
// Attach file into the exif data
$exif?->update(['file_id' => $file->id]);
// Move file to external storage
match (config('filesystems.default')) {

View File

@@ -22,6 +22,11 @@ class UpgradingVersionsController
) {
}
public function upgrade_to_2_1_1(): void
{
($this->upgradeDatabase)();
}
public function upgrade_to_2_0_10(): void
{
($this->upgradeDatabase)();

View File

@@ -723,19 +723,6 @@ if (! function_exists('map_language_translations')) {
}
}
if (! function_exists('get_file_type_from_mimetype')) {
/**
* Get file type from mimetype
*
* @param $mimetype
* @return mixed
*/
function get_file_type_from_mimetype($mimetype)
{
return explode('/', $mimetype)[1];
}
}
if (! function_exists('getPrettyName')) {
/**
* Format pretty name file
@@ -756,40 +743,6 @@ if (! function_exists('getPrettyName')) {
}
}
if (! function_exists('readExifData')) {
/**
* Get exif data from jpeg image
*/
function readExifData(string $file): object|null
{
$mimetype = Storage::mimeType($file);
if (!$mimetype) {
return null;
}
$type = get_file_type_from_mimetype(
Storage::mimeType($file)
);
if ($type !== 'jpeg') {
return null;
}
try {
// Try to get the exif data
$data = Image::make(Storage::path($file))->exif();
// Encode data
$encodedData = mb_convert_encoding($data, 'UTF8', 'UTF8');
return json_decode(json_encode($encodedData));
} catch (Exception $e) {
return null;
}
}
}
if (! function_exists('get_default_language_translations')) {
/**
* @return Collection

BIN
storage/files.index-shm Normal file

Binary file not shown.

BIN
storage/files.index-wal Normal file

Binary file not shown.