mirror of
https://github.com/ToxicCrack/PrintABrick.git
synced 2025-05-17 21:00:09 -07:00
158 lines
4.5 KiB
PHP
158 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace AppBundle\Service;
|
|
|
|
use AppBundle\Entity\LDraw\Model;
|
|
use AppBundle\Entity\Rebrickable\Set;
|
|
use League\Flysystem\Filesystem;
|
|
|
|
class ZipService
|
|
{
|
|
/** @var \ZipArchive */
|
|
private $archive;
|
|
|
|
/** @var Filesystem */
|
|
private $mediaFilesystem;
|
|
|
|
/** @var SetService */
|
|
private $setService;
|
|
|
|
/** @var ModelService */
|
|
private $modelService;
|
|
|
|
private $zipName;
|
|
|
|
private $models;
|
|
|
|
/**
|
|
* ZipService constructor.
|
|
*
|
|
* @param $mediaFilesystem
|
|
* @param $setService
|
|
*/
|
|
public function __construct($mediaFilesystem, $setService, $modelService)
|
|
{
|
|
$this->mediaFilesystem = $mediaFilesystem;
|
|
$this->setService = $setService;
|
|
$this->modelService = $modelService;
|
|
}
|
|
|
|
private function createZip($path)
|
|
{
|
|
$archive = new \ZipArchive();
|
|
$archive->open($path, \ZipArchive::CREATE);
|
|
|
|
return $archive;
|
|
}
|
|
|
|
public function createFromSet(Set $set, $sorted = false)
|
|
{
|
|
$sort = $sorted ? 'sorted' : 'unsorted';
|
|
$this->zipName = "set_{$set->getNumber()}_{$set->getName()}({$sort})";
|
|
|
|
$zipPath = tempnam(sys_get_temp_dir(), 'printabrick');
|
|
$this->archive = $this->createZip($zipPath);
|
|
|
|
if ($sorted) {
|
|
$this->addSetGroupedByColor($set);
|
|
} else {
|
|
$this->addSet($set);
|
|
}
|
|
|
|
$this->addLicense();
|
|
$this->archive->close();
|
|
|
|
return $zipPath;
|
|
}
|
|
|
|
public function createFromModel(Model $model, $subparts = false)
|
|
{
|
|
$this->zipName = "model_{$model->getNumber()}";
|
|
|
|
$zipPath = tempnam(sys_get_temp_dir(), 'printabrick');
|
|
$this->archive = $this->createZip($zipPath);
|
|
|
|
$filename = "{$this->zipName}/{$model->getNumber()}.stl";
|
|
$this->addModel($filename, $model);
|
|
|
|
if ($subparts) {
|
|
foreach ($this->modelService->getAllSubparts($model) as $subpart) {
|
|
$submodel = $subpart['model'];
|
|
$filename = "{$this->zipName}/submodels/{$submodel->getNumber()}_({$subpart['quantity']}x).stl";
|
|
|
|
$this->addModel($filename, $submodel);
|
|
}
|
|
}
|
|
|
|
$this->addLicense();
|
|
$this->archive->close();
|
|
|
|
return $zipPath;
|
|
}
|
|
|
|
/**
|
|
* Add stl files of models used in set into zip grouped by color.
|
|
*
|
|
* @param Set $set
|
|
* @param bool $spare If true - add only spare parts, false - add only regular parts, null - add all parts
|
|
*/
|
|
public function addSetGroupedByColor(Set $set, $spare = null)
|
|
{
|
|
$colors = $this->setService->getModelsGroupedByColor($set, $spare);
|
|
|
|
foreach ($colors as $colorArray) {
|
|
$models = $colorArray['models'];
|
|
$color = $colorArray['color'];
|
|
foreach ($models as $modelArray) {
|
|
$model = $modelArray['model'];
|
|
$quantity = $modelArray['quantity'];
|
|
|
|
$filename = "{$this->zipName}/{$color->getName()}/{$model->getNumber()}_({$quantity}x).stl";
|
|
|
|
$this->addModel($filename, $model);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Add stl files used in set into zip.
|
|
*
|
|
* @param Set $set
|
|
* @param bool $spare If true - add only spare parts, false - add only regular parts, null - add all parts
|
|
*/
|
|
public function addSet(Set $set, $spare = null)
|
|
{
|
|
$models = $this->setService->getModels($set, $spare);
|
|
|
|
foreach ($models as $number => $array) {
|
|
$model = $array['model'];
|
|
$quantity = $array['quantity'];
|
|
$filename = "{$this->zipName}/{$number}_({$quantity}x).stl";
|
|
$this->models[$number] = $array['model'];
|
|
|
|
$this->addModel($filename, $model);
|
|
}
|
|
}
|
|
|
|
private function addModel($path, $model)
|
|
{
|
|
$this->archive->addFromString($path, $this->mediaFilesystem->read($model->getPath()));
|
|
$this->models[$model->getNumber()] = $model;
|
|
}
|
|
|
|
private function addLicense()
|
|
{
|
|
$text = sprintf('All stl files in this archive were converted by LDView from LDraw Library http://www.ldraw.org/'."\n\n");
|
|
$text .= sprintf('Files are licensed under the Creative Commons - Attribution license.'."\n");
|
|
$text .= sprintf('http://creativecommons.org/licenses/by/2.0/'."\n\n");
|
|
|
|
$text .= sprintf('Attribution:'."\n"."\n");
|
|
|
|
foreach ($this->models as $model) {
|
|
$text .= sprintf('%s - "%s" by %s'."\n", $model->getNumber(), $model->getName(), $model->getAuthor()->getName());
|
|
}
|
|
|
|
$this->archive->addFromString("{$this->zipName}/LICENSE.txt", $text);
|
|
}
|
|
}
|