Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support files that aren't base64 encoded. #12

Open
wants to merge 2 commits into
base: 8.x-2.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/Normalizer/FileEntityNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down