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;
|
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
|
|
|
use Log;
|
2016-06-04 11:20:11 +00:00
|
|
|
use ZipArchive;
|
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
|
|
|
|
*
|
|
|
|
* @return string Full path to the generated archive
|
2016-08-03 10:42:11 +00:00
|
|
|
* @throws Exception
|
2016-06-02 17:53:26 +00:00
|
|
|
*/
|
|
|
|
public function from($mixed)
|
|
|
|
{
|
|
|
|
if (is_a($mixed, Song::class)) {
|
|
|
|
return $this->fromSong($mixed);
|
|
|
|
} elseif (is_a($mixed, Collection::class)) {
|
|
|
|
return $this->fromMultipleSongs($mixed);
|
|
|
|
} elseif (is_a($mixed, Album::class)) {
|
|
|
|
return $this->fromAlbum($mixed);
|
|
|
|
} elseif (is_a($mixed, Artist::class)) {
|
|
|
|
return $this->fromArtist($mixed);
|
|
|
|
} elseif (is_a($mixed, Playlist::class)) {
|
|
|
|
return $this->fromPlaylist($mixed);
|
|
|
|
} else {
|
|
|
|
throw new Exception('Unsupport download type.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 07:26:39 +00:00
|
|
|
/**
|
|
|
|
* Generate the downloadable path for a song.
|
|
|
|
*
|
|
|
|
* @param Song $song
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-06-02 17:53:26 +00:00
|
|
|
protected function fromSong(Song $song)
|
|
|
|
{
|
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.
|
|
|
|
$localPath = rtrim(sys_get_temp_dir(), '/').'/'.basename($s3Params['key']);
|
|
|
|
$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.
|
2016-07-11 07:26:39 +00:00
|
|
|
$newPath = rtrim(sys_get_temp_dir(), '/').'/'.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
|
|
|
|
*
|
|
|
|
* @return string
|
2016-08-03 10:42:11 +00:00
|
|
|
* @throws Exception
|
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());
|
|
|
|
}
|
|
|
|
|
2016-06-04 11:20:11 +00:00
|
|
|
if (!class_exists('ZipArchive')) {
|
2016-06-02 17:53:26 +00:00
|
|
|
throw new Exception('Downloading multiple files requires ZipArchive module.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start gathering the songs into a zip file.
|
2016-06-04 11:20:11 +00:00
|
|
|
$zip = new ZipArchive();
|
2016-06-02 17:53:26 +00:00
|
|
|
|
2016-07-09 05:19:58 +00:00
|
|
|
// We use system's temp dir instead of storage_path() here, so that the generated files
|
2016-06-02 17:53:26 +00:00
|
|
|
// can be cleaned up automatically after server reboot.
|
|
|
|
$filename = rtrim(sys_get_temp_dir(), '/').'/koel-download-'.uniqid().'.zip';
|
2016-06-04 11:20:11 +00:00
|
|
|
if ($zip->open($filename, ZipArchive::CREATE) !== true) {
|
2016-06-02 17:53:26 +00:00
|
|
|
throw new Exception('Cannot create zip file.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$localNames = [
|
2016-06-04 11:20:11 +00:00
|
|
|
// The data will follow this format:
|
2016-06-02 17:53:26 +00:00
|
|
|
// 'duplicated-name.mp3' => currentFileIndex
|
|
|
|
];
|
|
|
|
|
|
|
|
$songs->each(function ($s) use ($zip, &$localNames) {
|
|
|
|
try {
|
2016-07-11 07:26:39 +00:00
|
|
|
$path = $this->fromSong($s);
|
|
|
|
|
2016-06-02 17:53:26 +00:00
|
|
|
// We add all files into the zip archive as a flat structure.
|
|
|
|
// As a result, there can be duplicate file names.
|
|
|
|
// The following several lines are to make sure each file name is unique.
|
2016-07-11 07:26:39 +00:00
|
|
|
$name = basename($path);
|
2016-06-02 17:53:26 +00:00
|
|
|
if (array_key_exists($name, $localNames)) {
|
2016-06-04 11:20:11 +00:00
|
|
|
++$localNames[$name];
|
2016-06-02 17:53:26 +00:00
|
|
|
$parts = explode('.', $name);
|
|
|
|
$ext = $parts[count($parts) - 1];
|
|
|
|
$parts[count($parts) - 1] = $localNames[$name].".$ext";
|
|
|
|
$name = implode('.', $parts);
|
|
|
|
} else {
|
|
|
|
$localNames[$name] = 1;
|
|
|
|
}
|
|
|
|
|
2016-07-11 07:26:39 +00:00
|
|
|
$zip->addFile($path, $name);
|
2016-06-02 17:53:26 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::error($e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$zip->close();
|
|
|
|
|
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function fromPlaylist(Playlist $playlist)
|
|
|
|
{
|
|
|
|
return $this->fromMultipleSongs($playlist->songs);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
protected function fromArtist(Artist $artist)
|
|
|
|
{
|
2016-06-04 16:56:38 +00:00
|
|
|
// Don't forget the contributed songs.
|
2016-06-04 11:57:27 +00:00
|
|
|
$songs = $artist->songs->merge($artist->getContributedSongs());
|
2016-06-02 17:53:26 +00:00
|
|
|
|
|
|
|
return $this->fromMultipleSongs($songs);
|
|
|
|
}
|
|
|
|
}
|