Quellcode durchsuchen

implement organization files deletion

Olivier Massot vor 1 Jahr
Ursprung
Commit
d3f1c35e8d

+ 2 - 2
config/packages/knp_gaufrette.yaml

@@ -4,10 +4,10 @@ knp_gaufrette:
   adapters:
     storage:
       local:
-        directory: '%kernel.project_dir%/storage'
+        directory: '%kernel.project_dir%/var/files/storage'
         create: true
   filesystems:
     storage:
       adapter: storage
 
-  stream_wrapper: ~
+  stream_wrapper: ~

+ 1 - 0
config/services.yaml

@@ -23,6 +23,7 @@ services:
             $opentalentNoReplyEmailAddress: 'noreply@opentalent.fr'
             $legacyBaseUrl: '%env(PUBLIC_API_LEG_BASE_URL)%'
             $baseUrl: '%env(PUBLIC_API_BASE_URL)%'
+            $fileStorageDir: '%kernel.project_dir%/var/files/storage'
 
     # makes classes in src/ available to be used as services
     # this creates a service per class whose id is the fully-qualified class name

+ 18 - 8
src/Service/File/FileManager.php

@@ -150,12 +150,17 @@ class FileManager
     public function deleteOrganizationFiles(int $organizationId): void
     {
         foreach ($this->storageIterator->getStorages() as $storageService) {
-            $storageService->removeOrganizationDirectory($organizationId);
+            $storageService->deleteOrganizationFiles($organizationId);
         }
 
-        $this->entityManager
-            ->createQuery('DELETE FROM App\Entity\File f WHERE f.organization_id = :organization_id')
-            ->setParameter('organization_id', $organizationId)
+        $qb = $this->entityManager->createQueryBuilder();
+        $qb
+            ->delete(File::class, 'f')
+            ->where(
+                $qb->expr()->eq('f.organization', ':organizationId')
+            )
+            ->setParameter('organizationId', $organizationId)
+            ->getQuery()
             ->execute();
     }
 
@@ -169,12 +174,17 @@ class FileManager
     public function deletePersonFiles(int $personId): void
     {
         foreach ($this->storageIterator->getStorages() as $storageService) {
-            $storageService->removePersonDirectory($personId);
+            $storageService->deletePersonFiles($personId);
         }
 
-        $this->entityManager
-            ->createQuery('DELETE FROM App\Entity\File f WHERE f.organization_id = :organization_id')
-            ->setParameter('person_id', $personId)
+        $qb = $this->entityManager->createQueryBuilder();
+        $qb
+            ->delete(File::class, 'f')
+            ->where(
+                $qb->expr()->eq('f.person', ':$personId')
+            )
+            ->setParameter('$personId', $personId)
+            ->getQuery()
             ->execute();
     }
 }

+ 6 - 4
src/Service/File/Storage/ApiLegacyStorage.php

@@ -59,11 +59,12 @@ class ApiLegacyStorage implements FileStorageInterface
      * @param int $organizationId
      * @return void
      */
-    public function removeOrganizationDirectory(int $organizationId): void
+    public function deleteOrganizationFiles(int $organizationId): void
     {
+        // TODO: change the url with the definitive one
         $url = sprintf('api/files/organization/remove-all/%s', $organizationId);
 
-        $this->apiLegacyRequestService->get($url);
+        // $this->apiLegacyRequestService->get($url);
     }
 
     /**
@@ -72,10 +73,11 @@ class ApiLegacyStorage implements FileStorageInterface
      * @param int $personId
      * @return void
      */
-    public function removePersonDirectory(int $personId): void
+    public function deletePersonFiles(int $personId): void
     {
+        // TODO: change the url with the definitive one
         $url = sprintf('api/files/person/remove-all/%s', $personId);
 
-        $this->apiLegacyRequestService->get($url);
+        // $this->apiLegacyRequestService->get($url);
     }
 }

+ 2 - 2
src/Service/File/Storage/FileStorageInterface.php

@@ -16,7 +16,7 @@ interface FileStorageInterface
 
     public function support(File $file): bool;
 
-    public function removeOrganizationDirectory(int $organizationId): void;
+    public function deleteOrganizationFiles(int $organizationId): void;
 
-    public function removePersonDirectory(int $personId): void;
+    public function deletePersonFiles(int $personId): void;
 }

+ 31 - 13
src/Service/File/Storage/LocalStorage.php

@@ -47,14 +47,15 @@ class LocalStorage implements FileStorageInterface
     protected FilesystemInterface $filesystem;
 
     public function __construct(
-        protected readonly FilesystemMap $filesystemMap,
+        protected readonly FilesystemMap          $filesystemMap,
         protected readonly EntityManagerInterface $entityManager,
-        protected readonly AccessRepository $accessRepository,
-        protected readonly DataManager $dataManager,
-        protected readonly CacheManager $cacheManager,
-        protected readonly ImageFactory $imageFactory,
-        protected readonly FileUtils $fileUtils,
-        protected readonly UrlBuilder $urlBuilder
+        protected readonly AccessRepository       $accessRepository,
+        protected readonly DataManager            $dataManager,
+        protected readonly CacheManager           $cacheManager,
+        protected readonly ImageFactory           $imageFactory,
+        protected readonly FileUtils              $fileUtils,
+        protected readonly UrlBuilder             $urlBuilder,
+        protected readonly string                 $fileStorageDir
     ) {
         $this->filesystem = $filesystemMap->get(static::FS_KEY);
     }
@@ -300,10 +301,10 @@ class LocalStorage implements FileStorageInterface
      * @param int $organizationId
      * @return void
      */
-    public function removeOrganizationDirectory(int $organizationId): void
+    public function deleteOrganizationFiles(int $organizationId): void
     {
-        $this->filesystem->delete('organization/' . $organizationId);
-        $this->filesystem->delete('temp/organization/' . $organizationId);
+        $this->rrmDir('organization/' . $organizationId);
+        $this->rrmDir('temp/organization/' . $organizationId);
     }
 
     /**
@@ -312,10 +313,27 @@ class LocalStorage implements FileStorageInterface
      * @param int $personId
      * @return void
      */
-    public function removePersonDirectory(int $personId): void
+    public function deletePersonFiles(int $personId): void
     {
-        $this->filesystem->delete('person/' . $personId);
-        $this->filesystem->delete('temp/person/' . $personId);
+        $this->rrmDir('person/' . $personId);
+        $this->rrmDir('temp/person/' . $personId);
+    }
+
+    /**
+     * Supprime récursivement un répertoire
+     *
+     * (Au moment du développement, Gaufrette ne permet pas la suppression de répertoire, on laissera
+     * le soin à un cron de supprimer les répertoires vides du storage)
+     *
+     * @param string $dirKey
+     * @return void
+     */
+    protected function rrmDir(string $dirKey): void {
+        if (!$this->filesystem->isDirectory($dirKey)) {
+            throw new \RuntimeException('Directory `'.$dirKey.'` does not exist');
+        }
+        $dir = Path::join($this->fileStorageDir, $dirKey);
+        Path::rmtree($dir);
     }
 
     /**

+ 7 - 2
src/Service/Organization/OrganizationFactory.php

@@ -600,6 +600,9 @@ class OrganizationFactory
     public function delete(OrganizationDeletionRequest $organizationDeletionRequest): OrganizationDeletionRequest
     {
         $organization = $this->organizationRepository->find($organizationDeletionRequest->getOrganizationId());
+        if (!$organization) {
+            throw new \RuntimeException("No organization was found for id : " . $organizationDeletionRequest->getOrganizationId());
+        }
 
         $this->logger->info(
             "Start the deletion of organization '" . $organization->getName() . "' [" . $organization->getId() . "]"
@@ -620,7 +623,9 @@ class OrganizationFactory
             $this->entityManager->remove($organization);
 
             // Supprime les personnes qui n'avaient pas d'autre Access attaché
+            $deletedPersonIds = [];
             foreach ($orphanPersons as $person) {
+                $deletedPersonIds[] = $person->getId();
                 $this->entityManager->remove($person);
             }
 
@@ -656,9 +661,9 @@ class OrganizationFactory
             $withError = true;
         }
 
-        foreach ($orphanPersons as $person) {
+        foreach ($deletedPersonIds as $personId) {
             try {
-                $this->fileManager->deletePersonFiles($person->getId());
+                $this->fileManager->deletePersonFiles($personId);
             } catch (\Exception $e) {
                 $this->logger->critical("An error happened while deleting the person's files, please proceed manually (id=" . $person->getId() . ").");
                 $this->logger->debug($e);

+ 1 - 1
src/Service/Utils/Path.php

@@ -96,7 +96,7 @@ class Path
      *
      * @return bool returns true if the directory was successfully removed, false otherwise
      */
-    protected static function rmtree(string $path): bool
+    public static function rmtree(string $path): bool
     {
         if (!file_exists($path)) {
             return true;