2024-02-04 20:31:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services\SongStorage;
|
|
|
|
|
2024-02-05 13:27:17 +00:00
|
|
|
use App\Facades\License;
|
2024-02-04 20:31:01 +00:00
|
|
|
use App\Models\Song;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Services\FileScanner;
|
|
|
|
use Illuminate\Http\UploadedFile;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
2024-02-05 11:50:06 +00:00
|
|
|
class S3CompatibleStorage extends CloudStorage
|
2024-02-04 20:31:01 +00:00
|
|
|
{
|
2024-02-05 11:50:06 +00:00
|
|
|
public function __construct(protected FileScanner $scanner, private string $bucket)
|
2024-02-04 20:31:01 +00:00
|
|
|
{
|
2024-02-05 11:50:06 +00:00
|
|
|
parent::__construct($scanner);
|
2024-02-04 20:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function storeUploadedFile(UploadedFile $file, User $uploader): Song
|
|
|
|
{
|
2024-02-05 11:50:06 +00:00
|
|
|
return DB::transaction(function () use ($file, $uploader): Song {
|
|
|
|
$result = $this->scanUploadedFile($file, $uploader);
|
2024-02-04 20:31:01 +00:00
|
|
|
$song = $this->scanner->getSong();
|
2024-02-05 11:50:06 +00:00
|
|
|
$key = $this->generateStorageKey($file->getClientOriginalName(), $uploader);
|
2024-02-04 20:31:01 +00:00
|
|
|
|
2024-02-05 11:50:06 +00:00
|
|
|
Storage::disk('s3')->put($key, File::get($result->path));
|
2024-02-05 13:27:17 +00:00
|
|
|
$song->update(['path' => "s3+://$this->bucket/$key"]);
|
2024-02-04 20:31:01 +00:00
|
|
|
|
2024-02-05 11:50:06 +00:00
|
|
|
File::delete($result->path);
|
2024-02-04 20:31:01 +00:00
|
|
|
|
|
|
|
return $song;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSongPresignedUrl(Song $song): string
|
|
|
|
{
|
|
|
|
return Storage::disk('s3')->temporaryUrl($song->storage_metadata->getPath(), now()->addHour());
|
|
|
|
}
|
2024-02-05 13:27:17 +00:00
|
|
|
|
|
|
|
public function supported(): bool
|
|
|
|
{
|
|
|
|
return License::isPlus();
|
|
|
|
}
|
2024-02-04 20:31:01 +00:00
|
|
|
}
|