koel/app/Models/SongZipArchive.php

135 lines
3 KiB
PHP
Raw Normal View History

2017-06-04 01:12:08 +00:00
<?php
namespace App\Models;
use App\Facades\Download;
use Exception;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use ZipArchive;
class SongZipArchive
{
/**
* @var ZipArchive
*/
protected $archive;
/**
2017-06-04 01:12:24 +00:00
* Path to the zip archive.
2017-06-04 01:12:08 +00:00
*
* @var string
*/
protected $path;
/**
* Names of the files in the archive
2017-06-04 01:12:24 +00:00
* Format: [file-name.mp3' => currentFileIndex].
2017-06-04 01:12:08 +00:00
*
* @var array
*/
protected $fileNames = [];
/**
* @param string $path
*
* @throws Exception
*/
public function __construct($path = '')
{
if (!class_exists('ZipArchive')) {
throw new Exception('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';
$this->archive = new ZipArchive();
if ($this->archive->open($this->path, ZipArchive::CREATE) !== true) {
throw new Exception('Cannot create zip file.');
}
}
/**
* Add multiple songs into the archive.
*
* @param Collection $songs
*
* @return $this
*/
public function addSongs(Collection $songs)
{
$songs->each(function ($song) {
$this->addSong($song);
});
return $this;
}
/**
* Add a single song into the archive.
*
* @param Song $song
*
* @return $this
*/
public function addSong(Song $song)
{
try {
$path = Download::fromSong($song);
// 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.
$name = basename($path);
if (array_key_exists($name, $this->fileNames)) {
2017-11-08 13:11:45 +00:00
$this->fileNames[$name]++;
2017-06-04 01:12:08 +00:00
$parts = explode('.', $name);
$ext = $parts[count($parts) - 1];
$parts[count($parts) - 1] = $this->fileNames[$name].".$ext";
$name = implode('.', $parts);
} else {
$this->fileNames[$name] = 1;
}
$this->archive->addFile($path, $name);
} catch (Exception $e) {
Log::error($e);
}
return $this;
}
/**
* Finish (close) the archive.
*
* @return $this
*/
public function finish()
{
$this->archive->close();
return $this;
}
/**
* Get the path to the archive.
*
* @return string
*/
public function getPath()
{
return $this->path;
}
2017-08-05 17:28:28 +00:00
/**
* @return ZipArchive
*/
public function getArchive()
{
return $this->archive;
}
2017-06-04 01:12:08 +00:00
}