From 30994caa1d6f2ceb4a41b420206ce4e03b5848f5 Mon Sep 17 00:00:00 2001 From: Mr Snow Date: Mon, 26 Jan 2015 16:59:31 +1100 Subject: [PATCH 1/2] Support files that aren't base64 encoded. --- src/Normalizer/FileEntityNormalizer.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Normalizer/FileEntityNormalizer.php b/src/Normalizer/FileEntityNormalizer.php index 8ef1524..f9946af 100644 --- a/src/Normalizer/FileEntityNormalizer.php +++ b/src/Normalizer/FileEntityNormalizer.php @@ -52,8 +52,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); From 520f59b627a911a1d83be400ef7911093e068d8f Mon Sep 17 00:00:00 2001 From: James Millar Date: Wed, 15 Jul 2015 14:58:21 +1000 Subject: [PATCH 2/2] Fix issue in flipped: https://github.com/Laudanum/flipped/issues/346 by stopping creation of data attribute with file content --- src/Normalizer/FileEntityNormalizer.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Normalizer/FileEntityNormalizer.php b/src/Normalizer/FileEntityNormalizer.php index f9946af..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; }