deblan.io-murph/src/Controller/StlMeshController.php

70 lines
1.9 KiB
PHP
Raw Normal View History

2021-07-09 23:42:38 +02:00
<?php
namespace App\Controller;
use App\Api\TTRssClient;
use App\Core\Controller\Site\PageController;
use App\Markdown\Parser\Post as PostParser;
use Symfony\Component\HttpFoundation\Response;
use App\Repository\StlMeshRepositoryQuery;
use App\Entity\StlMesh;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Annotation\Route;
class StlMeshController extends PageController
{
public function meshes(StlMeshRepositoryQuery $query): Response
{
$pager = $query->create()
->orderBy('.sortOrder')
->paginate(1, 200);
return $this->defaultRender($this->siteRequest->getPage()->getTemplate(), [
'pager' => $pager,
]);
}
/**
2021-10-20 14:17:13 +02:00
* @Route("/mesh/download/{stlMesh}/{key}", name="mesh_download")
2021-07-09 23:42:38 +02:00
*/
2021-10-20 14:17:13 +02:00
public function download(StlMesh $stlMesh, int $key): Response
2021-07-09 23:42:38 +02:00
{
2021-10-20 14:17:13 +02:00
$file = $stlMesh->getFiles()[--$key] ?? null;
if (null === $file) {
throw $this->createNotFoundException();
}
$response = new BinaryFileResponse($file['file']);
2021-07-09 23:42:38 +02:00
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
2021-10-20 14:17:13 +02:00
sprintf('%s.stl', $file['name'])
2021-07-09 23:42:38 +02:00
);
return $response;
}
/**
2021-10-20 14:17:13 +02:00
* @Route("/mesh/viewer/{stlMesh}/{key}", name="mesh_viewer")
2021-07-09 23:42:38 +02:00
*/
2021-10-20 14:17:13 +02:00
public function viewer(StlMesh $stlMesh, int $key): Response
2021-07-09 23:42:38 +02:00
{
2021-10-20 14:17:13 +02:00
$file = $stlMesh->getFiles()[--$key] ?? null;
if (null === $file) {
throw $this->createNotFoundException();
}
2021-11-13 23:32:25 +01:00
if (isset($file['htmlPreviewFile'])) {
$file['htmlPreviewFile'] = file_get_contents($file['htmlPreviewFile']);
}
2021-07-09 23:42:38 +02:00
return $this->render('page/mesh/viewer.html.twig', [
'mesh' => $stlMesh,
2021-10-20 14:17:13 +02:00
'file' => $file,
'key' => $key + 1,
2021-07-09 23:42:38 +02:00
]);
}
}