mirror of
https://github.com/koel/koel
synced 2024-11-10 06:34:14 +00:00
Fix download issues
This commit is contained in:
parent
f454850e6f
commit
168f70481c
3 changed files with 106 additions and 129 deletions
|
@ -6,6 +6,7 @@ use App\Facades\Download;
|
|||
use Exception;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use RuntimeException;
|
||||
use ZipArchive;
|
||||
|
||||
class SongZipArchive
|
||||
|
@ -33,22 +34,26 @@ class SongZipArchive
|
|||
/**
|
||||
* @param string $path
|
||||
*
|
||||
* @throws Exception
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function __construct($path = '')
|
||||
{
|
||||
if (!class_exists('ZipArchive')) {
|
||||
throw new Exception('Downloading multiple files requires ZipArchive module.');
|
||||
throw new RuntimeException('Downloading multiple files requires ZipArchive module.');
|
||||
}
|
||||
|
||||
// We use system's temp dir instead of storage_path() here, so that the generated files
|
||||
// can be cleaned up automatically after server reboot.
|
||||
$this->path = $path ?: $path = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'koel-download-'.uniqid().'.zip';
|
||||
if ($path) {
|
||||
$this->path = $path;
|
||||
} else {
|
||||
// We use system's temp dir instead of storage_path() here, so that the generated files
|
||||
// can be cleaned up automatically after server reboot.
|
||||
$this->path = sprintf('%s%skoel-download-%s.zip', sys_get_temp_dir(), DIRECTORY_SEPARATOR, uniqid());
|
||||
}
|
||||
|
||||
$this->archive = new ZipArchive();
|
||||
|
||||
if ($this->archive->open($this->path, ZipArchive::CREATE) !== true) {
|
||||
throw new Exception('Cannot create zip file.');
|
||||
throw new RuntimeException('Cannot create zip file.');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -61,9 +66,7 @@ class SongZipArchive
|
|||
*/
|
||||
public function addSongs(Collection $songs)
|
||||
{
|
||||
$songs->each(function ($song) {
|
||||
$this->addSong($song);
|
||||
});
|
||||
$songs->each([$this, 'addSong']);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
|
@ -7,8 +7,8 @@ use App\Models\Artist;
|
|||
use App\Models\Playlist;
|
||||
use App\Models\Song;
|
||||
use App\Models\SongZipArchive;
|
||||
use Exception;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class Download
|
||||
{
|
||||
|
@ -17,25 +17,26 @@ class Download
|
|||
*
|
||||
* @param Song|Collection<Song>|Album|Artist|Playlist $mixed
|
||||
*
|
||||
* @throws Exception
|
||||
* @throws InvalidArgumentException
|
||||
*
|
||||
* @return string Full path to the generated archive
|
||||
*/
|
||||
public function from($mixed)
|
||||
{
|
||||
if ($mixed instanceof Song) {
|
||||
return $this->fromSong($mixed);
|
||||
} elseif ($mixed instanceof Collection) {
|
||||
return $this->fromMultipleSongs($mixed);
|
||||
} elseif ($mixed instanceof Album) {
|
||||
return $this->fromAlbum($mixed);
|
||||
} elseif ($mixed instanceof Artist) {
|
||||
return $this->fromArtist($mixed);
|
||||
} elseif ($mixed instanceof Playlist) {
|
||||
return $this->fromPlaylist($mixed);
|
||||
} else {
|
||||
throw new Exception('Unsupported download type.');
|
||||
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);
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('Unsupported download type.');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -50,41 +51,22 @@ class Download
|
|||
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(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.basename($s3Params['key']);
|
||||
$url = $song->getObjectStoragePublicUrl();
|
||||
|
||||
abort_unless($url, 404);
|
||||
|
||||
$localPath = sys_get_temp_dir().DIRECTORY_SEPARATOR.basename($s3Params['key']);
|
||||
|
||||
// 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;
|
||||
abort_unless(file_exists($localPath), 404);
|
||||
}
|
||||
|
||||
// The BinaryFileResponse factory only accept ASCII-only file names.
|
||||
if (ctype_print($localPath)) {
|
||||
return $localPath;
|
||||
}
|
||||
|
||||
// For those with high-byte characters in names, we copy it into a safe name
|
||||
// as a workaround.
|
||||
$newPath = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR)
|
||||
.DIRECTORY_SEPARATOR
|
||||
.utf8_decode(basename($song->path));
|
||||
|
||||
if ($s3Params) {
|
||||
// If the file is downloaded from S3, we rename it directly.
|
||||
// This will save us some disk space.
|
||||
rename($localPath, $newPath);
|
||||
} else {
|
||||
// Else we copy it to another file to not mess up the original one.
|
||||
copy($localPath, $newPath);
|
||||
}
|
||||
|
||||
return $newPath;
|
||||
return $localPath;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -92,8 +74,6 @@ class Download
|
|||
*
|
||||
* @param Collection $songs
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function fromMultipleSongs(Collection $songs)
|
||||
|
@ -111,8 +91,6 @@ class Download
|
|||
/**
|
||||
* @param Playlist $playlist
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function fromPlaylist(Playlist $playlist)
|
||||
|
@ -123,8 +101,6 @@ class Download
|
|||
/**
|
||||
* @param Album $album
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function fromAlbum(Album $album)
|
||||
|
@ -135,8 +111,6 @@ class Download
|
|||
/**
|
||||
* @param Artist $artist
|
||||
*
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function fromArtist(Artist $artist)
|
||||
|
|
148
composer.json
148
composer.json
|
@ -1,77 +1,77 @@
|
|||
{
|
||||
"name": "phanan/koel",
|
||||
"description": "Personal audio streaming service that works.",
|
||||
"keywords": ["audio", "stream", "mp3"],
|
||||
"license": "MIT",
|
||||
"type": "project",
|
||||
"require": {
|
||||
"php": ">=5.6.4",
|
||||
"laravel/framework": "5.4.*",
|
||||
"james-heinrich/getid3": "^1.9",
|
||||
"guzzlehttp/guzzle": "^6.1",
|
||||
"tymon/jwt-auth": "^0.5.6",
|
||||
"aws/aws-sdk-php-laravel": "^3.1",
|
||||
"pusher/pusher-php-server": "^2.6",
|
||||
"predis/predis": "~1.0",
|
||||
"doctrine/dbal": "^2.5",
|
||||
"jackiedo/dotenv-editor": "^1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"fzaninotto/faker": "~1.4",
|
||||
"mockery/mockery": "~1.0",
|
||||
"phpunit/phpunit": "~5.7",
|
||||
"symfony/css-selector": "2.8.*|3.0.*",
|
||||
"symfony/dom-crawler": "^3.2",
|
||||
"facebook/webdriver": "^1.2",
|
||||
"barryvdh/laravel-ide-helper": "^2.1",
|
||||
"laravel/tinker": "^1.0",
|
||||
"laravel/browser-kit-testing": "^1.0",
|
||||
"codeclimate/php-test-reporter": "^0.4.4",
|
||||
"mikey179/vfsStream": "^1.6"
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
"database"
|
||||
],
|
||||
"psr-4": {
|
||||
"App\\": "app/",
|
||||
"Tests\\": "tests/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"classmap": [
|
||||
"tests/TestCase.php",
|
||||
"tests/e2e"
|
||||
]
|
||||
},
|
||||
"scripts": {
|
||||
"post-install-cmd": [
|
||||
"php artisan clear-compiled",
|
||||
"php artisan optimize",
|
||||
"php artisan cache:clear",
|
||||
"php -r \"if (!file_exists('.env')) copy('.env.example', '.env');\""
|
||||
],
|
||||
"pre-update-cmd": [
|
||||
"php artisan clear-compiled"
|
||||
],
|
||||
"post-update-cmd": [
|
||||
"php artisan optimize",
|
||||
"php artisan cache:clear"
|
||||
],
|
||||
"post-root-package-install": [
|
||||
"php -r \"copy('.env.example', '.env');\""
|
||||
],
|
||||
"post-create-project-cmd": [
|
||||
"php artisan key:generate"
|
||||
],
|
||||
"test": [
|
||||
"phpunit"
|
||||
]
|
||||
},
|
||||
"config": {
|
||||
"preferred-install": "dist",
|
||||
"platform": {
|
||||
"php": "5.6.31"
|
||||
}
|
||||
"name": "phanan/koel",
|
||||
"description": "Personal audio streaming service that works.",
|
||||
"keywords": ["audio", "stream", "mp3"],
|
||||
"license": "MIT",
|
||||
"type": "project",
|
||||
"require": {
|
||||
"php": ">=5.6.4",
|
||||
"laravel/framework": "5.4.*",
|
||||
"james-heinrich/getid3": "^1.9",
|
||||
"guzzlehttp/guzzle": "^6.1",
|
||||
"tymon/jwt-auth": "^0.5.6",
|
||||
"aws/aws-sdk-php-laravel": "^3.1",
|
||||
"pusher/pusher-php-server": "^2.6",
|
||||
"predis/predis": "~1.0",
|
||||
"doctrine/dbal": "^2.5",
|
||||
"jackiedo/dotenv-editor": "^1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"fzaninotto/faker": "~1.4",
|
||||
"mockery/mockery": "~1.0",
|
||||
"phpunit/phpunit": "~5.7",
|
||||
"symfony/css-selector": "2.8.*|3.0.*",
|
||||
"symfony/dom-crawler": "^3.2",
|
||||
"facebook/webdriver": "^1.2",
|
||||
"barryvdh/laravel-ide-helper": "^2.1",
|
||||
"laravel/tinker": "^1.0",
|
||||
"laravel/browser-kit-testing": "^1.0",
|
||||
"codeclimate/php-test-reporter": "^0.4.4",
|
||||
"mikey179/vfsStream": "^1.6"
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
"database"
|
||||
],
|
||||
"psr-4": {
|
||||
"App\\": "app/",
|
||||
"Tests\\": "tests/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"classmap": [
|
||||
"tests/TestCase.php",
|
||||
"tests/e2e"
|
||||
]
|
||||
},
|
||||
"scripts": {
|
||||
"post-install-cmd": [
|
||||
"php artisan clear-compiled",
|
||||
"php artisan optimize",
|
||||
"php artisan cache:clear",
|
||||
"php -r \"if (!file_exists('.env')) copy('.env.example', '.env');\""
|
||||
],
|
||||
"pre-update-cmd": [
|
||||
"php artisan clear-compiled"
|
||||
],
|
||||
"post-update-cmd": [
|
||||
"php artisan optimize",
|
||||
"php artisan cache:clear"
|
||||
],
|
||||
"post-root-package-install": [
|
||||
"php -r \"copy('.env.example', '.env');\""
|
||||
],
|
||||
"post-create-project-cmd": [
|
||||
"php artisan key:generate"
|
||||
],
|
||||
"test": [
|
||||
"phpunit"
|
||||
]
|
||||
},
|
||||
"config": {
|
||||
"preferred-install": "dist",
|
||||
"platform": {
|
||||
"php": "5.6.31"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue