koel/app/Models/SupportsS3.php

43 lines
960 B
PHP
Raw Normal View History

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
2022-07-18 11:00:37 +00:00
use Illuminate\Database\Eloquent\Casts\Attribute;
/**
2022-07-18 11:00:37 +00:00
* @property array<string>|null $s3_params
*
* @method static Builder hostedOnS3()
*/
trait SupportsS3
{
/**
* Get the bucket and key name of an S3 object.
*
* @return array<string>|null
*/
2022-07-18 11:00:37 +00:00
protected function s3Params(): Attribute
{
2022-07-18 11:00:37 +00:00
return Attribute::get(function (): ?array {
if (!preg_match('/^s3:\\/\\/(.*)/', $this->path, $matches)) {
return null;
}
2022-07-18 11:00:37 +00:00
[$bucket, $key] = explode('/', $matches[1], 2);
2022-07-18 11:00:37 +00:00
return compact('bucket', 'key');
});
}
public static function getPathFromS3BucketAndKey(string $bucket, string $key): string
{
return "s3://$bucket/$key";
}
public function scopeHostedOnS3(Builder $query): Builder
{
return $query->where('path', 'LIKE', 's3://%');
}
}