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;
|
2022-01-17 23:20:57 +00:00
|
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
2021-06-05 10:47:56 +00:00
|
|
|
use Illuminate\Support\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
|
|
|
{
|
2021-06-05 10:47:56 +00:00
|
|
|
private S3Service $s3Service;
|
2018-08-29 04:06:17 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*
|
2018-09-03 12:41:49 +00:00
|
|
|
* @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);
|
2020-12-22 20:11:22 +00:00
|
|
|
|
2018-08-18 10:35:42 +00:00
|
|
|
case Collection::class:
|
2022-01-17 23:20:57 +00:00
|
|
|
case EloquentCollection::class:
|
2018-08-18 10:35:42 +00:00
|
|
|
return $this->fromMultipleSongs($mixed);
|
2020-12-22 20:11:22 +00:00
|
|
|
|
2018-08-18 10:35:42 +00:00
|
|
|
case Album::class:
|
|
|
|
return $this->fromAlbum($mixed);
|
2020-12-22 20:11:22 +00:00
|
|
|
|
2018-08-18 10:35:42 +00:00
|
|
|
case Artist::class:
|
|
|
|
return $this->fromArtist($mixed);
|
2020-12-22 20:11:22 +00:00
|
|
|
|
2018-08-18 10:35:42 +00:00
|
|
|
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
|
|
|
{
|
2020-12-22 20:11:22 +00:00
|
|
|
if ($song->s3_params) {
|
2016-07-11 07:26:39 +00:00
|
|
|
// 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);
|
2021-06-05 10:47:56 +00:00
|
|
|
abort_unless((bool) $url, 404);
|
2016-07-11 07:26:39 +00:00
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
$localPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . basename($song->s3_params['key']);
|
2018-08-18 10:35:42 +00:00
|
|
|
|
2019-06-30 09:44:39 +00:00
|
|
|
// The following function requires allow_url_fopen to be ON.
|
2016-07-11 07:26:39 +00:00
|
|
|
// 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);
|
2016-06-05 17:23:03 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2016-06-04 11:20:11 +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
|
|
|
}
|
|
|
|
}
|