|
|
@@ -3,20 +3,290 @@ declare(strict_types=1);
|
|
|
|
|
|
namespace App\Service\Storage;
|
|
|
|
|
|
+use App\Entity\Access\Access;
|
|
|
+use App\Entity\Core\File;
|
|
|
+use App\Entity\Organization\Organization;
|
|
|
+use App\Entity\Person\Person;
|
|
|
+use App\Enum\Core\FileStatusEnum;
|
|
|
+use App\Enum\Core\FileTypeEnum;
|
|
|
use App\Service\Utils\Path;
|
|
|
+use DateTime;
|
|
|
+use Doctrine\ORM\EntityManagerInterface;
|
|
|
+use Exception;
|
|
|
+use Gaufrette\Filesystem;
|
|
|
use Knp\Bundle\GaufretteBundle\FilesystemMap;
|
|
|
+use Mimey\MimeTypes;
|
|
|
+use Ramsey\Uuid\Uuid;
|
|
|
+use RuntimeException;
|
|
|
|
|
|
/**
|
|
|
- * Base class for file storage
|
|
|
+ * Read and write files into the file storage
|
|
|
*/
|
|
|
-abstract class FileStorage
|
|
|
+class FileStorage
|
|
|
{
|
|
|
+ /**
|
|
|
+ * Key of the gaufrette storage, as defined in config/packages/knp_gaufrette.yaml
|
|
|
+ */
|
|
|
+ protected const FS_KEY = 'storage';
|
|
|
+
|
|
|
+ protected Filesystem $filesystem;
|
|
|
+
|
|
|
public function __construct(
|
|
|
- protected FilesystemMap $filesystem
|
|
|
+ protected FilesystemMap $filesystemMap,
|
|
|
+ protected EntityManagerInterface $entityManager
|
|
|
)
|
|
|
- {}
|
|
|
+ {
|
|
|
+ $this->filesystem = $filesystemMap->get(static::FS_KEY);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Return true if the file exists in the file storage
|
|
|
+ *
|
|
|
+ * @param File $file
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function exists(File $file): bool {
|
|
|
+ return $this->filesystem->has($file->getSlug());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Lists all the non-temporary files of the given owner
|
|
|
+ *
|
|
|
+ * @param Organization|Access|Person $owner
|
|
|
+ * @param FileTypeEnum|null $type
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function listByOwner (
|
|
|
+ Organization | Access | Person $owner,
|
|
|
+ ?FileTypeEnum $type = null
|
|
|
+ ): array {
|
|
|
+ return $this->filesystem->listKeys(
|
|
|
+ $this->getPrefix($owner, false, $type?->getValue())
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Reads the given file and returns its content as a string
|
|
|
+ *
|
|
|
+ * @param File $file
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public function read(File $file): string
|
|
|
+ {
|
|
|
+ return $this->filesystem->read($file->getSlug());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Prepare a File record with a PENDING status.
|
|
|
+ * This record will hold all the data needed to create the file, except its content.
|
|
|
+ *
|
|
|
+ * @param Organization|Access|Person $owner Owner of the file, either an organization, a person or both (access)
|
|
|
+ * @param string $filename The file's name (mandatory)
|
|
|
+ * @param FileTypeEnum $type The type of the new file
|
|
|
+ * @param Access $createdBy Id of the access responsible for this creation
|
|
|
+ * @param bool $isTemporary Is it a temporary file that can be deleted after some time
|
|
|
+ * @param string|null $mimeType Mimetype of the file, if not provided, the method will try to guess it from its file name's extension
|
|
|
+ * @param string $visibility
|
|
|
+ * @param bool $flushFile Should the newly created file be flushed after having been persisted?
|
|
|
+ * @return File
|
|
|
+ */
|
|
|
+ public function prepareFile(
|
|
|
+ Organization | Access | Person $owner,
|
|
|
+ string $filename,
|
|
|
+ FileTypeEnum $type,
|
|
|
+ Access $createdBy,
|
|
|
+ bool $isTemporary = false,
|
|
|
+ string $visibility = 'NOBODY',
|
|
|
+ string $mimeType = null,
|
|
|
+ bool $flushFile = true
|
|
|
+ ): File
|
|
|
+ {
|
|
|
+ $file = (new File())
|
|
|
+ ->setName($filename)
|
|
|
+ ->setSlug(null)
|
|
|
+ ->setType($type->getValue())
|
|
|
+ ->setVisibility($visibility)
|
|
|
+ ->setIsTemporaryFile($isTemporary)
|
|
|
+ ->setMimeType($mimeType ?? $this->guessMimeTypeFromFilename($filename))
|
|
|
+ ->setCreatedBy($createdBy->getId())
|
|
|
+ ->setStatus(FileStatusEnum::PENDING()->getValue());
|
|
|
+
|
|
|
+ if ($owner instanceof Access) {
|
|
|
+ $file->setOrganization($owner->getOrganization())
|
|
|
+ ->setPerson($owner->getPerson());
|
|
|
+ } else if ($owner instanceof Organization) {
|
|
|
+ $file->setOrganization($owner);
|
|
|
+ } else if ($owner instanceof Person) {
|
|
|
+ $file->setPerson($owner);
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->entityManager->persist($file);
|
|
|
+
|
|
|
+ if ($flushFile) {
|
|
|
+ $this->entityManager->flush();
|
|
|
+ }
|
|
|
+
|
|
|
+ return $file;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Write the $content into the file storage and update the given File object's size, slug, status (READY)...
|
|
|
+ *
|
|
|
+ * @param File $file The file object that is about to be written
|
|
|
+ * @param string $content The content of the file
|
|
|
+ * @param Access $author The access responsible for the creation / update of the file
|
|
|
+ * @return File
|
|
|
+ */
|
|
|
+ public function writeFile(File $file, string $content, Access $author): File
|
|
|
+ {
|
|
|
+ if (empty($file->getName())) {
|
|
|
+ throw new RuntimeException('File has no filename');
|
|
|
+ }
|
|
|
+
|
|
|
+ /** @noinspection ProperNullCoalescingOperatorUsageInspection */
|
|
|
+ $prefix = $this->getPrefix(
|
|
|
+ $file->getOrganization() ?? $file->getPerson(),
|
|
|
+ $file->getIsTemporaryFile(),
|
|
|
+ $file->getType()
|
|
|
+ );
|
|
|
+
|
|
|
+ try {
|
|
|
+ $uid = date('Ymd_His') . '_' . substr(Uuid::uuid4()->toString(), 0, 5);
|
|
|
+ } catch (Exception $e) {
|
|
|
+ throw new RuntimeException('Error while generating the uuid', 0, $e);
|
|
|
+ }
|
|
|
+
|
|
|
+ $isNew = $file->getSlug() === null;
|
|
|
+ $key = $file->getSlug() ?? Path::join($prefix, $uid, $file->getName());
|
|
|
+
|
|
|
+ if (!$isNew && !$this->filesystem->has($key)) {
|
|
|
+ throw new RuntimeException('The file `' . $key . '` does not exist in the file storage');
|
|
|
+ }
|
|
|
+
|
|
|
+ $size = $this->filesystem->write($key, $content, true);
|
|
|
+
|
|
|
+ $file->setSize($size)
|
|
|
+ ->setStatus(FileStatusEnum::READY()->getValue());
|
|
|
+
|
|
|
+ if ($isNew) {
|
|
|
+ $file->setSlug($key)
|
|
|
+ ->setCreateDate(new DateTime())
|
|
|
+ ->setCreatedBy($author->getId());
|
|
|
+ } else {
|
|
|
+ $file->setUpdateDate(new DateTime())
|
|
|
+ ->setUpdatedBy($author->getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->entityManager->flush();
|
|
|
+
|
|
|
+ return $file;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Convenient method to successively prepare and write a file
|
|
|
+ *
|
|
|
+ * @param Organization|Access|Person $owner
|
|
|
+ * @param string $filename
|
|
|
+ * @param FileTypeEnum $type
|
|
|
+ * @param string $content
|
|
|
+ * @param Access $author
|
|
|
+ * @param bool $isTemporary
|
|
|
+ * @param string|null $mimeType
|
|
|
+ * @param string $visibility
|
|
|
+ * @return File
|
|
|
+ */
|
|
|
+ public function makeFile (
|
|
|
+ Organization | Access | Person $owner,
|
|
|
+ string $filename,
|
|
|
+ FileTypeEnum $type,
|
|
|
+ string $content,
|
|
|
+ Access $author,
|
|
|
+ bool $isTemporary = false,
|
|
|
+ string $mimeType = null,
|
|
|
+ string $visibility = 'NOBODY'
|
|
|
+ ): File
|
|
|
+ {
|
|
|
+ $file = $this->prepareFile(
|
|
|
+ $owner,
|
|
|
+ $filename,
|
|
|
+ $type,
|
|
|
+ $author,
|
|
|
+ $isTemporary,
|
|
|
+ $mimeType,
|
|
|
+ $visibility,
|
|
|
+ false
|
|
|
+ );
|
|
|
+
|
|
|
+ return $this->writeFile($file, $content, $author);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Delete the given file from the filesystem and update the status of the File
|
|
|
+ *
|
|
|
+ * @param File $file
|
|
|
+ * @param Access $author
|
|
|
+ * @return File
|
|
|
+ */
|
|
|
+ public function delete(File $file, Access $author): File
|
|
|
+ {
|
|
|
+ $deleted = $this->filesystem->delete($file->getSlug());
|
|
|
+
|
|
|
+ if (!$deleted) {
|
|
|
+ throw new RuntimeException('File `' . $file->getSlug() . '` could\'nt be deleted');
|
|
|
+ }
|
|
|
+
|
|
|
+ $file->setStatus(FileStatusEnum::DELETED()->getValue())
|
|
|
+ ->setSize(0)
|
|
|
+ ->setUpdatedBy($author->getId());
|
|
|
+
|
|
|
+ return $file;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * If an organization or access owns the file, the prefix will be '(_temp_/)organization/{id}'.
|
|
|
+ * If a person owns it, the prefix will be '{temp}/person/{id}'
|
|
|
+ *
|
|
|
+ * With {id} being the id of the organization or of the person.
|
|
|
+ *
|
|
|
+ * If the file is temporary, '_temp_/' is prepended to the prefix.
|
|
|
+ *
|
|
|
+ * @param Organization|Access|Person $owner
|
|
|
+ * @param bool $isTemporary
|
|
|
+ * @param string|null $type
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ private function getPrefix(Organization | Access | Person $owner, bool $isTemporary, string $type = null): string
|
|
|
+ {
|
|
|
+ if ($owner instanceof Access) {
|
|
|
+ $prefix = Path::join('organization', $owner->getOrganization()?->getId(), $owner->getPerson()?->getId());
|
|
|
+ } else {
|
|
|
+ $prefix = Path::join($owner instanceof Person ? 'person' : 'organization', $owner->getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($isTemporary) {
|
|
|
+ $prefix = Path::join('_temp_', $prefix);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($type !== null) {
|
|
|
+ $prefix = Path::join($prefix, strtolower($type));
|
|
|
+ }
|
|
|
+
|
|
|
+ return $prefix;
|
|
|
+ }
|
|
|
|
|
|
- protected function getStorageBaseDir(): string {
|
|
|
- return Path::join(Path::getProjectDir(), 'var', 'files');
|
|
|
+ /**
|
|
|
+ * Try to guess the mimetype from the filename
|
|
|
+ *
|
|
|
+ * Return null if it did not manage to guess it.
|
|
|
+ *
|
|
|
+ * @param string $filename
|
|
|
+ * @return string|null
|
|
|
+ */
|
|
|
+ private function guessMimeTypeFromFilename(string $filename): string | null {
|
|
|
+ $ext = pathinfo($filename, PATHINFO_EXTENSION);
|
|
|
+ if (empty($ext)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return (new MimeTypes)->getMimeType($ext);
|
|
|
}
|
|
|
}
|