koel/app/Services/DownloadService.php

64 lines
1.6 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\DropboxStorage;
use App\Services\SongStorages\S3CompatibleStorage;
2024-04-26 13:35:26 +00:00
use App\Services\SongStorages\SftpStorage;
2024-05-19 05:49:42 +00:00
use App\Values\Podcast\EpisodePlayable;
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-05-19 05:49:42 +00:00
if ($song->isEpisode()) {
return EpisodePlayable::getForEpisode($song)->path;
2024-05-19 05:49:42 +00:00
}
2024-04-18 17:20:14 +00:00
if ($song->storage === SongStorageType::LOCAL) {
return File::exists($song->path) ? $song->path : null;
}
2024-04-26 13:35:26 +00:00
if ($song->storage === SongStorageType::SFTP) {
return app(SftpStorage::class)->copyToLocal($song);
}
2024-04-18 17:20:14 +00:00
switch ($song->storage) {
case SongStorageType::DROPBOX:
$cloudStorage = app(DropboxStorage::class);
break;
2024-04-18 17:20:14 +00:00
case SongStorageType::S3:
case SongStorageType::S3_LAMBDA:
$cloudStorage = app(S3CompatibleStorage::class);
break;
default:
return null;
}
return $cloudStorage->copyToLocal($song);
2016-06-02 17:53:26 +00:00
}
}