2024-02-05 12:50:06 +01:00
|
|
|
<?php
|
|
|
|
|
2024-02-24 01:36:02 +07:00
|
|
|
namespace App\Services\SongStorages;
|
2024-02-05 12:50:06 +01:00
|
|
|
|
2024-02-05 22:17:41 +01:00
|
|
|
use App\Models\Song;
|
2024-02-05 12:50:06 +01:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Services\FileScanner;
|
2024-04-26 15:35:26 +02:00
|
|
|
use App\Services\SongStorages\Concerns\ScansUploadedFile;
|
2024-02-05 12:50:06 +01:00
|
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Symfony\Component\Uid\Ulid;
|
|
|
|
|
2024-02-05 14:27:17 +01:00
|
|
|
abstract class CloudStorage extends SongStorage
|
2024-02-05 12:50:06 +01:00
|
|
|
{
|
2024-04-26 15:35:26 +02:00
|
|
|
use ScansUploadedFile;
|
2024-02-05 12:50:06 +01:00
|
|
|
|
2024-04-26 15:35:26 +02:00
|
|
|
public function __construct(protected FileScanner $scanner)
|
2024-02-05 12:50:06 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-02-05 22:17:41 +01:00
|
|
|
public function copyToLocal(Song $song): string
|
2024-02-05 12:50:06 +01:00
|
|
|
{
|
2024-04-05 00:20:42 +02:00
|
|
|
self::assertSupported();
|
|
|
|
|
2024-02-05 22:17:41 +01:00
|
|
|
$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'koel_tmp';
|
|
|
|
File::ensureDirectoryExists($tmpDir);
|
|
|
|
|
|
|
|
$publicUrl = $this->getSongPresignedUrl($song);
|
|
|
|
$localPath = $tmpDir . DIRECTORY_SEPARATOR . basename($song->storage_metadata->getPath());
|
|
|
|
|
|
|
|
File::copy($publicUrl, $localPath);
|
|
|
|
|
|
|
|
return $localPath;
|
2024-02-05 12:50:06 +01:00
|
|
|
}
|
2024-02-05 14:27:17 +01:00
|
|
|
|
2024-02-05 22:17:41 +01:00
|
|
|
protected function generateStorageKey(string $filename, User $uploader): string
|
2024-02-05 14:27:17 +01:00
|
|
|
{
|
2024-02-05 22:17:41 +01:00
|
|
|
return sprintf('%s__%s__%s', $uploader->id, Str::lower(Ulid::generate()), $filename);
|
2024-02-05 14:27:17 +01:00
|
|
|
}
|
2024-02-05 22:17:41 +01:00
|
|
|
|
|
|
|
abstract public function getSongPresignedUrl(Song $song): string;
|
2024-02-05 12:50:06 +01:00
|
|
|
}
|