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;
|
2018-08-18 10:35:42 +00:00
|
|
|
use RuntimeException;
|
2017-06-04 01:12:08 +00:00
|
|
|
use ZipArchive;
|
|
|
|
|
|
|
|
class SongZipArchive
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ZipArchive
|
|
|
|
*/
|
2019-06-30 09:44:39 +00:00
|
|
|
private $archive;
|
2017-06-04 01:12:08 +00:00
|
|
|
|
|
|
|
/**
|
2017-06-04 01:12:24 +00:00
|
|
|
* Path to the zip archive.
|
2017-06-04 01:12:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-06-30 09:44:39 +00:00
|
|
|
private $path;
|
2017-06-04 01:12:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2019-06-30 09:44:39 +00:00
|
|
|
private $fileNames = [];
|
2017-06-04 01:12:08 +00:00
|
|
|
|
2019-06-30 09:44:39 +00:00
|
|
|
public function __construct(string $path = '')
|
2017-06-04 01:12:08 +00:00
|
|
|
{
|
2019-06-30 09:44:39 +00:00
|
|
|
$this->path = $path ? $path : self::generateRandomArchivePath();
|
2017-06-04 01:12:08 +00:00
|
|
|
|
|
|
|
$this->archive = new ZipArchive();
|
|
|
|
|
|
|
|
if ($this->archive->open($this->path, ZipArchive::CREATE) !== true) {
|
2018-08-18 10:35:42 +00:00
|
|
|
throw new RuntimeException('Cannot create zip file.');
|
2017-06-04 01:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add multiple songs into the archive.
|
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function addSongs(Collection $songs): self
|
2017-06-04 01:12:08 +00:00
|
|
|
{
|
2018-08-18 10:35:42 +00:00
|
|
|
$songs->each([$this, 'addSong']);
|
2017-06-04 01:12:08 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a single song into the archive.
|
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function addSong(Song $song): self
|
2017-06-04 01:12:08 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
$path = Download::fromSong($song);
|
2019-06-30 09:44:39 +00:00
|
|
|
$this->archive->addFile($path, $this->generateZipContentFileNameFromPath($path));
|
2017-06-04 01:12:08 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::error($e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finish (close) the archive.
|
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function finish(): self
|
2017-06-04 01:12:08 +00:00
|
|
|
{
|
|
|
|
$this->archive->close();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function getPath(): string
|
2017-06-04 01:12:08 +00:00
|
|
|
{
|
|
|
|
return $this->path;
|
|
|
|
}
|
2017-08-05 17:28:28 +00:00
|
|
|
|
2019-06-30 09:44:39 +00:00
|
|
|
/**
|
|
|
|
* We add all files into the zip archive as a flat structure.
|
|
|
|
* As a result, there can be duplicate file names.
|
|
|
|
* This method makes sure each file name is unique in the zip archive.
|
|
|
|
*/
|
|
|
|
private function generateZipContentFileNameFromPath(string $path): string
|
|
|
|
{
|
|
|
|
$name = basename($path);
|
|
|
|
|
|
|
|
if (array_key_exists($name, $this->fileNames)) {
|
|
|
|
$this->fileNames[$name]++;
|
|
|
|
$parts = explode('.', $name);
|
|
|
|
$ext = $parts[count($parts) - 1];
|
|
|
|
$parts[count($parts) - 1] = $this->fileNames[$name] . ".$ext";
|
|
|
|
$name = implode('.', $parts);
|
|
|
|
} else {
|
|
|
|
$this->fileNames[$name] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function generateRandomArchivePath(): string
|
2017-08-05 17:28:28 +00:00
|
|
|
{
|
2019-06-30 09:44:39 +00:00
|
|
|
// We use system's temp dir instead of storage_path() here, so that the generated files
|
|
|
|
// can be cleaned up automatically after server reboot.
|
|
|
|
return sprintf('%s%skoel-download-%s.zip', sys_get_temp_dir(), DIRECTORY_SEPARATOR, uniqid());
|
2017-08-05 17:28:28 +00:00
|
|
|
}
|
2017-06-04 01:12:08 +00:00
|
|
|
}
|