2016-09-02 21:27:50 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\Album;
|
2016-09-02 22:00:42 +01:00
|
|
|
use App\AlbumSources\IAlbumSource;
|
2016-09-02 21:27:50 +01:00
|
|
|
use App\Photo;
|
|
|
|
use App\Upload;
|
|
|
|
use App\UploadPhoto;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
|
|
|
class ProcessUploadCommand extends Command
|
|
|
|
{
|
2016-09-02 22:18:40 +01:00
|
|
|
const METADATA_VERSION = 1;
|
|
|
|
|
2016-09-02 21:27:50 +01:00
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'twilight:process-uploads';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Processes uploads made through the web application.';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$uploadsToProcess = Upload::where([
|
|
|
|
['is_completed', false],
|
|
|
|
['is_processing', false]
|
|
|
|
])
|
|
|
|
->orderBy('created_at', 'desc')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
foreach ($uploadsToProcess as $upload)
|
|
|
|
{
|
|
|
|
$this->output->writeln(sprintf('Processing upload #%d', $upload->id));
|
|
|
|
$this->handleUpload($upload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function handleUpload(Upload $upload)
|
|
|
|
{
|
|
|
|
$photos = $upload->uploadPhotos;
|
|
|
|
foreach ($photos as $photo)
|
|
|
|
{
|
|
|
|
$this->handlePhoto($photo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function handlePhoto(UploadPhoto $uploadPhoto)
|
|
|
|
{
|
2016-09-02 22:18:40 +01:00
|
|
|
/** @var Photo $photo */
|
2016-09-02 21:27:50 +01:00
|
|
|
$photo = $uploadPhoto->photo;
|
|
|
|
$this->output->writeln(sprintf('Analysing photo #%d: %s', $photo->id, $photo->name));
|
|
|
|
|
2016-09-02 22:00:42 +01:00
|
|
|
/** @var Album $album */
|
2016-09-02 21:27:50 +01:00
|
|
|
$album = $photo->album;
|
2016-09-02 22:00:42 +01:00
|
|
|
$albumSource = $album->getAlbumSource();
|
2016-09-02 21:27:50 +01:00
|
|
|
|
2016-09-02 22:00:42 +01:00
|
|
|
$photoFile = $albumSource->getPathToPhoto($album, $photo);
|
2016-09-02 21:27:50 +01:00
|
|
|
|
2016-09-02 22:18:40 +01:00
|
|
|
$exifData = @exif_read_data($photoFile);
|
|
|
|
dump($exifData);
|
|
|
|
|
|
|
|
$photo->metadata_version = ProcessUploadCommand::METADATA_VERSION;
|
|
|
|
$photo->taken_at = $this->metadataDateTime($exifData);
|
|
|
|
$photo->camera_make = $this->metadataCameraMake($exifData);
|
|
|
|
$photo->camera_model = $this->metadataCameraModel($exifData);
|
|
|
|
$photo->camera_software = $this->metadataCameraSoftware($exifData);
|
|
|
|
$photo->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function metadataCameraMake(array $exifData)
|
|
|
|
{
|
|
|
|
if (isset($exifData['Make']))
|
|
|
|
{
|
|
|
|
return $exifData['Make'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function metadataCameraModel(array $exifData)
|
|
|
|
{
|
|
|
|
if (isset($exifData['Model']))
|
|
|
|
{
|
|
|
|
return $exifData['Model'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function metadataCameraSoftware(array $exifData)
|
|
|
|
{
|
|
|
|
if (isset($exifData['Software']))
|
|
|
|
{
|
|
|
|
return $exifData['Software'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function metadataDateTime(array $exifData)
|
|
|
|
{
|
|
|
|
$dateTime = null;
|
|
|
|
if (isset($exifData['DateTime']))
|
|
|
|
{
|
|
|
|
$dateTime = $exifData['DateTime'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_null($dateTime))
|
|
|
|
{
|
|
|
|
$dateTime = preg_replace('/^([\d]{4}):([\d]{2}):([\d]{2})/', '$1-$2-$3', $dateTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dateTime;
|
2016-09-02 21:27:50 +01:00
|
|
|
}
|
|
|
|
}
|