You are here

function gathercontent_ajax_media_download in GatherContent 7.2

AJAX function to process individual media item.

1 string reference to 'gathercontent_ajax_media_download'
gathercontent_menu in ./gathercontent.module
Implements hook_menu().

File

includes/media.inc, line 77
Page for importing media via AJAX.

Code

function gathercontent_ajax_media_download() {
  $out = array(
    'error' => t('Verification failed, please refreshing the page and try again.'),
  );
  if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest") {
    $obj = gathercontent_get_obj();
    $cur_num = $_POST['cur_num'];
    $cur_total = $_POST['cur_total'];
    $retry = $_POST['cur_retry'];
    $media = variable_get('gathercontent_media_files');
    $total = $media['total_files'];
    unset($media['total_files']);
    $post_id = key($media);
    if (is_array($media[$post_id]['files']) && count($media[$post_id]['files']) > 0) {
      $cur_post = $media[$post_id];
      $page_total = $cur_post['total_files'];
      $more_than_1 = count($cur_post['files'][0]) > 1;
      $file = array_shift($cur_post['files'][0]);
      $is_file_field = $file['is_file_field'];
      $field_type = $file['field_type'];
      $node = node_load($post_id);
      $entity = entity_metadata_wrapper('node', $node);
      if (!$more_than_1) {
        array_shift($cur_post['files']);
      }
      $file_obj = db_select('gathercontent_media', 'g')
        ->fields('f', array(
        'fid',
        'filename',
        'uri',
        'filemime',
      ))
        ->condition('g.gid', $file['id']);
      $file_obj
        ->join('file_managed', 'f', 'f.fid = g.fid');
      $file_obj = $file_obj
        ->execute();
      if ($file_obj
        ->rowCount() > 0) {
        $file_obj = $file_obj
          ->fetchObject();
        $obj
          ->addMediaToEntity($entity, $is_file_field, $file_obj, $file['field'], $more_than_1, $field_type, $file['counter']);
        $out = $obj
          ->getMediaAjaxOutput($post_id, $media, $cur_post, $page_total, $total);
        $out['success'] = TRUE;
      }
      else {
        $dir = 'public://';
        if (isset($field['file_directory'])) {
          $dir .= $field['file_directory'];
        }
        $filename = $file['original_filename'];
        if (substr($dir, -1) != '/') {
          $dir .= '/';
        }
        if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY)) {
          watchdog('file', 'The upload directory %directory for the file field !name could not be created or is not accessible. A newly uploaded file could not be saved in this directory as a consequence, and the upload was canceled.', array(
            '%directory' => $dir,
            '!name' => $element['#field_name'],
          ));
        }
        $destination = file_destination($dir . $filename, FILE_EXISTS_RENAME);
        $resp = $obj
          ->curl('https://gathercontent.s3.amazonaws.com/' . $file['filename']);
        if ($resp['httpcode'] == 200) {
          $fp = fopen($destination, 'w');
          if (!$fp) {
            $real_destination = drupal_realpath($destination);
            $fp = fopen($real_destination, 'w');
          }
          if ($fp) {
            fwrite($fp, $resp['response']);
            fclose($fp);
            global $user;
            $new_file = (object) array(
              'fid' => NULL,
              'uri' => $destination,
              'filename' => drupal_basename($destination),
              'filemime' => file_get_mimetype($destination),
              'uid' => $user->uid,
              'status' => FILE_STATUS_PERMANENT,
            );
            $new_file = file_save($new_file);
            $gid = (object) array(
              'fid' => $new_file->fid,
              'gid' => $file['id'],
            );
            drupal_write_record('gathercontent_media', $gid);
            $obj
              ->addMediaToEntity($entity, $is_file_field, $new_file, $file['field'], $more_than_1, $field_type, $file['counter']);
            $out = $obj
              ->getMediaAjaxOutput($post_id, $media, $cur_post, $page_total, $total);
            $out['success'] = TRUE;
          }
          else {
            if ($retry == '1') {
              $out = $obj
                ->getMediaAjaxOutput($post_id, $media, $cur_post, $page_total, $total);
              $out['success'] = FALSE;
              $out['error'] = sprintf(t('There was an error with the file (%s)'), $new_file);
            }
            else {
              $out = array(
                'success' => FALSE,
                'retry' => TRUE,
                'msg' => sprintf(t('Retrying to download (%s)'), $file['original_filename']),
              );
            }
          }
        }
        else {
          if ($retry == '1') {
            $out = $obj
              ->getMediaAjaxOutput($post_id, $media, $cur_post, $page_total, $total);
            $out['success'] = FALSE;
            $out['error'] = sprintf(t('There was an error with the file (%s)'), $new_file);
          }
          else {
            $out = array(
              'success' => FALSE,
              'retry' => TRUE,
              'msg' => sprintf(t('Retrying to download (%s)'), $file['original_filename']),
            );
          }
        }
      }
    }
    else {
      if ($retry == '1') {
        $out = $obj
          ->getMediaAjaxOutput($post_id, $media, $cur_post, $page_total, $total);
        $out['success'] = FALSE;
        $out['error'] = sprintf(t('Failed to download the file (%s)'), $file['original_filename']);
      }
      else {
        $out = array(
          'success' => FALSE,
          'retry' => TRUE,
          'msg' => sprintf(t('Retrying to download (%s)'), $file['original_filename']),
        );
      }
    }
  }
  echo json_encode($out);
  exit;
}