2024-02-04 20:31:01 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-23 18:36:02 +00:00
|
|
|
namespace App\Services\SongStorages;
|
2024-02-04 20:31:01 +00:00
|
|
|
|
2024-04-18 17:20:14 +00:00
|
|
|
use App\Enums\SongStorageType;
|
2024-02-04 20:31:01 +00:00
|
|
|
use App\Models\Song;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Services\FileScanner;
|
2024-04-26 13:35:26 +00:00
|
|
|
use App\Services\SongStorages\Concerns\DeletesUsingFilesystem;
|
2024-02-04 20:31:01 +00:00
|
|
|
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-04-26 13:35:26 +00:00
|
|
|
use DeletesUsingFilesystem;
|
|
|
|
|
2024-04-18 14:36:28 +00:00
|
|
|
public function __construct(protected FileScanner $scanner, private readonly 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-04-04 22:20:42 +00:00
|
|
|
self::assertSupported();
|
|
|
|
|
2024-02-05 11:50:06 +00:00
|
|
|
return DB::transaction(function () use ($file, $uploader): Song {
|
2024-04-26 13:35:26 +00:00
|
|
|
$result = $this->scanUploadedFile($this->scanner, $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-23 16:03:54 +00:00
|
|
|
|
2024-02-05 21:17:41 +00:00
|
|
|
$song->update([
|
|
|
|
'path' => "s3://$this->bucket/$key",
|
2024-04-18 17:20:14 +00:00
|
|
|
'storage' => SongStorageType::S3,
|
2024-02-05 21:17:41 +00:00
|
|
|
]);
|
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
|
|
|
|
{
|
2024-04-04 22:20:42 +00:00
|
|
|
self::assertSupported();
|
|
|
|
|
2024-02-04 20:31:01 +00:00
|
|
|
return Storage::disk('s3')->temporaryUrl($song->storage_metadata->getPath(), now()->addHour());
|
|
|
|
}
|
2024-02-05 13:27:17 +00:00
|
|
|
|
2024-02-23 16:03:54 +00:00
|
|
|
public function delete(Song $song, bool $backup = false): void
|
|
|
|
{
|
2024-04-04 22:20:42 +00:00
|
|
|
self::assertSupported();
|
2024-04-26 13:35:26 +00:00
|
|
|
$this->deleteUsingFileSystem(Storage::disk('s3'), $song, $backup);
|
2024-02-23 16:03:54 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 09:11:15 +00:00
|
|
|
public function testSetup(): void
|
|
|
|
{
|
|
|
|
Storage::disk('s3')->put('test.txt', 'Koel test file');
|
|
|
|
Storage::disk('s3')->delete('test.txt');
|
|
|
|
}
|
|
|
|
|
2024-04-26 13:35:26 +00:00
|
|
|
protected function getStorageType(): SongStorageType
|
2024-02-05 13:27:17 +00:00
|
|
|
{
|
2024-04-26 13:35:26 +00:00
|
|
|
return SongStorageType::S3;
|
2024-02-05 13:27:17 +00:00
|
|
|
}
|
2024-02-04 20:31:01 +00:00
|
|
|
}
|