koel/app/Services/DownloadService.php

57 lines
1.5 KiB
PHP
Raw Normal View History

2016-06-02 17:53:26 +00:00
<?php
namespace App\Services;
2024-04-18 17:20:14 +00:00
use App\Enums\SongStorageType;
2016-06-04 17:10:29 +00:00
use App\Models\Song;
2017-06-04 01:12:08 +00:00
use App\Models\SongZipArchive;
use App\Services\SongStorages\CloudStorage;
use App\Services\SongStorages\DropboxStorage;
use App\Services\SongStorages\S3CompatibleStorage;
2021-06-05 10:47:56 +00:00
use Illuminate\Support\Collection;
2024-01-07 12:43:10 +00:00
use Illuminate\Support\Facades\File;
2016-06-02 17:53:26 +00:00
2018-08-18 12:27:17 +00:00
class DownloadService
2016-06-02 17:53:26 +00:00
{
public function getDownloadablePath(Collection $songs): ?string
2016-06-02 17:53:26 +00:00
{
2024-01-07 12:43:10 +00:00
if ($songs->count() === 1) {
2024-01-10 23:11:45 +00:00
return $this->getLocalPath($songs->first());
2016-06-02 17:53:26 +00:00
}
2018-08-18 10:35:42 +00:00
2024-01-07 12:43:10 +00:00
return (new SongZipArchive())
->addSongs($songs)
->finish()
->getPath();
2016-06-02 17:53:26 +00:00
}
public function getLocalPath(Song $song): ?string
2016-06-02 17:53:26 +00:00
{
2024-04-18 17:20:14 +00:00
if (!$song->storage->supported()) {
return null;
}
2024-04-18 17:20:14 +00:00
if ($song->storage === SongStorageType::LOCAL) {
return File::exists($song->path) ? $song->path : null;
}
2024-04-18 17:20:14 +00:00
switch ($song->storage) {
case SongStorageType::DROPBOX:
/** @var CloudStorage $cloudStorage */
$cloudStorage = app(DropboxStorage::class);
break;
2024-04-18 17:20:14 +00:00
case SongStorageType::S3:
case SongStorageType::S3_LAMBDA:
/** @var CloudStorage $cloudStorage */
$cloudStorage = app(S3CompatibleStorage::class);
break;
default:
return null;
}
return $cloudStorage->copyToLocal($song);
2016-06-02 17:53:26 +00:00
}
}