koel/app/Services/DownloadService.php

121 lines
3 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;
2018-08-18 10:35:42 +00:00
use Illuminate\Database\Eloquent\Collection;
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
{
/**
* Generic method to generate a download archive from various source types.
*
* @param Song|Collection<Song>|Album|Artist|Playlist $mixed
*
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
*/
public function from($mixed)
{
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
}
2016-07-11 07:26:39 +00:00
/**
* Generate the downloadable path for a song.
*
* @param Song $song
*
* @return string
*/
2017-06-04 01:12:08 +00:00
public function fromSong(Song $song)
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.
$url = $song->getObjectStoragePublicUrl();
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
}
2016-07-11 07:26:39 +00:00
/**
* Generate a downloadable path of multiple songs in zip format.
*
* @param Collection $songs
*
2016-08-03 10:42:39 +00:00
* @return string
2016-07-11 07:26:39 +00:00
*/
2016-06-02 17:53:26 +00:00
protected function fromMultipleSongs(Collection $songs)
{
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
}
/**
* @param Playlist $playlist
*
2017-12-09 20:10:55 +00:00
* @return string
*/
2016-06-02 17:53:26 +00:00
protected function fromPlaylist(Playlist $playlist)
{
return $this->fromMultipleSongs($playlist->songs);
}
/**
* @param Album $album
*
2017-12-09 20:10:55 +00:00
* @return string
*/
2016-06-02 17:53:26 +00:00
protected function fromAlbum(Album $album)
{
return $this->fromMultipleSongs($album->songs);
2016-06-02 17:53:26 +00:00
}
/**
* @param Artist $artist
*
2017-12-09 20:10:55 +00:00
* @return string
*/
2016-06-02 17:53:26 +00:00
protected function fromArtist(Artist $artist)
{
2017-06-10 11:36:32 +00:00
return $this->fromMultipleSongs($artist->songs);
2016-06-02 17:53:26 +00:00
}
}