You are here

function angular_media_file_url_to_object in CKEditor Widgets 7

Upload a file from a URL.

This will copy a file from a remote location and store it locally.

See also

media_parse_to_uri()

media_parse_to_file()

1 call to angular_media_file_url_to_object()
angular_media_callback_file in ./angular_media.module
Callback to retrieve (GET) or save (POST) updates.

File

./angular_media.module, line 367
Implementation of angular_media.module.

Code

function angular_media_file_url_to_object($remote_uri, $filename) {

  // Much of this is copied from media_internet.module
  // Coppies the remote file locally.

  //@TODO: we should follow redirection here an save the final filename, not just the basename.
  global $user;
  $local_filename = $filename;
  $local_filename = file_munge_filename($local_filename, variable_get('file_extensions'), FALSE);
  $local_uri = file_stream_wrapper_uri_normalize('temporary://' . $local_filename);
  if (!@copy($remote_uri, $local_uri)) {
    throw new Exception('Unable to add file ' . $remote_uri);
    return;
  }

  // Make the current fileObject point to the local_uri, not the remote one.
  $file = file_uri_to_object($local_uri);
  $scheme = variable_get('file_default_scheme', 'public') . '://';
  $uri = file_stream_wrapper_uri_normalize($scheme . $file->filename);

  // Now to its new home.
  $file = file_move($file, $uri, FILE_EXISTS_RENAME);

  // Add metadata
  $file->filemime = file_get_mimetype($uri);
  $file->filename = drupal_basename($uri);
  $file->uid = $user->uid;
  $file->filesize = @filesize($uri);
  $file->timestamp = REQUEST_TIME;
  $file->status = FILE_STATUS_PERMANENT;
  if (!$file->fid) {
    form_set_error('url', 'Unknown error: unable to add file, please check URL / Embed code and try again ' . $embed_code);
    return;
  }
  return $file;
}