forked from LycheeOrg/Lychee
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[stacked 7] Add standalone flow. (LycheeOrg#2371)
- Loading branch information
Showing
19 changed files
with
514 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
app/Actions/Photo/Pipes/Standalone/CreateOriginalSizeVariant.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Actions\Photo\Pipes\Standalone; | ||
|
||
use App\Contracts\PhotoCreate\StandalonePipe; | ||
use App\DTO\ImageDimension; | ||
use App\DTO\PhotoCreate\StandaloneDTO; | ||
use App\Enum\SizeVariantType; | ||
|
||
class CreateOriginalSizeVariant implements StandalonePipe | ||
{ | ||
public function handle(StandaloneDTO $state, \Closure $next): StandaloneDTO | ||
{ | ||
// Create original size variant of photo | ||
// If the image has been loaded (and potentially auto-rotated) | ||
// take the dimension from the image. | ||
// As a fallback for media files from which no image could be extracted (e.g. unsupported file formats) we use the EXIF data. | ||
$imageDim = $state->sourceImage?->isLoaded() ? | ||
$state->sourceImage->getDimensions() : | ||
new ImageDimension($state->exifInfo->width, $state->exifInfo->height); | ||
$state->photo->size_variants->create( | ||
SizeVariantType::ORIGINAL, | ||
$state->targetFile->getRelativePath(), | ||
$imageDim, | ||
$state->streamStat->bytes | ||
); | ||
|
||
return $next($state); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Actions\Photo\Pipes\Standalone; | ||
|
||
use App\Contracts\Models\SizeVariantFactory; | ||
use App\Contracts\PhotoCreate\StandalonePipe; | ||
use App\DTO\PhotoCreate\StandaloneDTO; | ||
use App\Exceptions\Handler; | ||
|
||
class CreateSizeVariants implements StandalonePipe | ||
{ | ||
public function handle(StandaloneDTO $state, \Closure $next): StandaloneDTO | ||
{ | ||
// Create remaining size variants if we were able to successfully | ||
// extract a reference image | ||
if ($state->sourceImage?->isLoaded()) { | ||
try { | ||
/** @var SizeVariantFactory $sizeVariantFactory */ | ||
$sizeVariantFactory = resolve(SizeVariantFactory::class); | ||
$sizeVariantFactory->init($state->photo, $state->sourceImage, $state->namingStrategy); | ||
$sizeVariantFactory->createSizeVariants(); | ||
} catch (\Throwable $t) { | ||
// Don't re-throw the exception, because we do not want the | ||
// import to fail completely only due to missing size variants. | ||
// There are just too many options why the creation of size | ||
// variants may fail. | ||
Handler::reportSafely($t); | ||
} | ||
} | ||
|
||
return $next($state); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/Actions/Photo/Pipes/Standalone/ExtractGoogleMotionPictures.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace App\Actions\Photo\Pipes\Standalone; | ||
|
||
use App\Contracts\PhotoCreate\StandalonePipe; | ||
use App\DTO\PhotoCreate\StandaloneDTO; | ||
use App\Exceptions\Handler; | ||
use App\Image\Files\TemporaryLocalFile; | ||
use App\Image\Handlers\GoogleMotionPictureHandler; | ||
|
||
class ExtractGoogleMotionPictures implements StandalonePipe | ||
{ | ||
public function handle(StandaloneDTO $state, \Closure $next): StandaloneDTO | ||
{ | ||
if ($state->exifInfo->microVideoOffset === 0) { | ||
return $next($state); | ||
} | ||
|
||
// Handle Google Motion Pictures | ||
// We must extract the video stream from the original (local) | ||
// file and stash it away, before the original file is moved into | ||
// its (potentially remote) final position | ||
try { | ||
$state->tmpVideoFile = new TemporaryLocalFile(GoogleMotionPictureHandler::FINAL_VIDEO_FILE_EXTENSION, $state->sourceFile->getBasename()); | ||
$gmpHandler = new GoogleMotionPictureHandler(); | ||
$gmpHandler->load($state->sourceFile, $state->exifInfo->microVideoOffset); | ||
$gmpHandler->saveVideoStream($state->tmpVideoFile); | ||
} catch (\Throwable $e) { | ||
Handler::reportSafely($e); | ||
$state->tmpVideoFile = null; | ||
} | ||
|
||
return $next($state); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace App\Actions\Photo\Pipes\Standalone; | ||
|
||
use App\Contracts\PhotoCreate\StandalonePipe; | ||
use App\DTO\PhotoCreate\StandaloneDTO; | ||
use App\Exceptions\Handler; | ||
use App\Image\Handlers\ImageHandler; | ||
use App\Image\Handlers\VideoHandler; | ||
|
||
class FetchSourceImage implements StandalonePipe | ||
{ | ||
public function handle(StandaloneDTO $state, \Closure $next): StandaloneDTO | ||
{ | ||
try { | ||
if ($state->photo->isVideo()) { | ||
$videoHandler = new VideoHandler(); | ||
$videoHandler->load($state->sourceFile); | ||
$position = is_numeric($state->photo->aperture) ? floatval($state->photo->aperture) / 2 : 0.0; | ||
$state->sourceImage = $videoHandler->extractFrame($position); | ||
} else { | ||
// Load source image if it is a supported photo format | ||
$state->sourceImage = new ImageHandler(); | ||
$state->sourceImage->load($state->sourceFile); | ||
} | ||
} catch (\Throwable $t) { | ||
// This may happen for videos if FFmpeg is not available to | ||
// extract a still frame, or for raw files (Imagick may be | ||
// able to convert them to jpeg, but there are no | ||
// guarantees, and Imagick may not be available). | ||
$state->sourceImage = null; | ||
|
||
// Log an error without failing. | ||
Handler::reportSafely($t); | ||
} | ||
|
||
return $next($state); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Actions\Photo\Pipes\Standalone; | ||
|
||
use App\Contracts\PhotoCreate\StandalonePipe; | ||
use App\DTO\PhotoCreate\StandaloneDTO; | ||
|
||
/** | ||
* Set the timestamps of the creation and updated_at time. | ||
*/ | ||
class FixTimeStamps implements StandalonePipe | ||
{ | ||
public function handle(StandaloneDTO $state, \Closure $next): StandaloneDTO | ||
{ | ||
$state->photo->updateTimestamps(); | ||
|
||
return $next($state); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Actions\Photo\Pipes\Standalone; | ||
|
||
use App\Contracts\Models\AbstractSizeVariantNamingStrategy; | ||
use App\Contracts\PhotoCreate\StandalonePipe; | ||
use App\DTO\PhotoCreate\StandaloneDTO; | ||
|
||
class InitNamingStrategy implements StandalonePipe | ||
{ | ||
public function handle(StandaloneDTO $state, \Closure $next): StandaloneDTO | ||
{ | ||
$state->namingStrategy = resolve(AbstractSizeVariantNamingStrategy::class); | ||
$state->namingStrategy->setPhoto($state->photo); | ||
$state->namingStrategy->setExtension( | ||
$state->sourceFile->getOriginalExtension() | ||
); | ||
|
||
return $next($state); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/Actions/Photo/Pipes/Standalone/PlaceGoogleMotionVideo.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace App\Actions\Photo\Pipes\Standalone; | ||
|
||
use App\Contracts\PhotoCreate\StandalonePipe; | ||
use App\DTO\PhotoCreate\StandaloneDTO; | ||
use App\Image\Files\FlysystemFile; | ||
|
||
class PlaceGoogleMotionVideo implements StandalonePipe | ||
{ | ||
public function handle(StandaloneDTO $state, \Closure $next): StandaloneDTO | ||
{ | ||
// If we have a temporary video file from a Google Motion Picture, | ||
// we must move the preliminary extracted video file next to the | ||
// final target file | ||
if ($state->tmpVideoFile !== null) { | ||
$videoTargetPath = | ||
pathinfo($state->targetFile->getRelativePath(), PATHINFO_DIRNAME) . | ||
'/' . | ||
pathinfo($state->targetFile->getRelativePath(), PATHINFO_FILENAME) . | ||
$state->tmpVideoFile->getExtension(); | ||
$videoTargetFile = new FlysystemFile($state->targetFile->getDisk(), $videoTargetPath); | ||
$videoTargetFile->write($state->tmpVideoFile->read()); | ||
$state->photo->live_photo_short_path = $videoTargetFile->getRelativePath(); | ||
$state->tmpVideoFile->close(); | ||
$state->tmpVideoFile->delete(); | ||
$state->tmpVideoFile = null; | ||
} | ||
|
||
return $next($state); | ||
} | ||
} |
Oops, something went wrong.