koel/app/Services/DownloadService.php

56 lines
1.5 KiB
PHP
Raw Normal View History

2016-06-02 17:53:26 +00:00
<?php
namespace App\Services;
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;
use App\Values\SongStorageTypes;
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
{
if (!SongStorageTypes::supported($song->storage)) {
return null;
}
if (!$song->storage || $song->storage === SongStorageTypes::LOCAL) {
return File::exists($song->path) ? $song->path : null;
}
switch (true) {
case $song->storage === SongStorageTypes::DROPBOX:
/** @var CloudStorage $cloudStorage */
$cloudStorage = app(DropboxStorage::class);
break;
case $song->storage === SongStorageTypes::S3 || $song->storage === SongStorageTypes::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
}
}