Index: sources/remote.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/filefield_sources/sources/remote.inc,v
retrieving revision 1.6
diff -u -r1.6 remote.inc
--- sources/remote.inc	13 Mar 2010 00:09:15 -0000	1.6
+++ sources/remote.inc	8 Aug 2010 22:28:54 -0000
@@ -112,30 +112,6 @@
   if (isset($item['filefield_remote']['url']) && strlen($item['filefield_remote']['url']) > 0 && valid_url($item['filefield_remote']['url']) && $item['filefield_remote']['url'] != FILEFIELD_SOURCE_REMOTE_HINT_TEXT) {
     $field = content_fields($element['#field_name'], $element['#type_name']);
     $url = $item['filefield_remote']['url'];
-    $pathinfo = pathinfo($url);
-    $filename = rawurldecode(basename($url));
-    $filepath = file_create_filename($filename, file_directory_temp());
-
-    if (empty($pathinfo['extension'])) {
-      form_error($element, t('The remote URL must be a file and have an extension.'));
-      return;
-    }
-
-    // Perform basic extension check on the file before trying to transfer.
-    $extensions = $field['widget']['file_extensions'];
-    $regex = '/\.('. ereg_replace(' +', '|', preg_quote($extensions)) .')$/i';
-    if (!empty($extensions) && !preg_match($regex, $filename)) {
-      form_error($element, t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => $extensions)));
-      return;
-    }
-
-    $options = array(
-      'key' => $element['#type_name'] . '_' . $element['#field_name'] . '_' . $element['#delta'],
-      'filepath' => $filepath,
-    );
-
-    filefield_source_remote_set_transfer_options($options);
-
 
     // Check the headers to make sure it exists and is within the allowed size.
     $ch = curl_init();
@@ -163,6 +139,26 @@
       return;
     }
 
+    // Update the $url variable to reflect any redirects.
+    $url = $info['url'];
+    $pathinfo = pathinfo($url);
+    $filename = rawurldecode(basename($url));
+    $filepath = file_create_filename($filename, file_directory_temp());
+
+    if (empty($pathinfo['extension'])) {
+      form_error($element, t('The remote URL must be a file and have an extension.'));
+      return;
+    }
+
+    // Perform basic extension check on the file before trying to transfer.
+    $extensions = $field['widget']['file_extensions'];
+    $regex = '/\.('. ereg_replace(' +', '|', preg_quote($extensions)) .')$/i';
+    if (!empty($extensions) && !preg_match($regex, $filename)) {
+      form_error($element, t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => $extensions)));
+      return;
+    }
+
+    // Check file size based off of header information.
     if (!empty($element['#upload_validators']['filefield_validate_size'][0])) {
       $max_size = $element['#upload_validators']['filefield_validate_size'][0];
       $file_size = $info['download_content_length'];
@@ -172,6 +168,13 @@
       }
     }
 
+    // Set progress bar information.
+    $options = array(
+      'key' => $element['#type_name'] . '_' . $element['#field_name'] . '_' . $element['#delta'],
+      'filepath' => $filepath,
+    );
+    filefield_source_remote_set_transfer_options($options);
+
     // Then make the actual request to download the file.
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
