diff --git a/plugins/views_data_export_plugin_display_export.inc b/plugins/views_data_export_plugin_display_export.inc index 741d488..709e3a3 100644 --- a/plugins/views_data_export_plugin_display_export.inc +++ b/plugins/views_data_export_plugin_display_export.inc @@ -404,6 +404,7 @@ class views_data_export_plugin_display_export extends views_plugin_display_feed break; case VIEWS_DATA_EXPORT_FOOTER: + $this->outputfile_update_size(); $sandbox['finished'] = 1; $state->batch_state = VIEWS_DATA_EXPORT_FINISHED; break; @@ -576,7 +577,10 @@ class views_data_export_plugin_display_export extends views_plugin_display_feed } // Set the headers. $this->add_http_headers(); - file_transfer($this->outputfile_path(), array()); + $uri = $this->outputfile_path(); + $scheme = file_uri_scheme($uri); + $target = file_uri_target($uri); + call_user_func_array('file_download', array_merge(array($scheme), explode('/', $target))); } /** @@ -716,6 +720,18 @@ class views_data_export_plugin_display_export extends views_plugin_display_feed } } + /** + * Updates the file size in the file entity. + */ + protected function outputfile_update_size() { + $output_file = $this->outputfile_path(); + $file = current(file_load_multiple(array(), array('uri' => $output_file))); + if ($file) { + $file->filesize = filesize($output_file); + file_save($file); + } + } + function abort_export($errors) { // Just cause the next batch to do the clean-up if (!is_array($errors)) { diff --git a/views_data_export.module b/views_data_export.module index 92d9bf2..1d774b6 100644 --- a/views_data_export.module +++ b/views_data_export.module @@ -31,6 +31,42 @@ function views_data_export_views_api() { } /** + * Implements hook_file_download(). + */ +function views_data_export_file_download($uri) { + if (views_data_export_is_export_file($uri)) { + // The correct headers have already been sent, this is only necessary for + // file_download() to proceed with the download. + $result = array( + 'X-Drupal-ViewsDataExport' => 1, + ); + + // Allow only owners to access export files. + if (!user_access('access all views')) { + $file = current(entity_load('file', FALSE, array('uri' => $uri))); + if ($file && $file->uid != $GLOBALS['user']->uid) { + $result = -1; + } + } + + return $result; + } +} + +/** + * Checks whether the passed URI identifies an export file. + * + * @param string $uri + * A file URI. + * + * @return bool + * TRUE if the URI identifies an export file, FALSE otherwise. + */ +function views_data_export_is_export_file($uri) { + return file_uri_scheme($uri) == 'temporary' && strpos(file_uri_target($uri), 'views_data_export') === 0; +} + +/** * Implementation of hook_theme(). */ function views_data_export_theme() { @@ -275,7 +311,7 @@ function views_data_export_view_clear($export_id) { function views_data_export_file_presave($file) { // Ensure temporary files really are temporary. // @see: https://drupal.org/node/2198399 - if (strpos($file->filename, 'views_data_export') === 0) { + if (views_data_export_is_export_file($file->uri)) { // There is no FILE_STATUS_TEMPORARY. $file->status = 0; }