diff --git a/src/Normalizer/FileEntityNormalizer.php b/src/Normalizer/FileEntityNormalizer.php index 8ef1524..d6a95eb 100644 --- a/src/Normalizer/FileEntityNormalizer.php +++ b/src/Normalizer/FileEntityNormalizer.php @@ -38,9 +38,10 @@ public function normalize($entity, $format = NULL, array $context = array()) { if (!isset($context['included_fields']) || in_array('data', $context['included_fields'])) { // Save base64-encoded file contents to the "data" property. $file_data = base64_encode(file_get_contents($entity->getFileUri())); - $data += array( - 'data' => array(array('value' => $file_data)), - ); + // @todo these lined commented to stop entire file add to api response + // $data += array( + // 'data' => array(array('value' => $file_data)), + // ); } return $data; } @@ -52,8 +53,13 @@ public function denormalize($data, $class, $format = NULL, array $context = arra // Avoid 'data' being treated as a field. $file_data = $data['data'][0]['value']; unset($data['data']); + // If the file is not base64 encoded do something else + if ( strpos($file_data, 'http') === 0 ) { + $file_contents = file_get_contents($file_data); + } else { + $file_contents = base64_decode($file_data); + } // Decode and save to file. - $file_contents = base64_decode($file_data); $entity = parent::denormalize($data, $class, $format, $context); $dirname = drupal_dirname($entity->getFileUri()); file_prepare_directory($dirname, FILE_CREATE_DIRECTORY);