2024-02-05 11:50:06 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-23 18:36:02 +00:00
|
|
|
namespace App\Services\SongStorages;
|
2024-02-05 11:50:06 +00:00
|
|
|
|
|
|
|
use App\Exceptions\SongUploadFailedException;
|
2024-02-05 21:17:41 +00:00
|
|
|
use App\Models\Song;
|
2024-02-05 11:50:06 +00:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Services\FileScanner;
|
|
|
|
use App\Values\ScanConfiguration;
|
|
|
|
use App\Values\ScanResult;
|
|
|
|
use Illuminate\Http\UploadedFile;
|
|
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Symfony\Component\Uid\Ulid;
|
|
|
|
|
2024-02-05 13:27:17 +00:00
|
|
|
abstract class CloudStorage extends SongStorage
|
2024-02-05 11:50:06 +00:00
|
|
|
{
|
|
|
|
public function __construct(protected FileScanner $scanner)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function scanUploadedFile(UploadedFile $file, User $uploader): ScanResult
|
|
|
|
{
|
2024-04-04 22:20:42 +00:00
|
|
|
self::assertSupported();
|
|
|
|
|
2024-02-05 11:50:06 +00:00
|
|
|
// Can't scan the uploaded file directly, as it apparently causes some misbehavior during idv3 tag reading.
|
|
|
|
// Instead, we copy the file to the tmp directory and scan it from there.
|
2024-02-05 13:27:17 +00:00
|
|
|
$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'koel_tmp';
|
|
|
|
File::ensureDirectoryExists($tmpDir);
|
|
|
|
|
2024-02-05 11:50:06 +00:00
|
|
|
$tmpFile = $file->move($tmpDir, $file->getClientOriginalName());
|
|
|
|
|
|
|
|
$result = $this->scanner->setFile($tmpFile)
|
|
|
|
->scan(ScanConfiguration::make(
|
|
|
|
owner: $uploader,
|
|
|
|
makePublic: $uploader->preferences->makeUploadsPublic
|
|
|
|
));
|
|
|
|
|
|
|
|
throw_if($result->isError(), new SongUploadFailedException($result->error));
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2024-02-05 21:17:41 +00:00
|
|
|
public function copyToLocal(Song $song): string
|
2024-02-05 11:50:06 +00:00
|
|
|
{
|
2024-04-04 22:20:42 +00:00
|
|
|
self::assertSupported();
|
|
|
|
|
2024-02-05 21:17:41 +00: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 11:50:06 +00:00
|
|
|
}
|
2024-02-05 13:27:17 +00:00
|
|
|
|
2024-02-05 21:17:41 +00:00
|
|
|
protected function generateStorageKey(string $filename, User $uploader): string
|
2024-02-05 13:27:17 +00:00
|
|
|
{
|
2024-02-05 21:17:41 +00:00
|
|
|
return sprintf('%s__%s__%s', $uploader->id, Str::lower(Ulid::generate()), $filename);
|
2024-02-05 13:27:17 +00:00
|
|
|
}
|
2024-02-05 21:17:41 +00:00
|
|
|
|
|
|
|
abstract public function getSongPresignedUrl(Song $song): string;
|
2024-02-05 11:50:06 +00:00
|
|
|
}
|