예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function normalize($object, $format = null, array $context = array())
 {
     if ($object instanceof Folder) {
         return ['id' => $object->getId(), 'name' => $object->getName(), 'parent' => $object->getParent() ? $object->getParent()->getId() : null, 'size_bytes' => 0, 'size' => '', 'create_ts' => $object->getCreatedAt()->getTimestamp(), 'created' => $object->getCreatedAt()->format('d-m-Y H:i'), 'thumb' => null, 'type' => 'folder', 'file_count' => $object->getFiles()->count(), 'folder_count' => $object->getChildren()->count()];
     }
     if ($object instanceof File) {
         return ['id' => $object->getId(), 'name' => $object->getOriginalName(), 'size_bytes' => $object->getSize(), 'size' => SizeHumanizer::human($object->getSize()), 'create_ts' => $object->getCreatedAt()->getTimestamp(), 'created' => $object->getCreatedAt()->format('d-m-Y H:i'), 'thumb' => $this->getThumbnail($object), 'original' => $this->upload_helper->asset($object, 'file'), 'type' => $this->getExtension($object)];
     }
     return null;
 }
 /**
  * Gets the public path for the file associated with the uploadable
  * object.
  *
  * @param object $obj         The object.
  * @param string $mappingName The mapping name.
  * @param string $className   The object's class. Mandatory if $obj can't be used to determine it.
  *
  * @return string The public path.
  */
 public function asset($obj, $mappingName, $resize_name = null, $className = null)
 {
     $url = $this->helper->asset($obj, $mappingName, $className);
     if (is_null($resize_name)) {
         return $url;
     } elseif (!is_null($url)) {
         return ResizedNamer::getUrl($url, $resize_name);
     }
     return '';
 }
 public function serialize(JsonSerializationVisitor $visitor, OwnerData $owner, array $type)
 {
     $scheme = $this->request->getScheme() . '://' . $this->request->getHttpHost();
     $data = $owner->getData();
     if ($owner->getAvatarFileName()) {
         $data['avatar_file_path'] = $this->uh->asset($owner, 'avatar');
     } else {
         $data['avatar_file_path'] = $scheme . $owner->getDefaultAvatar();
     }
     return $data;
 }
예제 #4
0
 /**
  * Serialize a single file.
  *
  * @param AbstractFile $File
  *
  * @return array
  */
 public function serialize(AbstractFile $file)
 {
     $fileFile = $file->getFile();
     $data = ['id' => $file->getId(), 'name' => $file->getFileName(), 'path' => $this->vich->asset($file, 'file'), 'size' => $this->formatSize(is_null($fileFile) ? $file->getFileSize() : $fileFile->getSize()), 'mime' => is_null($fileFile) ? $file->getMimeType() : $fileFile->getMimeType()];
     if ($file->isImage()) {
         $fileName = $this->vich->asset($file, 'file');
         $data['thumb'] = $this->imagine->getBrowserPath($fileName, 'tgmedia_thumbnail');
     } else {
         $data['type'] = $file->getExtension();
     }
     return $data;
 }
예제 #5
0
 /**
  * Checks if file is uploaded.
  *
  * @param mixed  $object
  * @param string $field
  *
  * @return bool|string
  */
 public function is_uploaded($object, $field)
 {
     if ($object === null) {
         return false;
     }
     try {
         $this->em->initializeObject($object);
         return $this->uploader->asset($object, $field);
     } catch (InvalidArgumentException $e) {
         return false;
     }
 }
 /**
  * @param PreSerializeEvent $event
  */
 public function onPreSerializeFile(PreSerializeEvent $event)
 {
     $entity = $event->getObject();
     $type = $event->getType();
     $className = $type['name'];
     $request = $this->requestStack->getCurrentRequest();
     $isUploadable = $this->metadataReader->isUploadable($className);
     if (!$isUploadable) {
         return;
     }
     $cacheKey = md5($className . '_' . $entity->getId());
     if (isset(self::$cache[$cacheKey])) {
         return;
     }
     self::$cache[$cacheKey] = true;
     $uploadableFields = $this->metadataReader->getUploadableFields($className);
     foreach ($uploadableFields as $uploadableField) {
         $propertyName = $uploadableField['propertyName'];
         $fileNameProperty = $uploadableField['fileNameProperty'];
         $getterFile = 'get' . $fileNameProperty;
         if ($entity->{$getterFile}()) {
             $setterFile = 'set' . $fileNameProperty;
             $originUrl = $this->uploaderHelper->asset($entity, $propertyName);
             if ($originUrl) {
                 /** @var ImagineFilters $imagineFiltersAnnotation */
                 $reflectionClass = new \ReflectionClass($className);
                 $imagineFiltersAnnotation = $this->annotationsReader->getPropertyAnnotation($reflectionClass->getProperty($fileNameProperty), new ImagineFilters());
                 if ($imagineFiltersAnnotation) {
                     $urls = [];
                     foreach ($imagineFiltersAnnotation->getFilters() as $filter) {
                         $urls[$filter] = $this->imagineHelper->filter($originUrl, $filter);
                     }
                     $property = $imagineFiltersAnnotation->getProperty();
                     $entity->{$property} = $urls;
                 }
             }
             if ($request) {
                 $originUrl = $request->getSchemeAndHttpHost() . $originUrl;
             }
             $entity->{$setterFile}($originUrl);
         }
     }
 }
 /**
  * Gets the public path for the file associated with the uploadable
  * object.
  * 
  * @param object $obj The object.
  * @param string $field The field.
  * @return string The public path.
  */
 public function asset($obj, $field)
 {
     return $this->helper->asset($obj, $field);
 }
 /**
  * Gets the public path for the file associated with the uploadable
  * object.
  *
  * @param object $obj       The object.
  * @param string $fieldName The field name.
  * @param string $className The object's class. Mandatory if $obj can't be used to determine it.
  *
  * @return string The public path.
  */
 public function asset($obj, $fieldName, $className = null)
 {
     return $this->helper->asset($obj, $fieldName, $className);
 }
예제 #9
0
 /**
  * {@inheritdoc}
  */
 public function asset($obj, $fieldName)
 {
     return $this->uploaderHelper->asset($obj, $fieldName);
 }