You are here

function s3fs_cors_upload_value in S3 File System CORS Upload 7

Value callback for s3fs_cors_upload element type.

1 call to s3fs_cors_upload_value()
s3fs_cors_field_widget_value in ./s3fs_cors.module
The #value_callback for the s3fs_cors field element.
1 string reference to 's3fs_cors_upload_value'
s3fs_cors_element_info in ./s3fs_cors.module
Implements hook_element_info().

File

./s3fs_cors.module, line 330
Allow uploading of files directly to AmazonS3 via the browser using CORS.

Code

function s3fs_cors_upload_value(&$element, $input = FALSE, $form_state = NULL) {
  global $user;
  $fid = 0;
  $return = array();
  $parents = $element['#parents'];
  $parents_id = implode('_', $parents);
  if (!empty($input['fid'])) {

    // The input will have a non-zero fid only when saving the full node form.
    // We don't want to do anything when that happens, because everything has
    // already been done in the AJAX workflow.
    return $input;
  }
  $remove_button_clicked = isset($form_state['input']['_triggering_element_name']) && $form_state['input']['_triggering_element_name'] == "{$parents_id}_remove_button";

  // TODO: I'm relatively sure this is useless, because of how we deal with the files.
  // But just in case, I'm going to leave it around for now.
  // Find the current value of this field from the form state, if it's there.
  $form_state_fid = $form_state['values'];
  foreach ($parents as $parent) {
    $form_state_fid = isset($form_state_fid[$parent]) ? $form_state_fid[$parent] : 0;
  }
  if ($element['#extended'] && isset($form_state_fid['fid'])) {
    $fid = $form_state_fid['fid'];
  }
  elseif (is_numeric($form_state_fid)) {
    $fid = $form_state_fid;
  }

  // If there's valid input, save the new upload.
  if ($input !== FALSE && $fid == 0 && !empty($input['filename']) && !$remove_button_clicked) {
    $return = $input;
    $base_dir = 's3://';
    if (!empty($element['#upload_location'])) {
      $base_dir = $element['#upload_location'];
      if (!preg_match('/\\/$/', $base_dir)) {
        $base_dir .= '/';
      }
    }
    if (module_exists('transliteration') && variable_get('transliteration_file_uploads', 1)) {
      $input['filename'] = transliteration_clean_filename($input['filename']);
    }

    // Construct a Drupal file object.
    $file = new stdClass();
    $file->uid = $user->uid;
    $file->filename = $input['filename'];
    $file->filesize = $input['filesize'];
    $file->filemime = $input['filemime'];
    $file->uri = file_destination("{$base_dir}{$input['filename']}", FILE_EXISTS_RENAME);
    $file->status = 0;
    $file->timestamp = REQUEST_TIME;

    // Save the uploaded file to the file_managed table.
    $file = _s3fs_cors_file_save($file);
    $return['fid'] = $file->fid;

    // Store the file's metadata into s3fs's metadata cache.
    $wrapper = new S3fsStreamWrapper();
    $wrapper
      ->writeUriToCache($file->uri);
  }
  if ($input === FALSE || $remove_button_clicked) {

    // If there is no input, or the remove button was just clicked, set the
    // default value.
    if ($element['#extended']) {
      $default_fid = isset($element['#default_value']['fid']) ? $element['#default_value']['fid'] : 0;
      $return = isset($element['#default_value']) ? $element['#default_value'] : array(
        'fid' => 0,
      );
    }
    else {
      $default_fid = isset($element['#default_value']) ? $element['#default_value'] : 0;
      $return = array(
        'fid' => 0,
      );
    }

    // Confirm that the file exists when used as a default value.
    if ($default_fid && ($file = file_load($default_fid))) {
      $return['fid'] = $file->fid;
    }
    else {
      $return['fid'] = $fid;
    }
  }
  return $return;
}