Fix download issues

This commit is contained in:
Phan An 2018-08-18 12:35:42 +02:00
parent f454850e6f
commit 168f70481c
3 changed files with 106 additions and 129 deletions

View file

@ -6,6 +6,7 @@ use App\Facades\Download;
use Exception; use Exception;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use RuntimeException;
use ZipArchive; use ZipArchive;
class SongZipArchive class SongZipArchive
@ -33,22 +34,26 @@ class SongZipArchive
/** /**
* @param string $path * @param string $path
* *
* @throws Exception * @throws RuntimeException
*/ */
public function __construct($path = '') public function __construct($path = '')
{ {
if (!class_exists('ZipArchive')) { 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 if ($path) {
// can be cleaned up automatically after server reboot. $this->path = $path;
$this->path = $path ?: $path = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'koel-download-'.uniqid().'.zip'; } 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(); $this->archive = new ZipArchive();
if ($this->archive->open($this->path, ZipArchive::CREATE) !== true) { 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) public function addSongs(Collection $songs)
{ {
$songs->each(function ($song) { $songs->each([$this, 'addSong']);
$this->addSong($song);
});
return $this; return $this;
} }

View file

@ -7,8 +7,8 @@ use App\Models\Artist;
use App\Models\Playlist; use App\Models\Playlist;
use App\Models\Song; use App\Models\Song;
use App\Models\SongZipArchive; use App\Models\SongZipArchive;
use Exception; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Collection; use InvalidArgumentException;
class Download class Download
{ {
@ -17,25 +17,26 @@ class Download
* *
* @param Song|Collection<Song>|Album|Artist|Playlist $mixed * @param Song|Collection<Song>|Album|Artist|Playlist $mixed
* *
* @throws Exception * @throws InvalidArgumentException
* *
* @return string Full path to the generated archive * @return string Full path to the generated archive
*/ */
public function from($mixed) public function from($mixed)
{ {
if ($mixed instanceof Song) { switch (get_class($mixed)) {
return $this->fromSong($mixed); case Song::class:
} elseif ($mixed instanceof Collection) { return $this->fromSong($mixed);
return $this->fromMultipleSongs($mixed); case Collection::class:
} elseif ($mixed instanceof Album) { return $this->fromMultipleSongs($mixed);
return $this->fromAlbum($mixed); case Album::class:
} elseif ($mixed instanceof Artist) { return $this->fromAlbum($mixed);
return $this->fromArtist($mixed); case Artist::class:
} elseif ($mixed instanceof Playlist) { return $this->fromArtist($mixed);
return $this->fromPlaylist($mixed); case Playlist::class:
} else { return $this->fromPlaylist($mixed);
throw new Exception('Unsupported download type.');
} }
throw new InvalidArgumentException('Unsupported download type.');
} }
/** /**
@ -50,41 +51,22 @@ class Download
if ($s3Params = $song->s3_params) { if ($s3Params = $song->s3_params) {
// The song is hosted on Amazon S3. // The song is hosted on Amazon S3.
// We download it back to our local server first. // 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(); $url = $song->getObjectStoragePublicUrl();
abort_unless($url, 404); abort_unless($url, 404);
$localPath = sys_get_temp_dir().DIRECTORY_SEPARATOR.basename($s3Params['key']);
// The following function require allow_url_fopen to be ON. // The following function require allow_url_fopen to be ON.
// We're just assuming that to be the case here. // We're just assuming that to be the case here.
copy($url, $localPath); copy($url, $localPath);
} else { } else {
// The song is hosted locally. Make sure the file exists. // The song is hosted locally. Make sure the file exists.
abort_unless(file_exists($song->path), 404);
$localPath = $song->path; $localPath = $song->path;
abort_unless(file_exists($localPath), 404);
} }
// The BinaryFileResponse factory only accept ASCII-only file names. return $localPath;
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;
} }
/** /**
@ -92,8 +74,6 @@ class Download
* *
* @param Collection $songs * @param Collection $songs
* *
* @throws Exception
*
* @return string * @return string
*/ */
protected function fromMultipleSongs(Collection $songs) protected function fromMultipleSongs(Collection $songs)
@ -111,8 +91,6 @@ class Download
/** /**
* @param Playlist $playlist * @param Playlist $playlist
* *
* @throws Exception
*
* @return string * @return string
*/ */
protected function fromPlaylist(Playlist $playlist) protected function fromPlaylist(Playlist $playlist)
@ -123,8 +101,6 @@ class Download
/** /**
* @param Album $album * @param Album $album
* *
* @throws Exception
*
* @return string * @return string
*/ */
protected function fromAlbum(Album $album) protected function fromAlbum(Album $album)
@ -135,8 +111,6 @@ class Download
/** /**
* @param Artist $artist * @param Artist $artist
* *
* @throws Exception
*
* @return string * @return string
*/ */
protected function fromArtist(Artist $artist) protected function fromArtist(Artist $artist)

View file

@ -1,77 +1,77 @@
{ {
"name": "phanan/koel", "name": "phanan/koel",
"description": "Personal audio streaming service that works.", "description": "Personal audio streaming service that works.",
"keywords": ["audio", "stream", "mp3"], "keywords": ["audio", "stream", "mp3"],
"license": "MIT", "license": "MIT",
"type": "project", "type": "project",
"require": { "require": {
"php": ">=5.6.4", "php": ">=5.6.4",
"laravel/framework": "5.4.*", "laravel/framework": "5.4.*",
"james-heinrich/getid3": "^1.9", "james-heinrich/getid3": "^1.9",
"guzzlehttp/guzzle": "^6.1", "guzzlehttp/guzzle": "^6.1",
"tymon/jwt-auth": "^0.5.6", "tymon/jwt-auth": "^0.5.6",
"aws/aws-sdk-php-laravel": "^3.1", "aws/aws-sdk-php-laravel": "^3.1",
"pusher/pusher-php-server": "^2.6", "pusher/pusher-php-server": "^2.6",
"predis/predis": "~1.0", "predis/predis": "~1.0",
"doctrine/dbal": "^2.5", "doctrine/dbal": "^2.5",
"jackiedo/dotenv-editor": "^1.0" "jackiedo/dotenv-editor": "^1.0"
}, },
"require-dev": { "require-dev": {
"fzaninotto/faker": "~1.4", "fzaninotto/faker": "~1.4",
"mockery/mockery": "~1.0", "mockery/mockery": "~1.0",
"phpunit/phpunit": "~5.7", "phpunit/phpunit": "~5.7",
"symfony/css-selector": "2.8.*|3.0.*", "symfony/css-selector": "2.8.*|3.0.*",
"symfony/dom-crawler": "^3.2", "symfony/dom-crawler": "^3.2",
"facebook/webdriver": "^1.2", "facebook/webdriver": "^1.2",
"barryvdh/laravel-ide-helper": "^2.1", "barryvdh/laravel-ide-helper": "^2.1",
"laravel/tinker": "^1.0", "laravel/tinker": "^1.0",
"laravel/browser-kit-testing": "^1.0", "laravel/browser-kit-testing": "^1.0",
"codeclimate/php-test-reporter": "^0.4.4", "codeclimate/php-test-reporter": "^0.4.4",
"mikey179/vfsStream": "^1.6" "mikey179/vfsStream": "^1.6"
}, },
"autoload": { "autoload": {
"classmap": [ "classmap": [
"database" "database"
], ],
"psr-4": { "psr-4": {
"App\\": "app/", "App\\": "app/",
"Tests\\": "tests/" "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"
}
} }
},
"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"
}
}
} }