koel/app/Services/DownloadService.php

98 lines
2.7 KiB
PHP
Raw Normal View History

2016-06-02 17:53:26 +00:00
<?php
namespace App\Services;
use App\Models\Album;
use App\Models\Artist;
use App\Models\Playlist;
2016-06-04 17:10:29 +00:00
use App\Models\Song;
2017-06-04 01:12:08 +00:00
use App\Models\SongZipArchive;
2019-06-16 21:12:56 +00:00
use Illuminate\Database\Eloquent\Collection;
2018-08-18 10:35:42 +00:00
use InvalidArgumentException;
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
{
2018-08-29 04:06:17 +00:00
private $s3Service;
public function __construct(S3Service $s3Service)
{
$this->s3Service = $s3Service;
}
2016-06-02 17:53:26 +00:00
/**
* Generic method to generate a download archive from various source types.
*
* @param Song|Collection|Album|Artist|Playlist $mixed
2016-06-02 17:53:26 +00:00
*
2018-08-18 10:35:42 +00:00
* @throws InvalidArgumentException
2016-08-03 10:42:39 +00:00
*
* @return string Full path to the generated archive
2016-06-02 17:53:26 +00:00
*/
2018-08-24 15:27:19 +00:00
public function from($mixed): string
2016-06-02 17:53:26 +00:00
{
2018-08-18 10:35:42 +00:00
switch (get_class($mixed)) {
case Song::class:
return $this->fromSong($mixed);
case Collection::class:
return $this->fromMultipleSongs($mixed);
case Album::class:
return $this->fromAlbum($mixed);
case Artist::class:
return $this->fromArtist($mixed);
case Playlist::class:
return $this->fromPlaylist($mixed);
2016-06-02 17:53:26 +00:00
}
2018-08-18 10:35:42 +00:00
throw new InvalidArgumentException('Unsupported download type.');
2016-06-02 17:53:26 +00:00
}
2018-08-24 15:27:19 +00:00
public function fromSong(Song $song): string
2016-06-02 17:53:26 +00:00
{
2016-07-11 07:26:39 +00:00
if ($s3Params = $song->s3_params) {
// The song is hosted on Amazon S3.
// We download it back to our local server first.
2018-08-29 04:06:17 +00:00
$url = $this->s3Service->getSongPublicUrl($song);
2016-07-11 07:26:39 +00:00
abort_unless($url, 404);
2018-08-18 10:35:42 +00:00
$localPath = sys_get_temp_dir().DIRECTORY_SEPARATOR.basename($s3Params['key']);
2016-07-11 07:26:39 +00:00
// The following function require allow_url_fopen to be ON.
// We're just assuming that to be the case here.
copy($url, $localPath);
} else {
// The song is hosted locally. Make sure the file exists.
$localPath = $song->path;
2018-08-18 10:35:42 +00:00
abort_unless(file_exists($localPath), 404);
}
2018-08-18 10:35:42 +00:00
return $localPath;
2016-06-02 17:53:26 +00:00
}
2018-08-24 15:27:19 +00:00
protected function fromMultipleSongs(Collection $songs): string
2016-06-02 17:53:26 +00:00
{
if ($songs->count() === 1) {
return $this->fromSong($songs->first());
}
2017-06-04 01:12:08 +00:00
return (new SongZipArchive())
->addSongs($songs)
->finish()
->getPath();
2016-06-02 17:53:26 +00:00
}
2018-08-24 15:27:19 +00:00
protected function fromPlaylist(Playlist $playlist): string
2016-06-02 17:53:26 +00:00
{
return $this->fromMultipleSongs($playlist->songs);
}
2018-08-24 15:27:19 +00:00
protected function fromAlbum(Album $album): string
2016-06-02 17:53:26 +00:00
{
return $this->fromMultipleSongs($album->songs);
2016-06-02 17:53:26 +00:00
}
2018-08-24 15:27:19 +00:00
protected function fromArtist(Artist $artist): string
2016-06-02 17:53:26 +00:00
{
2017-06-10 11:36:32 +00:00
return $this->fromMultipleSongs($artist->songs);
2016-06-02 17:53:26 +00:00
}
}