Skip to content

Commit

Permalink
Upload cover
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMendolia committed Sep 1, 2023
1 parent e3af308 commit 5a69ef3
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/Service/BookFileSystemManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Entity\Book;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\String\Slugger\SluggerInterface;

Expand Down Expand Up @@ -165,9 +166,9 @@ public function getCalculatedFileName(Book $book): string
return $this->calculateFileName($book).'.'.$book->getExtension();
}

public function getCalculatedImageName(Book $book): string
public function getCalculatedImageName(Book $book, string $checksum = ''): string
{
return $this->calculateFileName($book).'.'.$book->getImageExtension();
return $checksum.$this->calculateFileName($book).'.'.$book->getImageExtension();
}

public function renameFiles(Book $book): Book
Expand Down Expand Up @@ -223,4 +224,30 @@ public function removeEmptySubFolders(string $path = null): bool

return $empty;
}

public function uploadBookCover(UploadedFile $file, Book $book): Book
{
$filesystem = new Filesystem();

$coverFileName = explode('/', $file->getClientOriginalName());
$coverFileName = end($coverFileName);
$ext = explode('.', $coverFileName);
$ext = end($ext);

$book->setImageExtension($ext);

$checksum = (string) md5_file($file->getRealPath());

$filesystem->mkdir($this->getCalculatedImagePath($book, true));
$filesystem->rename(
$file->getRealPath(),
$this->getCalculatedImagePath($book, true).$this->getCalculatedImageName($book, $checksum),
true);

$book->setImagePath($this->getCalculatedImagePath($book, false));
$book->setImageFilename($this->getCalculatedImageName($book, $checksum));
$book->setImageExtension($ext);

return $book;
}
}
58 changes: 58 additions & 0 deletions src/Twig/UploadBookPicture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Twig;

use App\Entity\Book;
use App\Service\BookFileSystemManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\ComponentToolsTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;
use Symfony\UX\LiveComponent\ValidatableComponentTrait;

#[AsLiveComponent()]
class UploadBookPicture extends AbstractController
{
use DefaultActionTrait;
use ValidatableComponentTrait;
use ComponentToolsTrait;

#[LiveProp()]
public Book $book;

#[LiveProp()]
public bool $isEditing = false;

public ?string $flashMessage = null;

#[LiveAction]
public function activateEditing(): void
{
$this->isEditing = true;
}

#[LiveAction]
public function uploadFiles(Request $request, BookFileSystemManager $fileSystemManager, EntityManagerInterface $entityManager): void
{

/** @var UploadedFile $symfonyFile */
$symfonyFile = $request->files->getIterator()->current();

$book = $fileSystemManager->uploadBookCover($symfonyFile, $this->book);

$entityManager->persist($book);
$entityManager->flush();
$this->dispatchBrowserEvent('manager:flush');
$this->isEditing = false;

$this->flashMessage = ' book updated';

$this->dispatchBrowserEvent('manager:flush');

}
}
2 changes: 2 additions & 0 deletions templates/book/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
{% endif %}
{{ include('book/_interaction.html.twig') }}

{{ component('UploadBookPicture',{'book':book}) }}

{{ component('AddBookToShelf',{'book':book, 'user':app.user}) }}
</div>
<div class="col-md-9">
Expand Down
25 changes: 25 additions & 0 deletions templates/components/UploadBookPicture.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<span {{ attributes.defaults(stimulus_controller('inline-edit-book')) }}>
{% if isEditing %}
<form class="btn-group">

<input type="file" name="cover" class="form-control"/>
<button data-action="live#action" class="btn btn-sm btn-outline-dark" data-action-name="files|uploadFiles">
Upload
</button>
</form>
{% else %}

<button
data-action="live#action"
data-action-name="activateEditing"
class="btn btn-sm btn-outline-dark"
title="Click to upload cover!"
>
<i class="bi bi-upload"></i> Upload new cover
</button>

{% if flashMessage %}
<i class="bi bi-check alert-remove"></i> {{ flashMessage }}
{% endif %}
{% endif %}
</span>

0 comments on commit 5a69ef3

Please sign in to comment.