diff --git a/includes/file.inc b/includes/file.inc index 1dd34cbae4fbe35d5c5c63d241b242e10448522a..588bea0867adeef02157a421f2630c6568b9aae6 100644 --- a/includes/file.inc +++ b/includes/file.inc @@ -2328,6 +2328,35 @@ function file_directory_temp() { return $temporary_directory; } +/** + * Examines a file object and returns appropriate content headers for download. + * + * @param $file + * A file object. + * @return + * An associative array of headers, as expected by file_transfer(). + */ +function file_get_content_headers($file) { + $name = mime_header_encode($file->filename); + $type = mime_header_encode($file->filemime); + // Serve images, text, and flash content for display rather than download. + $inline_types = variable_get('file_inline_types', array('^text/', '^image/', 'flash$')); + $disposition = 'attachment'; + foreach ($inline_types as $inline_type) { + // Exclamation marks are used as delimiters to avoid escaping slashes. + if (preg_match('!' . $inline_type . '!', $file->filemime)) { + $disposition = 'inline'; + } + } + + return array( + 'Content-Type' => $type . '; name="' . $name . '"', + 'Content-Length' => $file->filesize, + 'Content-Disposition' => $disposition . '; filename="' . $name . '"', + 'Cache-Control' => 'private', + ); +} + /** * @} End of "defgroup file". */ diff --git a/modules/file/file.module b/modules/file/file.module index 69130b2f86fcc925754809c08b00ffade56398a9..08b383555db4c74ebaf651f5fab7237572b12335 100644 --- a/modules/file/file.module +++ b/modules/file/file.module @@ -216,24 +216,8 @@ function file_file_download($uri, $field_type = 'file') { } // Access is granted. - $name = mime_header_encode($file->filename); - $type = mime_header_encode($file->filemime); - // Serve images, text, and flash content for display rather than download. - $inline_types = variable_get('file_inline_types', array('^text/', '^image/', 'flash$')); - $disposition = 'attachment'; - foreach ($inline_types as $inline_type) { - // Exclamation marks are used as delimiters to avoid escaping slashes. - if (preg_match('!' . $inline_type . '!', $file->filemime)) { - $disposition = 'inline'; - } - } - - return array( - 'Content-Type' => $type . '; name="' . $name . '"', - 'Content-Length' => $file->filesize, - 'Content-Disposition' => $disposition . '; filename="' . $name . '"', - 'Cache-Control' => 'private', - ); + $headers = file_get_content_headers($file); + return $headers; } /**