Projet

Général

Profil

Révision ca0757b9

Ajouté par Assos Assos il y a plus de 9 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/file_entity/file_entity.pages.inc
37 37
  }
38 38

  
39 39
  $headers = array(
40
    'Content-Type' => 'force-download',
41
    'Content-Disposition' => 'attachment; filename="' . $file->filename . '"',
40
    'Content-Type' => mime_header_encode($file->filemime),
41
    'Content-Disposition' => 'attachment; filename="' . mime_header_encode(drupal_basename($file->uri)) . '"',
42 42
    'Content-Length' => $file->filesize,
43 43
    'Content-Transfer-Encoding' => 'binary',
44 44
    'Pragma' => 'no-cache',
45 45
    'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
46 46
    'Expires' => '0',
47
    'Accept-Ranges' => 'bytes',
48 47
  );
49 48

  
50 49
  // Let other modules alter the download headers.
51 50
  drupal_alter('file_download_headers', $headers, $file);
52 51

  
53
  file_transfer($file->uri, $headers);
52
  // Let other modules know the file is being downloaded.
53
  module_invoke_all('file_transfer', $file->uri, $headers);
54

  
55
  if (file_entity_file_is_local($file)) {
56
    // For local files, transfer the file and do not reveal the actual URL.
57
    file_transfer($file->uri, $headers);
58
  }
59
  else {
60
    // For remote files, just redirect the user to that file's actual URL.
61
    $headers['Location'] = file_create_url($file->uri);
62
    foreach ($headers as $name => $value) {
63
      drupal_add_http_header($name, $value);
64
    }
65
    drupal_send_headers();
66
    drupal_exit();
67
  }
54 68
}
55 69

  
56 70
/**
......
66 80
function file_entity_add_upload($form, &$form_state, array $options = array()) {
67 81
  $step = (isset($form_state['step']) && in_array($form_state['step'], array(1, 2, 3, 4))) ? $form_state['step'] : 1;
68 82
  $form['#step'] = $step;
83
  $form['#options'] = $options;
69 84
  switch ($step) {
70 85
    case 1:
71 86
      return file_entity_add_upload_step_upload($form, $form_state, $options);
......
143 158
function file_entity_add_upload_step_scheme($form, &$form_state, array $options = array()) {
144 159
  $file = file_load($form_state['storage']['upload']);
145 160

  
146
  $options = array();
161
  $schemes = array();
147 162
  foreach (file_get_stream_wrappers(STREAM_WRAPPERS_WRITE_VISIBLE) as $scheme => $info) {
148
    $options[$scheme] = check_plain($info['description']);
163
    $schemes[$scheme] = check_plain($info['description']);
164
  }
165

  
166
  // Remove any schemes not found in the instance settings.
167
  if (!empty($options['schemes'])) {
168
    $schemes = array_intersect_key($schemes, $options['schemes']);
169
  }
170

  
171
  // Determine which scheme to use as the default value.
172
  if (isset($form_state['storage']['scheme'])) {
173
    $fallback_scheme = $form_state['storage']['scheme'];
174
  }
175
  elseif (!empty($options['uri_scheme'])) {
176
    $fallback_scheme = $options['uri_scheme'];
177
  }
178
  else {
179
    $fallback_scheme = file_default_scheme();
149 180
  }
150 181

  
151 182
  $form['scheme'] = array(
152 183
    '#type' => 'radios',
153 184
    '#title' => t('Destination'),
154
    '#options' => $options,
155
    '#default_value' => isset($form_state['storage']['scheme']) ? $form_state['storage']['scheme'] : file_default_scheme(),
185
    '#options' => $schemes,
186
    '#default_value' => $fallback_scheme,
156 187
    '#required' => TRUE,
157 188
  );
158 189

  
......
179 210
  $file = file_load($form_state['storage']['upload']);
180 211
  $file->type = $form_state['storage']['type'];
181 212

  
213
  // Let users modify the filename here.
214
  $form['filename'] = array(
215
    '#type' => 'textfield',
216
    '#title' => t('Name'),
217
    '#default_value' => $file->filename,
218
    '#required' => TRUE,
219
    '#maxlength' => 255,
220
    '#weight' => -10,
221
  );
222

  
182 223
  // Add fields.
183 224
  field_attach_form('file', $file, $form, $form_state);
184 225

  
......
207 248
  foreach (file_usage_list($file) as $module => $usage) {
208 249
    $info = system_get_info('module', $module);
209 250

  
210
    // There are cases, where actual entitiy doesen't exist.
251
    // There are cases where the actual entity doesn't exist.
211 252
    // We have to handle this.
212 253
    foreach ($usage as $entity_type => $entity_ids) {
213 254
      $entity_info = entity_get_info($entity_type);
......
315 356
          $form['#step'] += ($trigger == 'edit-previous') ? -1 : 1;
316 357
          $form_state['storage']['type'] = reset($candidates_keys);
317 358
        }
318
        elseif (variable_get('file_entity_file_upload_wizard_skip_file_type', FALSE)) {
359
        elseif (!$candidates || variable_get('file_entity_file_upload_wizard_skip_file_type', FALSE)) {
319 360
          // Do not assign the file a file type.
320 361
          $form['#step'] += ($trigger == 'edit-previous') ? -1 : 1;
321 362
          $form_state['storage']['type'] = FILE_TYPE_NONE;
......
323 364
      }
324 365
      else {
325 366
        // Check if we can skip step 3.
367
        $options = $form['#options'];
326 368
        $schemes = file_get_stream_wrappers(STREAM_WRAPPERS_WRITE_VISIBLE);
327
        if (count($schemes) == 1) {
369

  
370
        // Remove any schemes not found in the instance settings.
371
        if (!empty($options['schemes'])) {
372
          $schemes = array_intersect_key($schemes, $options['schemes']);
373
        }
374

  
375
        if (!file_entity_file_is_writeable($file)) {
376
          // The file is read-only (remote) and must use its provided scheme.
377
          $form['#step'] += ($trigger == 'edit-previous') ? -1 : 1;
378
          $form_state['storage']['scheme'] = file_uri_scheme($file->uri);
379
        }
380
        elseif (count($schemes) == 1) {
328 381
          // There is only one possible stream wrapper for this file.
329 382
          // Skip the third page.
330 383
          $form['#step'] += ($trigger == 'edit-previous') ? -1 : 1;
331 384
          $form_state['storage']['scheme'] = key($schemes);
332 385
        }
333 386
        elseif (variable_get('file_entity_file_upload_wizard_skip_scheme', FALSE)) {
334
          // Assign the file the default scheme.
335 387
          $form['#step'] += ($trigger == 'edit-previous') ? -1 : 1;
336
          $form_state['storage']['scheme'] = file_default_scheme();
388

  
389
          // Fallback to the URI scheme specified in the field settings
390
          // otherwise use the default file scheme.
391
          if (!empty($options['uri_scheme'])) {
392
            $form_state['storage']['scheme'] = $options['uri_scheme'];
393
          }
394
          else {
395
            $form_state['storage']['scheme'] = file_default_scheme();
396
          }
337 397
        }
338 398
      }
339 399
    }
......
480 540
    $form['upload']['#upload_location'] . '/' :
481 541
    variable_get('file_default_scheme', 'public') . '://';
482 542

  
543
  // Ensure writable destination directory for the files.
544
  file_prepare_directory($upload_location, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
545

  
483 546
  // We can't use file_save_upload() because of
484 547
  // http://www.jacobsingh.name/content/tight-coupling-no-not.
485 548
  foreach ($form_state['values']['upload'] as $uploaded_file) {
......
568 631
  if (file_entity_file_is_writeable($file)) {
569 632
    // Set up replacement file validation.
570 633
    $replacement_options = array();
571
    // The replacement file must have the same extension as the original file.
572
    $replacement_options['file_extensions'] = pathinfo($file->uri, PATHINFO_EXTENSION);
634

  
635
    // The replacement file must have an extension valid for the original type.
636
    $file_extensions = array();
637
    $file_type_name = isset($file->type) ? $file->type : file_get_type($file);
638
    if ($file_type_name && ($file_type = file_type_load($file_type_name))) {
639
      $file_extensions = file_type_get_valid_extensions($file_type);
640
    }
641

  
642
    // Set allowed file extensions.
643
    if (!empty($file_extensions)) {
644
      // Set to type based file extensions.
645
      $replacement_options['file_extensions'] = implode(' ', $file_extensions);
646
    }
647
    else {
648
      // Fallback to the extension of the current file.
649
      $replacement_options['file_extensions'] = pathinfo($file->uri, PATHINFO_EXTENSION);
650
    }
573 651

  
574 652
    $form['replace_upload'] = array(
575 653
      '#type' => 'file',
......
656 734
    '#value' => t('Save'),
657 735
    '#weight' => 5,
658 736
    '#submit' => array('file_entity_edit_submit'),
737
    '#validate' => array('file_entity_edit_validate'),
659 738
  );
660 739
  $form['actions']['delete'] = array(
661 740
    '#type' => 'submit',
......
722 801
 */
723 802
function file_entity_edit_submit($form, &$form_state) {
724 803
  $file = $form_state['file'];
804
  $orphaned_uri = '';
725 805

  
726 806
  // Check if a replacement file has been uploaded.
727 807
  if (!empty($form_state['values']['replace_upload'])) {
728 808
    $replacement = $form_state['values']['replace_upload'];
729 809
    // Move file from temp to permanent home.
730
    file_unmanaged_copy($replacement->uri, $file->uri, FILE_EXISTS_REPLACE);
810
    $destination_uri = file_uri_scheme($file->uri) . '://' . file_uri_target($replacement->uri);
811
    $replace_mode = $destination_uri == $file->uri ? FILE_EXISTS_REPLACE : FILE_EXISTS_RENAME;
812
    if ($new_file_uri = file_unmanaged_copy($replacement->uri, $destination_uri, $replace_mode)) {
813
      // @todo Add watchdog() about replaced file here?
814

  
815
      // Remove temporary file.
816
      file_delete($replacement);
817

  
818
      // Update if the uri target has changed.
819
      if ($new_file_uri != $file->uri) {
820
       // Store the original file uri to delete if save is sucessful.
821
       $orphaned_uri = $file->uri;
822

  
823
        // Update file entity uri.
824
        $file->uri = $new_file_uri;
825
      }
826
    }
731 827
  }
732 828

  
733 829
  // Run entity form submit handling and save the file.
......
767 863
  watchdog('file', '@type: updated %title.', $args);
768 864
  drupal_set_message(t('@type %title has been updated.', $args));
769 865

  
866
  // Clean up orphaned file.
867
  if (!empty($orphaned_uri)) {
868
    file_unmanaged_delete($orphaned_uri);
869

  
870
    $args['@orphaned'] = file_uri_target($orphaned_uri);
871
    watchdog('file', '@type: deleted orphaned file @orphaned for %title.', $args);
872
    drupal_set_message(t('The replaced @type @orphaned has been deleted.', $args));
873
  }
874

  
770 875
  $form_state['redirect'] = 'file/' . $file->fid;
771 876

  
772 877
  // Clear the page and block caches.
......
984 1089

  
985 1090
  return $validators;
986 1091
}
1092

  
1093
function file_entity_upload_archive_form($form, &$form_state) {
1094
  $options = array(
1095
    'file_extensions' => archiver_get_extensions(),
1096
  );
1097

  
1098
  $form['upload'] = array(
1099
    '#type' => 'managed_file',
1100
    '#title' => t('Upload an archive file'),
1101
    '#upload_location' => NULL, // Upload to the temporary directory.
1102
    '#upload_validators' => file_entity_get_upload_validators($options),
1103
    '#progress_indicator' => 'bar',
1104
    '#required' => TRUE,
1105
    '#pre_render' => array('file_managed_file_pre_render', 'file_entity_upload_validators_pre_render'),
1106
  );
1107

  
1108
  $form['pattern'] = array(
1109
    '#type' => 'textfield',
1110
    '#title' => t('Pattern'),
1111
    '#description' => t('Only files matching this pattern will be imported. For example, to import all jpg and gif files, the pattern would be <em>*.jpg|*.gif</em>. Use <em>.*</em> to extract all files in the archive.'),
1112
    '#default_value' => '.*',
1113
    '#required' => TRUE,
1114
  );
1115

  
1116
  $form['actions'] = array('#type' => 'actions');
1117
  $form['actions']['submit'] = array(
1118
    '#type' => 'submit',
1119
    '#value' => t('Submit'),
1120
  );
1121

  
1122
  form_load_include($form_state, 'inc', 'file_entity', 'file_entity.pages');
1123

  
1124
  return $form;
1125
}
1126

  
1127
/**
1128
 * Upload a file.
1129
 */
1130
function file_entity_upload_archive_form_submit($form, &$form_state) {
1131
  $form_state['files'] = array();
1132

  
1133
  if ($archive = file_load($form_state['values']['upload'])) {
1134
    if ($archiver = archiver_get_archiver($archive->uri)) {
1135
      $files = $archiver->listContents();
1136

  
1137
      $extract_dir = file_default_scheme() . '://' . pathinfo($archive->filename, PATHINFO_FILENAME);
1138
      $extract_dir = file_destination($extract_dir, FILE_EXISTS_RENAME);
1139
      if (!file_prepare_directory($extract_dir, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY)) {
1140
        throw new Exception(t('Unable to prepar, e directory %dir for extraction.', array('%dir' => $extract_dir)));
1141
      }
1142

  
1143
      $archiver->extract($extract_dir);
1144
      $pattern = '/' . $form_state['values']['pattern'] . '/';
1145
      if ($files = file_scan_directory($extract_dir, $pattern)) {
1146
        foreach ($files as $file) {
1147
          $file->status = FILE_STATUS_PERMANENT;
1148
          $file->uid = $archive->uid;
1149
          file_save($file);
1150
          $form_state['files'][$file->fid] = $file;
1151
        }
1152
      }
1153
      drupal_set_message(t('Extracted %file and added @count new files.', array('%file' => $archive->filename, '@count' => count($files))));
1154
    }
1155
    else {
1156
      throw new Exception(t('Cannot extract %file, not a valid archive.', array('%file' => $archive->uri)));
1157
    }
1158
  }
1159

  
1160
  // Redirect to the file edit page.
1161
  if (file_entity_access('edit') && module_exists('multiform')) {
1162
    $destination = array('destination' => 'admin/content/file');
1163
    if (isset($_GET['destination'])) {
1164
      $destination = drupal_get_destination();
1165
      unset($_GET['destination']);
1166
    }
1167
    $form_state['redirect'] = array('admin/content/file/edit-multiple/' . implode(' ', array_keys($form_state['files'])), array('query' => $destination));
1168
  }
1169
  else {
1170
    $form_state['redirect'] = 'admin/content/file';
1171
  }
1172
}

Formats disponibles : Unified diff