Skip to content

Commit

Permalink
make upload work
Browse files Browse the repository at this point in the history
  • Loading branch information
makomweb committed Jun 17, 2021
1 parent aebba18 commit 0d93ae3
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 122 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,10 @@ http://guest:guest@localhost:15672.
# Migrations

- `php bin/console make:migration`
- `php bin/console doctrine:migrations:migrate`
- `php bin/console doctrine:migrations:migrate`

# Create DB + create Schema

- `php bin/console doctrine:database:create`
- `php bin/console doctrine:schema:create`
- `php bin/console doctrine:schema:update --force`
36 changes: 0 additions & 36 deletions migrations/Version20210615083431.php

This file was deleted.

41 changes: 0 additions & 41 deletions migrations/Version20210615091526.php

This file was deleted.

31 changes: 0 additions & 31 deletions migrations/Version20210617062745.php

This file was deleted.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 32 additions & 5 deletions src/Controller/FileUploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,42 @@

namespace App\Controller;

use App\Entity\FileUpload;
use App\Repository\FileUploadRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Service\FileUploader;
use Psr\Log\LoggerInterface;
use Symfony\Component\Uid\Uuid;

class FileUploadController extends AbstractController
{
/**
* @param FileUploader $uploader
* @param LoggerInterface $logger
* @param FileUploadRepository $repository
*/
public function __construct(FileUploader $uploader, LoggerInterface $logger)
public function __construct(FileUploader $uploader,
LoggerInterface $logger,
FileUploadRepository $repository)
{
$this->uploader = $uploader;
$this->logger = $logger;
$this->repository = $repository;
}

/**
* @Route("/upload", name="upload")
*/
public function index()
{
return $this->render('file_upload/index.html.twig');
$uploads = $this->repository->findAll();

return $this->render('file_upload/index.html.twig', [
'uploads' => $uploads,
]);
}

/**
Expand Down Expand Up @@ -55,10 +66,26 @@ public function doUpload(Request $request, string $uploadDir): Response
Response::HTTP_UNPROCESSABLE_ENTITY, ['content-type' => 'text/plain']);
}

$filename = $file->getClientOriginalName();
$filename = $this->getUniqueFileName(
$file->getClientOriginalName()
);

$this->uploader->upload($uploadDir, $file, $filename);

return new Response("File uploaded", Response::HTTP_OK,
['content-type' => 'text/plain']);
{
$upload = new FileUpload();
$upload->setName($filename);
$upload->setStatus("initial");
$this->repository->save($upload);
}

return $this->redirectToRoute('upload');
}

private function getUniqueFileName(string $filename) {

$uuid = Uuid::v4();

return $uuid->toRfc4122() . '-' . $filename;
}
}
1 change: 1 addition & 0 deletions src/Entity/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/**
* @ORM\Entity(repositoryClass=FileUploadRepository::class)
* @ORM\Table(name="`file_upload`")
*/
class FileUpload
{
Expand Down
8 changes: 8 additions & 0 deletions src/Repository/FileUploadRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ public function findOneBySomeField($value): ?FileUpload
;
}
*/

public function save(FileUpload $upload): int
{
$this->_em->persist($upload);
$this->_em->flush();

return $upload->getId();
}
}
50 changes: 42 additions & 8 deletions templates/file_upload/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,51 @@

{% block body %}

<form action="{{ path('do-upload') }}" method="post" enctype="multipart/form-data">
<div class="container">

<input type="hidden" name="token" value="{{ csrf_token('upload') }}" />
<div class="mb-2 p-2 bg-white">

<div>
<label for="myfile">File to upload:</label>
<input type="file" name="myfile" id="myfile">
<table class="table table-sm table-hover">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{% for upload in uploads %}
<tr>
<td>{{ upload.id }}</td>
<td>{{ upload.name }}</td>
<td>
{% if upload.status == "initial" %}
<span class="badge badge-warning">{{ upload.status }}</span>
{% elseif order.status == "handled" %}
<span class="badge badge-success">{{ upload.status }}</span>
{% else %}
<span class="badge badge-primary">{{ upload.status }}</span>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>

<form action="{{ path('do-upload') }}" method="post" enctype="multipart/form-data">

<button type="submit">Send</button>

</form>
<input type="hidden" name="token" value="{{ csrf_token('upload') }}" />

<div>
<label for="myfile">File to upload:</label>
<input type="file" name="myfile" id="myfile">
</div>

<button type="submit">Send</button>

</form>

</div>

{% endblock %}

0 comments on commit 0d93ae3

Please sign in to comment.