You are here

function remote_stream_wrapper_file_add_form_submit in Remote Stream Wrapper 7

Save a file record based on a remote URL.

See also

remote_stream_wrapper_media_browser_form()

file_save()

DrupalRemoteStreamWrapper

File

./remote_stream_wrapper.module, line 275
Provides a remote stream wrapper and file field integration.

Code

function remote_stream_wrapper_file_add_form_submit($form, &$form_state) {
  $uri = $url = trim($form_state['values']['url']);
  try {
    $file = remote_stream_wrapper_file_load_by_uri($uri);
    if (!$file) {
      $file = remote_stream_wrapper_file_create_by_uri($uri);
      file_save($file);
    }
  } catch (Exception $e) {
    form_set_error('url', $e
      ->getMessage());
    return;
  }
  if (empty($file->fid)) {
    form_set_error('url', t('Unable to add file from URL %file.', array(
      '%file' => $url,
    )));
    return;
  }
  else {
    $form_state['file'] = $file;
  }
  if (drupal_valid_path('file/' . $file->fid . '/edit')) {
    $destination = array(
      'destination' => 'admin/content/file',
    );
    if (isset($_GET['destination'])) {
      $destination = drupal_get_destination();
      unset($_GET['destination']);
    }
    $form_state['redirect'] = array(
      'file/' . $file->fid . '/edit',
      array(
        'query' => $destination,
      ),
    );
  }
  else {
    $form_state['redirect'] = 'admin/content/file';
  }
}