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;
|
2024-02-23 18:36:02 +00:00
|
|
|
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
|
|
|
{
|
2024-02-05 21:17:41 +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
|
|
|
}
|
|
|
|
|
2024-02-05 21:17:41 +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()) {
|
2024-02-05 21:17:41 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
if ($song->isEpisode()) {
|
2024-09-03 10:52:07 +00:00
|
|
|
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) {
|
2024-02-05 21:17:41 +00:00
|
|
|
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:
|
2024-02-05 21:17:41 +00:00
|
|
|
$cloudStorage = app(DropboxStorage::class);
|
|
|
|
break;
|
|
|
|
|
2024-04-18 17:20:14 +00:00
|
|
|
case SongStorageType::S3:
|
|
|
|
case SongStorageType::S3_LAMBDA:
|
2024-02-05 21:17:41 +00:00
|
|
|
$cloudStorage = app(S3CompatibleStorage::class);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return null;
|
2016-06-05 17:23:03 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 21:17:41 +00:00
|
|
|
return $cloudStorage->copyToLocal($song);
|
2016-06-02 17:53:26 +00:00
|
|
|
}
|
|
|
|
}
|