Skip to content

Commit

Permalink
Add upload of user MapFile
Browse files Browse the repository at this point in the history
  • Loading branch information
jbelien committed Sep 14, 2018
1 parent 51f8dee commit 01e734d
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
$app->get('/mapfile', App\Handler\MapFileHandler::class, 'mapfile');
$app->get('/mapfile/edit', App\Handler\MapFileHandler::class, 'mapfile.edit');

$app->route('/open', App\Handler\OpenHandler::class, ['get', 'post'], 'open');

$app->put('/api/map', App\Handler\API\MapHandler::class, 'api.map');
$app->route('/api/layer/{id:\d+}', App\Handler\API\LayerHandler::class, ['put', 'delete'], 'api.layer');
$app->route('/api/layer/{layer:\d+}/class/{id:\d+}', App\Handler\API\ClassHandler::class, ['put', 'delete'], 'api.class');
Expand Down
1 change: 1 addition & 0 deletions src/App/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function getDependencies() : array
Handler\LayerHandler::class => Handler\LayerHandlerFactory::class,
Handler\MapHandler::class => Handler\MapHandlerFactory::class,
Handler\MapFileHandler::class => Handler\MapFileHandlerFactory::class,
Handler\OpenHandler::class => Handler\OpenHandlerFactory::class,
Handler\StyleHandler::class => Handler\StyleHandlerFactory::class,
],
];
Expand Down
79 changes: 79 additions & 0 deletions src/App/Handler/OpenHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace App\Handler;

use Exception;
use MapFile\Parser\Map as MapParser;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Diactoros\Response\RedirectResponse;
use Zend\Diactoros\UploadedFile;
use Zend\Expressive\Router;
use Zend\Expressive\Session\SessionMiddleware;
use Zend\Expressive\Template;

class OpenHandler implements RequestHandlerInterface
{
private $containerName;

private $router;

private $template;

public function __construct(
Router\RouterInterface $router,
Template\TemplateRendererInterface $template = null,
string $containerName
) {
$this->router = $router;
$this->template = $template;
$this->containerName = $containerName;
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
$session = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE);

$method = $request->getMethod();

switch ($method) {
case 'POST':
try {
$files = $request->getUploadedFiles();

if (isset($files['mapfile'])) {
if ($files['mapfile']->getError() === UPLOAD_ERR_OK) {
$temp = tempnam(sys_get_temp_dir(), 'mapfile-');

$files['mapfile']->moveTo($temp);

$map = (new MapParser($temp))->parse();

$session->set('map', serialize($map));

return new RedirectResponse($this->router->generateUri('map'));
} else {
throw new Exception(UploadedFile::ERROR_MESSAGES[$files['mapfile']->getError()]);
}
} else {
throw new Exception(UploadedFile::ERROR_MESSAGES[UPLOAD_ERR_NO_FILE]);
}
} catch (Exception $e) {
$data = [
'error' => $e->getMessage(),
];

return new HtmlResponse($this->template->render('app::open', $data));
}
break;

case 'GET':
return new HtmlResponse($this->template->render('app::open', []));
break;
}
}
}
21 changes: 21 additions & 0 deletions src/App/Handler/OpenHandlerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Handler;

use Psr\Container\ContainerInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Expressive\Router\RouterInterface;
use Zend\Expressive\Template\TemplateRendererInterface;

class OpenHandlerFactory
{
public function __invoke(ContainerInterface $container): RequestHandlerInterface
{
$router = $container->get(RouterInterface::class);
$template = $container->get(TemplateRendererInterface::class);

return new OpenHandler($router, $template, get_class($container));
}
}
45 changes: 45 additions & 0 deletions templates/app/open.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{% extends '@layout/default.html.twig' %}

{% block title %}Load your MapFile{% endblock %}

{% block content %}

<h1>Load your MapFile</h1>
<hr>

<p>
The application will parse your MapFile to allow you to modify it.<br>
<br>
The <a href="https://github.com/jbelien/MapFile-PHP-Library" target="_blank"><em>MapFile PHP Library</em></a> used
to parse your file supports <a href="https://mapserver.org/mapfile/" target="_blank">MapServer 7.2 configuration</a>.
If your MapFile is written for a previous version of MapServer, some parameters could be deprecated.<br>
You can comment (with <kbd>#</kbd>) or fix the deprecated parameter.<br>
You can also <a href="https://github.com/jbelien/MapFile-PHP-Library/issues" target="_blank">contact me</a> to add
support for deprecated (or unsupported) parameters.
</p>
<hr>

<form method="POST" action="{{ path('open') }}" enctype="multipart/form-data">
<div class="row">
<div class="col">
<div class="form-group">
<input type="file" class="form-control-file" id="open-mapfile" name="mapfile" accept=".map" required>
</div>
</div>
<div class="col-md-3 col-sm-4">
<button type="submit" class="btn btn-primary btn-block">
<i class="fas fa-file-upload"></i>
Upload
</button>
</div>
</div>
</form>
<hr>

{% if error is defined %}
<div class="alert alert-warning">
{{ error }}
</div>
{% endif %}

{% endblock %}
6 changes: 6 additions & 0 deletions templates/layout/default.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
MapFile
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ path('open') }}">
<i class="fas fa-upload"></i>
Load
</a>
</li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item">
Expand Down

0 comments on commit 01e734d

Please sign in to comment.