entityManager = $entityManager; $this->file_manager_warehouse = $file_manager_warehouse; } /** * @param FileItem $element * @return bool */ public function quickUpdate(FileItem $element): bool { if (!$this->entityManager->contains($element)) { try { $this->entityManager->persist($element); } catch (ORMException $e) { return false; } } try { $this->entityManager->flush(); } catch (OptimisticLockException $e) { return false; } catch (ORMException $e) { return false; } return true; } /** * @param FileItem $element * @return bool */ public function delete(FileItem $element) { try { $this->entityManager->remove($element); } catch (ORMException $e) { return false; } try { $this->entityManager->flush(); } catch (OptimisticLockException $e) { return false; } catch (ORMException $e) { return false; } return true; } /** * @param string $originalPath * @param string $name * @param Box|null $box * @param Directory $parentFolder * @return FileItem|null */ public function create(string $originalPath, string $name, ?Box $box = null, ?Directory $parentFolder = null){ $fileItem = new FileItem($originalPath, $name, $box, $parentFolder); if (!$this->quickUpdate($fileItem)) return null; if($this->move($fileItem)) return $fileItem; return null; } /** * @param FileItem $fileItem * @return bool|FileItem|null */ private function move(FileItem $fileItem){ $directory_storage = substr($fileItem->getGuid(),0,32); if(!is_dir($this->file_manager_warehouse.DIRECTORY_SEPARATOR.$directory_storage)) mkdir($this->file_manager_warehouse.DIRECTORY_SEPARATOR.$directory_storage); $fileItem->setPath($this->file_manager_warehouse.DIRECTORY_SEPARATOR.$directory_storage.DIRECTORY_SEPARATOR); if(!rename($fileItem->getOriginalPath(), $this->file_manager_warehouse.DIRECTORY_SEPARATOR.$directory_storage.DIRECTORY_SEPARATOR.$fileItem->getGuid())) return false; $fileItem->setBasePath($this->file_manager_warehouse.DIRECTORY_SEPARATOR.$directory_storage.DIRECTORY_SEPARATOR); return $this->quickUpdate($fileItem); } /** * @param string $guid * @return FileItem|object|null */ public function getByGUID(string $guid){ return $this->entityManager->getRepository(FileItem::class)->findOneBy(['guid'=>$guid]); } /** * @param FileItem $fileItem * @return mixed */ public function deleteFileItem(FileItem $fileItem){ return $this->delete($fileItem); } public function canView(FileItem $fileItem, CustomUserInterface $user){ return true; } public function getAllByGuid(array $array_guid){ $files_item = []; foreach ($array_guid as $guid) { $files_item[] = $this->getByGUID($guid); } return $files_item; } }