/** * Move file or folder * * @param FileAbstraction $entity * @param FileAbstraction$target or null * * @throws Exception\RuntimeException */ public function move(FileAbstraction $entity, $target = null) { $entityManager = $this->getDoctrineEntityManager(); $entityManager->beginTransaction(); try { // move entity in database $entityManager->persist($entity); $otherStorageOldPath = null; if ($entity instanceof Folder) { if ($entity->isPublic()) { $otherStorageOldPath = $this->getFilesystemPath($entity, true, self::FILE_INFO_INTERNAL); } else { $otherStorageOldPath = $this->getFilesystemPath($entity, true, self::FILE_INFO_EXTERNAL); } } $oldPath = $this->getFilesystemPath($entity); if ($target instanceof Folder) { $entity->moveAsLastChildOf($target); } else { $rootLevelFolder = $entityManager->getRepository(FileAbstraction::CN())->findOneBy(array('level' => 0)); if (!$rootLevelFolder instanceof FileAbstraction) { throw new Exception\RuntimeException('Failed to find root level file'); } $entity->moveAsNextSiblingOf($rootLevelFolder); } $entityManager->flush(); $otherStorageNewPath = null; if ($entity instanceof Folder) { if ($entity->isPublic()) { $otherStorageNewPath = $this->getFilesystemPath($entity, true, self::FILE_INFO_INTERNAL); } else { $otherStorageNewPath = $this->getFilesystemPath($entity, true, self::FILE_INFO_EXTERNAL); } } $newPath = $this->getFilesystemPath($entity); if (!rename($oldPath, $newPath)) { if ($entity instanceof Folder) { $this->log()->error("Failed to move '{$oldPath}' to '{$newPath}' in filesystem"); $this->createFolderInFileSystem($newPath); } else { throw new Exception\RuntimeException('Failed to move in filesystem'); } } // move file/folder from opposite storage in file system if ($entity instanceof Folder) { if (!rename($otherStorageOldPath, $otherStorageNewPath)) { $this->log()->error("Failed to move folder from '{$otherStorageOldPath}' to '{$otherStorageNewPath}' in filesystem"); $this->createFolderInFileSystem($otherStorageNewPath); //throw new Exception\RuntimeException('Failed to move in filesystem'); } } $entityManager->commit(); } catch (\Exception $e) { $entityManager->rollback(); throw $e; } // return entity back return $entity; }
/** * Deletes file or folder */ public function deleteAction() { $repository = $this->container->getDoctrine()->getManager()->getRepository(FileAbstraction::CN()); /* @var $repository FileNestedSetRepository */ $repository->getNestedSetRepository()->lock(); $file = $this->getEntity(); $this->checkActionPermission($file, FileAbstraction::PERMISSION_DELETE_NAME); if (is_null($file)) { throw new CmsException(null, 'File doesn\'t exist anymore'); } // try to delete try { if ($file->hasChildren()) { $confirmation = $this->getConfirmation('Are You sure?'); if ($confirmation instanceof Response) { return $confirmation; } $this->removeFilesRecursively($file); } else { $this->removeSingleFile($file); } } catch (NotEmptyException $e) { // Should not happen throw new CmsException(null, "Cannot delete not empty folders"); } return new SupraJsonResponse(null); }