/** * @param File $file */ public function upload(MediaFile $file) { if ($file->getHeight() && $file->getWidth()) { $category = MediaTypes::IMAGE; } else { $category = MediaTypes::DOCUMENT; } $oCategory = $this->objectManager->getRepository('BigfishMediaBundle:Category')->find($category); $fs = new Filesystem(); $checksum = $file->getChecksum(); $checkIfExists = $this->objectManager->getRepository('BigfishMediaBundle:Attachment')->findOneBy(array('checksum' => $checksum)); if ($checkIfExists) { $fs->remove(array($file->getRealPath())); return $checkIfExists; } $baseName = str_replace('.' . $file->getExtension(), '', $file->getFilename()); $entity = new Attachment(); $entity->setChecksum($checksum); $entity->setName($baseName); $entity->setFile($file->getFilename()); $entity->setExtension($file->getExtension()); $entity->setSize($file->getSize()); $entity->setMimeType($file->getMimeType()); $entity->setWidth($file->getWidth()); $entity->setHeight($file->getHeight()); $entity->setCategory($oCategory); $this->objectManager->persist($entity); $this->objectManager->flush(); $file->move($this->uploadPath, $file->getFilename()); return $entity; }