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;
|
2016-06-02 17:53:26 +00:00
|
|
|
use Exception;
|
2016-06-04 13:42:12 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2016-06-02 17:53:26 +00:00
|
|
|
|
|
|
|
class Download
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Generic method to generate a download archive from various source types.
|
|
|
|
*
|
|
|
|
* @param Song|Collection<Song>|Album|Artist|Playlist $mixed
|
|
|
|
*
|
2016-08-03 10:42:11 +00:00
|
|
|
* @throws Exception
|
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)
|
|
|
|
{
|
2017-04-29 02:55:41 +00:00
|
|
|
if ($mixed instanceof Song) {
|
2016-06-02 17:53:26 +00:00
|
|
|
return $this->fromSong($mixed);
|
2017-05-29 11:14:53 +00:00
|
|
|
} elseif ($mixed instanceof Collection) {
|
2016-06-02 17:53:26 +00:00
|
|
|
return $this->fromMultipleSongs($mixed);
|
2017-04-29 02:55:41 +00:00
|
|
|
} elseif ($mixed instanceof Album) {
|
2016-06-02 17:53:26 +00:00
|
|
|
return $this->fromAlbum($mixed);
|
2017-04-29 02:55:41 +00:00
|
|
|
} elseif ($mixed instanceof Artist) {
|
2016-06-02 17:53:26 +00:00
|
|
|
return $this->fromArtist($mixed);
|
2017-04-29 02:55:41 +00:00
|
|
|
} elseif ($mixed instanceof Playlist) {
|
2016-06-02 17:53:26 +00:00
|
|
|
return $this->fromPlaylist($mixed);
|
|
|
|
} else {
|
2017-12-09 18:34:27 +00:00
|
|
|
throw new Exception('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.
|
2017-06-04 01:12:08 +00:00
|
|
|
$localPath = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.basename($s3Params['key']);
|
2016-07-11 07:26:39 +00:00
|
|
|
$url = $song->getObjectStoragePublicUrl();
|
2016-06-05 17:23:03 +00:00
|
|
|
|
2016-07-11 07:26:39 +00:00
|
|
|
abort_unless($url, 404);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
abort_unless(file_exists($song->path), 404);
|
|
|
|
$localPath = $song->path;
|
2016-06-05 17:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The BinaryFileResponse factory only accept ASCII-only file names.
|
2016-07-11 07:26:39 +00:00
|
|
|
if (ctype_print($localPath)) {
|
|
|
|
return $localPath;
|
|
|
|
}
|
|
|
|
|
2016-06-05 17:23:03 +00:00
|
|
|
// For those with high-byte characters in names, we copy it into a safe name
|
|
|
|
// as a workaround.
|
2017-12-09 18:34:27 +00:00
|
|
|
$newPath = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR)
|
|
|
|
.DIRECTORY_SEPARATOR
|
|
|
|
.utf8_decode(basename($song->path));
|
2016-06-05 17:23:03 +00:00
|
|
|
|
2016-07-11 07:26:39 +00:00
|
|
|
if ($s3Params) {
|
|
|
|
// If the file is downloaded from S3, we rename it directly.
|
2016-08-03 10:42:11 +00:00
|
|
|
// This will save us some disk space.
|
2016-07-11 07:26:39 +00:00
|
|
|
rename($localPath, $newPath);
|
|
|
|
} else {
|
|
|
|
// Else we copy it to another file to not mess up the original one.
|
|
|
|
copy($localPath, $newPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $newPath;
|
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:11 +00:00
|
|
|
* @throws Exception
|
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
|
|
|
}
|
|
|
|
|
2017-12-09 18:34:27 +00:00
|
|
|
/**
|
|
|
|
* @param Playlist $playlist
|
|
|
|
*
|
|
|
|
* @throws Exception
|
2017-12-09 20:10:55 +00:00
|
|
|
*
|
|
|
|
* @return string
|
2017-12-09 18:34:27 +00:00
|
|
|
*/
|
2016-06-02 17:53:26 +00:00
|
|
|
protected function fromPlaylist(Playlist $playlist)
|
|
|
|
{
|
|
|
|
return $this->fromMultipleSongs($playlist->songs);
|
|
|
|
}
|
|
|
|
|
2017-12-09 18:34:27 +00:00
|
|
|
/**
|
|
|
|
* @param Album $album
|
|
|
|
*
|
|
|
|
* @throws Exception
|
2017-12-09 20:10:55 +00:00
|
|
|
*
|
|
|
|
* @return string
|
2017-12-09 18:34:27 +00:00
|
|
|
*/
|
2016-06-02 17:53:26 +00:00
|
|
|
protected function fromAlbum(Album $album)
|
|
|
|
{
|
2016-06-04 11:20:11 +00:00
|
|
|
return $this->fromMultipleSongs($album->songs);
|
2016-06-02 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 18:34:27 +00:00
|
|
|
/**
|
|
|
|
* @param Artist $artist
|
|
|
|
*
|
|
|
|
* @throws Exception
|
2017-12-09 20:10:55 +00:00
|
|
|
*
|
|
|
|
* @return string
|
2017-12-09 18:34:27 +00:00
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
}
|