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;
|
2024-02-23 18:36:02 +00:00
|
|
|
use App\Services\SongStorages\CloudStorage;
|
|
|
|
use App\Services\SongStorages\DropboxStorage;
|
|
|
|
use App\Services\SongStorages\S3CompatibleStorage;
|
2024-02-05 21:17:41 +00:00
|
|
|
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
|
|
|
{
|
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-02-05 21:17:41 +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;
|
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
|
|
|
}
|
|
|
|
}
|