diff --git a/includes/media.pages.inc b/includes/media.pages.inc index 6660643..62da578 100644 --- a/includes/media.pages.inc +++ b/includes/media.pages.inc @@ -112,10 +112,13 @@ function media_add_upload($form, &$form_state, $params = array()) { $form['#validators'] = $validators; $form['upload'] = array( - '#type' => 'file', + '#type' => 'managed_file', '#title' => t('Upload a new file'), '#description' => theme('file_upload_help', array('description' => '', 'upload_validators' => $validators)), + '#upload_location' => media_upload_destination_uri($params), '#upload_validators' => $validators, + '#progress_indicator' => 'bar', + '#required' => TRUE, ); $form['actions'] = array('#type' => 'actions'); @@ -123,66 +126,26 @@ function media_add_upload($form, &$form_state, $params = array()) { '#type' => 'submit', '#value' => t('Submit'), ); - + + $form_state['build_info']['files']['media_upload'] = drupal_get_path('module', 'media') . '/includes/media.pages.inc'; + return $form; } /** - * Validate the generic file upload with the global media settings. - */ -function media_add_upload_validate($form, &$form_state) { - // Save the file as a temporary file. - $file = file_save_upload('upload', $form['#validators']); - - if ($file === NULL) { - form_set_error('upload', t("No file appears to have been selected.")); - } - elseif ($file === FALSE) { - form_set_error('upload', t('File upload error.')); - } - else { - $form_state['values']['upload'] = $file; - } -} - -/** * Upload a file. */ function media_add_upload_submit($form, &$form_state) { - $params = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : array(); - $file = $form_state['values']['upload']; - - // The media browser widget does not use the 'display' field. - $file->display = TRUE; - - // Change the file status from temporary to permanent. - _media_save_file_permenently($file); - - // Determine what URI scheme this file should use. - $scheme = !empty($params['uri_scheme']) && file_stream_wrapper_valid_scheme($params['uri_scheme']) ? $params['uri_scheme'] : file_default_scheme(); - $scheme .= '://'; - - // Prepare the file's subdirectory path. - $directory = ''; - if (!empty($params['file_directory'])) { - $directory = token_replace($params['file_directory']) . '/'; - - // If the directory isn't writable, or doesn't exist and can't be created, - // the upload will fail. - $prepare_directory = file_stream_wrapper_uri_normalize($scheme . $directory); - if (!file_prepare_directory($prepare_directory, FILE_CREATE_DIRECTORY)) { - drupal_set_message(t('The file directory @dir does not exist or is not writable. Please contact an administrator.', array('@dir' => $prepare_directory)), 'error'); - return; - } - } + $file = file_load($form_state['values']['upload']); - // Compose the file's permanent destination. - $destination = file_stream_wrapper_uri_normalize($scheme . $directory . $file->filename); + if ($file) { + // The media browser widget does not use the 'display' field. + $file->display = TRUE; - // Save the uploaded file to the permanent location. - $file = file_move($file, $destination, FILE_EXISTS_RENAME); + // Change the file from temporary to permanent. + $file->status = FILE_STATUS_PERMANENT; + file_save($file); - if ($file) { $form_state['file'] = $file; drupal_set_message(t('The file @name was uploaded', array('@name' => $file->filename))); } @@ -205,6 +168,33 @@ function media_add_upload_submit($form, &$form_state) { } } +/** + * Determines the upload location for the file add upload form. + * + * @param array $params + * An array of parameters from the media browser. + * @param array $data + * (optional) An array of token objects to pass to token_replace(). + * + * @return + * A file directory URI with tokens replaced. + * + * @see token_replace() + */ +function media_upload_destination_uri(array $params, array $data = array()) { + $params += array( + 'uri_scheme' => file_default_scheme(), + 'file_directory' => '', + ); + + $destination = trim($params['file_directory'], '/'); + + // Replace tokens. + $destination = token_replace($destination, $data); + + return $params['uri_scheme'] . '://' . $destination; +} + function media_add_upload_multiple($form, &$form_state, $params = array()) { $form = media_add_upload($form, $form_state, $params); unset($form['upload']['#title']);