Skip to content

Commit

Permalink
Add books from interface
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMendolia committed Jan 24, 2024
1 parent 58a6a0e commit b1b5a6f
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 73 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ docker-compose.override.yml
!/public/books/.gitkeep
/public/covers
!/public/covers/.gitkeep
/public/books/consume
!/public/books/consume/.gitkeep

###< symfony/framework-bundle ###

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
FROM ghcr.io/biblioverse/biblioteca-docker:1.0.8
FROM ghcr.io/biblioverse/biblioteca-docker:1.0.11
73 changes: 15 additions & 58 deletions src/Command/BooksScanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

Expand All @@ -19,22 +20,16 @@
)]
class BooksScanCommand extends Command
{
private BookManager $bookManager;
private BookRepository $bookRepository;
private EntityManagerInterface $entityManager;
private BookFileSystemManager $fileSystemManager;

public function __construct(BookManager $bookManager, BookFileSystemManager $fileSystemManager, BookRepository $bookRepository, EntityManagerInterface $entityManager)
public function __construct(private EntityManagerInterface $entityManager, private BookManager $bookManager, private BookFileSystemManager $fileSystemManager)
{
parent::__construct();
$this->bookManager = $bookManager;
$this->bookRepository = $bookRepository;
$this->entityManager = $entityManager;
$this->fileSystemManager = $fileSystemManager;
}

protected function configure(): void
{
$this
->addOption('book-path', 'b', InputOption::VALUE_REQUIRED, 'Which filepath to consume')
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -43,55 +38,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$io->writeln('Scanning books directory');

$files = $this->fileSystemManager->getAllBooksFiles();
$progressBar = new ProgressBar($output, iterator_count($files));
$progressBar->setFormat('debug');
$progressBar->start();
foreach ($files as $file) {
$progressBar->advance();
try {
$flush = false;
$progressBar->setMessage($file->getFilename());
$book = $this->bookRepository->findOneBy(
[
'bookPath' => $this->fileSystemManager->getFolderName($file),
'bookFilename' => $file->getFilename(),
]);

if (null !== $book) {
continue;
}

$checksum = $this->fileSystemManager->getFileChecksum($file);
$book = $this->bookRepository->findOneBy(['checksum' => $checksum]);
if (null === $book) {
$book = $this->bookManager->createBook($file);
$flush = true;
} else {
$previousPath = $book->getBookPath();
$previousName = $book->getBookFilename();
$book = $this->bookManager->updateBookLocation($book, $file);
if ($book->getBookPath() !== $previousPath || $book->getBookFilename() !== $previousName) {
$flush = true;
}
}

if (true === $flush) {
$this->entityManager->persist($book);
$this->entityManager->flush();
}
} catch (\Exception $e) {
$io->error('died during process of '.$file->getRealPath());
$io->error($e->getMessage());
throw $e;
}
$book = null;
gc_collect_cycles();
if($input->getOption('book-path') !== null) {
$info = new \SplFileInfo($input->getOption('book-path'));
$book = $this->bookManager->consumeBook($info);
$this->entityManager->persist($book);
$this->entityManager->flush();
$this->fileSystemManager->renameFiles($book);
$this->entityManager->flush();
} else {
$files = $this->fileSystemManager->getAllBooksFiles();
$this->bookManager->consumeBooks(iterator_to_array($files), $input, $output);
}
$io->writeln('');
$io->writeln('Persisting books...');
$this->entityManager->flush();
$progressBar->finish();
$io->success('Done!');

return Command::SUCCESS;
Expand Down
74 changes: 73 additions & 1 deletion src/Controller/BookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
use App\Entity\Book;
use App\Repository\BookRepository;
use App\Service\BookFileSystemManager;
use App\Service\BookManager;
use App\Service\BookSuggestions;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Process\Process;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/books')]
class BookController extends AbstractController
{
#[Route('/{book}/{slug}', name: 'app_book')]
public function index(Request $request, Book $book, string $slug, BookSuggestions $bookSuggestions, BookRepository $bookRepository): Response
public function index(Request $request, Book $book, string $slug, BookSuggestions $bookSuggestions, BookRepository $bookRepository, BookManager $bookManager, BookFileSystemManager $fileSystemManager): Response
{
if ($slug !== $book->getSlug()) {
return $this->redirectToRoute('app_book', [
Expand Down Expand Up @@ -69,13 +71,18 @@ public function index(Request $request, Book $book, string $slug, BookSuggestion

$sameAuthorBooks = $bookRepository->getWithSameAuthors($book, 6);

$calculatedPath = $fileSystemManager->getCalculatedFilePath($book, false).$book->getBookFilename();
$needsRelocation = $fileSystemManager->getCalculatedFilePath($book, false).$book->getBookFilename()!==$book->getBookPath().$book->getBookFilename();

return $this->render('book/index.html.twig', [
'book' => $book,
'serie' => $serie,
'serieMax' => $serieMax,
'sameAuthor' => $sameAuthorBooks,
'suggestions' => $suggestions,
'form' => $form->createView(),
'calculatedPath' => $calculatedPath,
'needsRelocation' => $needsRelocation,
]);
}

Expand Down Expand Up @@ -129,4 +136,69 @@ public function deleteBook(int $id, EntityManagerInterface $entityManager, BookF

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

#[Route('/new/consume/files', name: 'app_book_consume')]
public function consume(Request $request, BookFileSystemManager $fileSystemManager): Response
{
$bookFiles = $fileSystemManager->getAllBooksFiles(true);

$bookFiles = iterator_to_array($bookFiles);

$consume = $request->get('consume');
if($consume!==null) {
foreach ($bookFiles as $bookFile) {
if($bookFile->getRealPath()!==$consume) {
continue;
}
$childProcess = new Process(['/var/www/html/bin/console', 'books:scan','--book-path', $bookFile->getRealPath()]);

$childProcess->start();

$childProcess->wait();

$childProcess = new Process(['/var/www/html/bin/console', 'books:extract-cover','all']);

$childProcess->start();

$childProcess->wait();

$this->addFlash('success', 'Book '.$bookFile->getFilename().' consumed');

return $this->redirectToRoute('app_book_consume');

}
}

$delete = $request->get('delete');
if($delete!==null) {
foreach ($bookFiles as $bookFile) {
if($bookFile->getRealPath()!==$delete) {
continue;
}
unlink($bookFile->getRealPath());
$this->addFlash('success', 'Book '.$bookFile->getFilename().' deleted');
return $this->redirectToRoute('app_book_consume');
}
}

return $this->render('book/consume.html.twig', [
'books' => $bookFiles,
]);
}


#[Route('/relocate/{id}/files', name: 'app_book_relocate')]
public function relocate(Book $book, BookFileSystemManager $fileSystemManager, EntityManagerInterface $entityManager): Response
{
$book = $fileSystemManager->renameFiles($book);
$entityManager->persist($book);
$entityManager->flush();

return $this->redirectToRoute('app_book', [
'book' => $book->getId(),
'slug'=> $book->getSlug(),
]);
}


}
1 change: 1 addition & 0 deletions src/Menu/MenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function createMainMenu(array $options): ItemInterface
$menu->addChild('My profile', ['route' => 'app_user_profile', ...$this->defaultAttr])->setExtra('icon', 'person-circle');
if ($this->security->isGranted('ROLE_ADMIN')) {
$menu->addChild('Admin', ['route' => 'app_user_index', ...$this->defaultAttr])->setExtra('icon', 'gear-fill');
$menu->addChild('Add Books', ['route' => 'app_book_consume', ...$this->defaultAttr])->setExtra('icon', 'bookmark-plus-fill');
}
$menu->addChild('Logout', ['route' => 'app_logout', ...$this->defaultAttr])->setExtra('icon', 'door-closed');

Expand Down
27 changes: 19 additions & 8 deletions src/Service/BookFileSystemManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ public function getCoverDirectory(): string
/**
* @return \Iterator<\SplFileInfo>
*/
public function getAllBooksFiles(): \Iterator
public function getAllBooksFiles(bool $onlyConsumeDirectory=false): \Iterator
{
try {
$finder = new Finder();
$finder->files()->name(self::ALLOWED_FILE_EXTENSIONS)->in($this->getBooksDirectory());
$finder->files()->name(self::ALLOWED_FILE_EXTENSIONS);
if($onlyConsumeDirectory) {
$finder->in($this->getBooksDirectory().'/consume');
} else {
$finder->in($this->getBooksDirectory());
}
$iterator = $finder->getIterator();
} catch (\Exception $e) {
$iterator = new \ArrayIterator();
Expand Down Expand Up @@ -185,7 +190,8 @@ public function renameFiles(Book $book): Book
$filesystem->rename(
$this->getBooksDirectory().$book->getBookPath().$book->getBookFilename(),
$this->getCalculatedFilePath($book, true).$this->getCalculatedFileName($book),
true);
true
);

$book->setBookPath($this->getCalculatedFilePath($book, false));
$book->setBookFilename($this->getCalculatedFileName($book));
Expand All @@ -196,7 +202,8 @@ public function renameFiles(Book $book): Book
$filesystem->rename(
$this->getCoverDirectory().$book->getImagePath().'/'.$book->getImageFilename(),
$this->getCalculatedImagePath($book, true).$this->getCalculatedImageName($book),
true);
true
);

$book->setImagePath($this->getCalculatedImagePath($book, false));
$book->setImageFilename($this->getCalculatedImageName($book));
Expand Down Expand Up @@ -249,7 +256,8 @@ public function uploadBookCover(UploadedFile $file, Book $book): Book
$filesystem->rename(
$file->getRealPath(),
$this->getCalculatedImagePath($book, true).$this->getCalculatedImageName($book, $checksum),
true);
true
);

$this->logger->info('Rename file', ['from' => $file->getRealPath(), 'to' => $this->getCalculatedImagePath($book, true).$this->getCalculatedImageName($book, $checksum)]);

Expand Down Expand Up @@ -442,7 +450,8 @@ private function extractFromRarArchive(\SplFileInfo $bookFile, Book $book): Book
$filesystem->rename(
$item->getRealPath(),
'/tmp/cover/cover.'.$ext,
true);
true
);
$file = new \SplFileInfo('/tmp/cover/cover.'.$ext);
}

Expand All @@ -460,7 +469,8 @@ private function extractFromRarArchive(\SplFileInfo $bookFile, Book $book): Book
$filesystem->rename(
$file->getRealPath(),
$this->getCalculatedImagePath($book, true).$this->getCalculatedImageName($book, $checksum),
true);
true
);

$book->setImagePath($this->getCalculatedImagePath($book, false));
$book->setImageFilename($this->getCalculatedImageName($book, $checksum));
Expand Down Expand Up @@ -514,7 +524,8 @@ private function extractFromGeneralArchive(\SplFileInfo $bookFile, Book $book):
$filesystem->rename(
$item->getRealPath(),
$this->getCalculatedImagePath($book, true).$this->getCalculatedImageName($book, $checksum),
true);
true
);

$book->setImagePath($this->getCalculatedImagePath($book, false));
$book->setImageFilename($this->getCalculatedImageName($book, $checksum));
Expand Down
Loading

0 comments on commit b1b5a6f

Please sign in to comment.