Added:
- FileSystemManager.php refactoring of all write/read actions on filesystem Deleted: - old ToolbarManager.php
This commit is contained in:
89
src/Manager/FileSystemManager.php
Normal file
89
src/Manager/FileSystemManager.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Manager;
|
||||
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\String\Slugger\SluggerInterface;
|
||||
|
||||
class FileSystemManager
|
||||
{
|
||||
private const string CBZ_DIRECTORY = 'public/cbz';
|
||||
private const string UPLOADS_DIRECTORY = 'public/tmp';
|
||||
private const string IMAGES_DIRECTORY = 'public/images';
|
||||
|
||||
public function __construct(
|
||||
private readonly string $projectDir,
|
||||
private readonly Filesystem $filesystem,
|
||||
private readonly SluggerInterface $slugger
|
||||
) {
|
||||
}
|
||||
|
||||
public function createMangaDirectory(string $mangaSlug, ?int $year): string
|
||||
{
|
||||
$year = $year ?? 'unknown';
|
||||
$directoryPath = $this->projectDir . '/' . self::CBZ_DIRECTORY . '/' . ucfirst($mangaSlug) . " ($year)";
|
||||
$this->filesystem->mkdir($directoryPath, 0755);
|
||||
return $directoryPath;
|
||||
}
|
||||
|
||||
public function createVolumeDirectory(string $mangaDir, int $volume): string
|
||||
{
|
||||
$volumeDir = sprintf('%s/volume_%02d', $mangaDir, $volume);
|
||||
$this->filesystem->mkdir($volumeDir, 0755);
|
||||
return $volumeDir;
|
||||
}
|
||||
|
||||
public function moveUploadedFile(string $sourcePath, string $destinationDir, string $originalFilename): string
|
||||
{
|
||||
$newFilename = $this->generateUniqueFilename($originalFilename);
|
||||
$destinationPath = $destinationDir . '/' . $newFilename;
|
||||
$this->filesystem->rename($sourcePath, $destinationPath, true);
|
||||
return $destinationPath;
|
||||
}
|
||||
|
||||
public function deleteFile(string $filePath): void
|
||||
{
|
||||
if ($this->filesystem->exists($filePath)) {
|
||||
$this->filesystem->remove($filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteDirectory(string $directoryPath): void
|
||||
{
|
||||
if ($this->filesystem->exists($directoryPath)) {
|
||||
$this->filesystem->remove($directoryPath);
|
||||
}
|
||||
}
|
||||
|
||||
public function fileExists(string $filePath): bool
|
||||
{
|
||||
return $this->filesystem->exists($filePath);
|
||||
}
|
||||
|
||||
public function moveFile(string $sourcePath, string $destinationPath): void
|
||||
{
|
||||
$this->filesystem->rename($sourcePath, $destinationPath, true);
|
||||
}
|
||||
|
||||
public function getUploadsDirectory(): string
|
||||
{
|
||||
return $this->projectDir . '/' . self::UPLOADS_DIRECTORY;
|
||||
}
|
||||
|
||||
private function generateUniqueFilename(string $originalFilename): string
|
||||
{
|
||||
$safeFilename = $this->slugger->slug(pathinfo($originalFilename, PATHINFO_FILENAME));
|
||||
return $safeFilename . '-' . uniqid() . '.' . pathinfo($originalFilename, PATHINFO_EXTENSION);
|
||||
}
|
||||
|
||||
public function getImagePath(string $subDir = ''): string
|
||||
{
|
||||
return $this->projectDir . '/' . self::IMAGES_DIRECTORY . ($subDir ? "/$subDir" : '');
|
||||
}
|
||||
|
||||
public function generateUniqueImageFilename(string $originalFilename): string
|
||||
{
|
||||
$safeFilename = $this->slugger->slug(pathinfo($originalFilename, PATHINFO_FILENAME));
|
||||
return $safeFilename . '-' . uniqid() . '.jpg';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user