-
-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Stacked 5] Remove local requirement Middleware (#2383)
* [Stacked 6] Remove hard coded values from naming strategy (#2384) * [Stacked 7] Finally upload the data to S3 (#2385)
- Loading branch information
Showing
11 changed files
with
172 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Actions\Photo\Pipes\Shared; | ||
|
||
use App\Assets\Features; | ||
use App\Contracts\PhotoCreate\PhotoDTO; | ||
use App\Contracts\PhotoCreate\PhotoPipe; | ||
use App\Jobs\UploadSizeVariantToS3Job; | ||
use App\Models\Configs; | ||
use App\Models\SizeVariant; | ||
|
||
/** | ||
* Upload SizeVariants to S3 once done (if enabled). | ||
* Note that we first create the job, then we dispatch it. | ||
* This allows to manage the queue properly and see it in the feedback. | ||
*/ | ||
class UploadSizeVariantsToS3 implements PhotoPipe | ||
{ | ||
public function handle(PhotoDTO $state, \Closure $next): PhotoDTO | ||
{ | ||
if (Features::active('use-s3')) { | ||
$use_job_queues = Configs::getValueAsBool('use_job_queues'); | ||
|
||
$jobs = $state->getPhoto()->size_variants->toCollection() | ||
->filter(fn ($v) => $v !== null) | ||
->map(fn (SizeVariant $variant) => new UploadSizeVariantToS3Job($variant)); | ||
|
||
$jobs->each(fn ($job) => $use_job_queues ? dispatch($job) : dispatch_sync($job)); | ||
} | ||
|
||
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
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 was deleted.
Oops, something went wrong.
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,96 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Enum\JobStatus; | ||
use App\Enum\SizeVariantType; | ||
use App\Enum\StorageDiskType; | ||
use App\Models\JobHistory; | ||
use App\Models\Photo; | ||
use App\Models\SizeVariant; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\Support\Facades\Storage; | ||
use Illuminate\Support\Str; | ||
|
||
class UploadSizeVariantToS3Job implements ShouldQueue | ||
{ | ||
use Dispatchable; | ||
use InteractsWithQueue; | ||
use Queueable; | ||
use SerializesModels; | ||
|
||
protected JobHistory $history; | ||
|
||
protected SizeVariant $variant; | ||
|
||
public function __construct(SizeVariant $variant) | ||
{ | ||
$this->variant = $variant; | ||
|
||
// Set up our new history record. | ||
$this->history = new JobHistory(); | ||
$this->history->owner_id = Auth::user()->id; | ||
$this->history->job = Str::limit(sprintf('Upload sizeVariant to S3: %s.', $this->variant->short_path), 200); | ||
$this->history->status = JobStatus::READY; | ||
$this->history->save(); | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
$this->history->status = JobStatus::STARTED; | ||
$this->history->save(); | ||
|
||
Storage::disk(StorageDiskType::S3->value)->writeStream( | ||
$this->variant->short_path, | ||
Storage::disk(StorageDiskType::LOCAL->value)->readStream($this->variant->short_path) | ||
); | ||
|
||
Storage::disk(StorageDiskType::LOCAL->value)->delete($this->variant->short_path); | ||
|
||
$this->variant->storage_disk = StorageDiskType::S3; | ||
$this->variant->save(); | ||
|
||
$this->handleVideoPartner(); | ||
|
||
// Once the job has finished, set history status to 1. | ||
$this->history->status = JobStatus::SUCCESS; | ||
$this->history->save(); | ||
} | ||
|
||
public function failed(\Throwable $th): void | ||
{ | ||
$this->history->status = JobStatus::FAILURE; | ||
$this->history->save(); | ||
|
||
if ($th->getCode() === 999) { | ||
$this->release(); | ||
} else { | ||
Log::error(__LINE__ . ':' . __FILE__ . ' ' . $th->getMessage(), $th->getTrace()); | ||
} | ||
} | ||
|
||
/** | ||
* If we have a live partner, then we also upload it. | ||
*/ | ||
private function handleVideoPartner(): void | ||
{ | ||
if ($this->variant->type !== SizeVariantType::ORIGINAL) { | ||
return; | ||
} | ||
|
||
$photo = Photo::query()->where('id', '=', $this->variant->photo_id)->first(); | ||
|
||
Storage::disk(StorageDiskType::S3->value)->writeStream( | ||
$photo->live_photo_short_path, | ||
Storage::disk(StorageDiskType::LOCAL->value)->readStream($photo->live_photo_short_path) | ||
); | ||
|
||
Storage::disk(StorageDiskType::LOCAL->value)->delete($photo->live_photo_short_path); | ||
} | ||
} |
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