You are here

function drupagram_local_image in Drupagram 7

Retrieves a local fid of a remote image. Attempts to save it if none is found.

1 call to drupagram_local_image()
drupagram_views_handler_field_images::render in ./drupagram_views_field_handlers.inc
Render the field.
1 string reference to 'drupagram_local_image'
drupagram_update_7100 in ./drupagram.install
Adds the drupagram_local_image table.

File

./drupagram.module, line 337
Provides API integration with the Instagram microblogging service.

Code

function drupagram_local_image($path) {
  global $user;
  $uri = !empty($path) ? file_default_scheme() . '://' . basename($path) : NULL;
  $file_object = _drupagram_load_data($path);
  if (!empty($file_object)) {
    return $file_object;
  }

  // Managed operations work with a file object.
  $result = drupal_http_request($path);
  if ($result->code == 200) {
    $file_object = file_save_data($result->data, $uri, FILE_EXISTS_RENAME);
    if (!empty($file_object)) {
      if ($user->uid == 1) {
        $url = file_create_url($file_object->uri);
        drupal_set_message(t('Saved managed file: %file to destination %destination (accessible via !url, actual uri=<span id="uri">@uri</span>)', array(
          '%file' => print_r($file_object, TRUE),
          '%destination' => $uri,
          '@uri' => $file_object->uri,
          '!url' => l(t('this URL'), $url),
        )));
      }

      // Save the reference
      $a = db_insert('drupagram_local_image')
        ->fields(array(
        'path' => substr($path, 0, 255),
        'file_object' => serialize($file_object),
      ))
        ->execute();
      return $file_object;
    }
  }
  if ($user->uid == 1) {
    drupal_set_message(t('Failed to save the managed file'), 'error');
  }
}