diff --git a/api/common/Controller/Import.php b/api/common/Controller/Import.php index 17dee20..48d091c 100644 --- a/api/common/Controller/Import.php +++ b/api/common/Controller/Import.php @@ -3,19 +3,47 @@ namespace Contabilidad\Common\Controller; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; +use Psr\Container\ContainerInterface as Container; use ProVM\Common\Define\Controller\Json; use ProVM\Common\Factory\Model as Factory; use Contabilidad\Common\Service\DocumentHandler as Handler; +use Contabilidad\Cuenta; class Import { use Json; - public function __invoke(Request $request, Response $response, Factory $factory): Response { - $post = $request->getParsedBody(); - return $this->withJson($response, $post); + public function __invoke(Request $request, Response $response, Factory $factory, Container $container): Response { + $post =$request->getParsedBody(); + $cuenta = $factory->find(Cuenta::class)->one($post['cuenta']); + $file = $request->getUploadedFiles()['archivo']; + $valid_media = [ + 'text/csv' => 'csvs', + 'application/pdf' => 'pdfs', + 'application/vnd.ms-excel' => 'xlss', + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlss', + 'application/json' => 'jsons' + ]; + if ($file->getError() === 0 and in_array($file->getClientMediaType(), array_keys($valid_media))) { + $filenfo = new \SplFileInfo($file->getClientFilename()); + $new_name = implode('.', [implode(' - ', [$cuenta->nombre, $cuenta->categoria()->nombre, $post['fecha']]), $filenfo->getExtension()]); + $to = implode(DIRECTORY_SEPARATOR, [$container->get('folders')->uploads, $valid_media[$file->getClientMediaType()], $new_name]); + $file->moveTo($to); + $status = file_exists($to); + } + $output = [ + 'input' => [ + 'name' => $file->getClientFilename(), + 'type' => $file->getClientMediaType(), + 'size' => $file->getSize(), + 'error' => $file->getError() + ], + 'new_name' => $new_name, + 'uploaded' => $status + ]; + return $this->withJson($response, $output); } public function uploads(Request $request, Response $response, Handler $handler): Response { - $output = $handler->handle(); - return $this->withJson($response, $output); + $output = $handler->handle(); + return $this->withJson($response, $output); } } diff --git a/api/nginx.conf b/api/nginx.conf index 44c93fd..5d96382 100644 --- a/api/nginx.conf +++ b/api/nginx.conf @@ -5,6 +5,8 @@ server { access_log /var/log/nginx/access.log; root /app/public; + client_max_body_size 50M; + location / { try_files $uri $uri/ /index.php?$query_string; } diff --git a/api/php.ini b/api/php.ini index e145929..c7681b7 100644 --- a/api/php.ini +++ b/api/php.ini @@ -1,2 +1,4 @@ log_errors = true -error_log = /var/log/php/error.log \ No newline at end of file +error_log = /var/log/php/error.log +upload_max_filesize = 50M +max_input_vars = 5000 diff --git a/ui/common/Controller/Importar.php b/ui/common/Controller/Importar.php new file mode 100644 index 0000000..dca07df --- /dev/null +++ b/ui/common/Controller/Importar.php @@ -0,0 +1,12 @@ +render($response, 'importar'); + } +} diff --git a/ui/resources/routes/importar.php b/ui/resources/routes/importar.php new file mode 100644 index 0000000..d25cc39 --- /dev/null +++ b/ui/resources/routes/importar.php @@ -0,0 +1,4 @@ +get('/importar[/]', Importar::class); diff --git a/ui/resources/views/importar.blade.php b/ui/resources/views/importar.blade.php new file mode 100644 index 0000000..a9171cf --- /dev/null +++ b/ui/resources/views/importar.blade.php @@ -0,0 +1,92 @@ +@extends('layout.base') + +@section('page_title') + Importar +@endsection + +@section('page_content') +