- Patch #524530 by c960657: fixed the headers sent by the image module.

merge-requests/26/head
Dries Buytaert 2009-07-20 21:52:49 +00:00
parent 444ae51f17
commit 1a4a3ec577
2 changed files with 55 additions and 25 deletions

View File

@ -69,8 +69,8 @@ function image_file_download($filepath) {
$headers = module_invoke_all('file_download', $original_path); $headers = module_invoke_all('file_download', $original_path);
if (!in_array(-1, $headers)) { if (!in_array(-1, $headers)) {
return array( return array(
'Content-Type: ' . $info['mime_type'], 'Content-Type' => $info['mime_type'],
'Content-Length: ' . $info['file_size'], 'Content-Length' => $info['file_size'],
); );
} }
} }
@ -306,13 +306,17 @@ function image_style_generate() {
// Don't start generating the image if it is already in progress. // Don't start generating the image if it is already in progress.
$cid = 'generate:' . $style_name . ':' . $path_md5; $cid = 'generate:' . $style_name . ':' . $path_md5;
if (cache_get($cid, 'cache_image')) { if (cache_get($cid, 'cache_image')) {
// Tell client to retry again in 3 seconds. Currently no browsers are known
// to support Retry-After.
drupal_set_header('503 Service Unavailable');
drupal_set_header('Retry-After', 3);
print t('Image generation in progress, please try again shortly.'); print t('Image generation in progress, please try again shortly.');
exit(); exit();
} }
// If the image has already been generated then send it. // If the image has already been generated then send it.
if ($image = image_load($destination)) { if ($image = image_load($destination)) {
file_transfer($image->source, array('Content-type: ' . $image->info['mime_type'], 'Content-length: ' . $image->info['file_size'])); file_transfer($image->source, array('Content-Type' => $image->info['mime_type'], 'Content-Length' => $image->info['file_size']));
} }
// Set a cache entry designating this image as being in-process. // Set a cache entry designating this image as being in-process.
@ -322,11 +326,12 @@ function image_style_generate() {
if (image_style_create_derivative($style, $source, $destination)) { if (image_style_create_derivative($style, $source, $destination)) {
$image = image_load($destination); $image = image_load($destination);
cache_clear_all($cid, 'cache_image'); cache_clear_all($cid, 'cache_image');
file_transfer($image->source, array('Content-type: ' . $image->info['mime_type'], 'Content-length: ' . $image->info['file_size'])); file_transfer($image->source, array('Content-Type' => $image->info['mime_type'], 'Content-Length' => $image->info['file_size']));
} }
else { else {
cache_clear_all($cid, 'cache_image'); cache_clear_all($cid, 'cache_image');
watchdog('image', 'Unable to generate the derived image located at %path.', $destination); watchdog('image', 'Unable to generate the derived image located at %path.', $destination);
drupal_set_header('500 Internal Server Error');
print t('Error generating image.'); print t('Error generating image.');
exit(); exit();
} }

View File

@ -32,8 +32,8 @@
*/ */
class ImageStylesPathAndUrlUnitTest extends DrupalWebTestCase { class ImageStylesPathAndUrlUnitTest extends DrupalWebTestCase {
protected $style_name; protected $style_name;
protected $image_with_generated; protected $image_info;
protected $image_without_generated; protected $image_filepath;
public static function getInfo() { public static function getInfo() {
return array( return array(
@ -47,28 +47,25 @@ class ImageStylesPathAndUrlUnitTest extends DrupalWebTestCase {
parent::setUp(); parent::setUp();
$this->style_name = 'style_foo'; $this->style_name = 'style_foo';
image_style_save(array('name' => $this->style_name));
// Create the directories for the styles. // Create the directories for the styles.
$status = file_check_directory($d = file_directory_path() .'/styles/' . $this->style_name, FILE_CREATE_DIRECTORY); $status = file_check_directory($d = file_directory_path() . '/styles/' . $this->style_name, FILE_CREATE_DIRECTORY);
$this->assertNotIdentical(FALSE, $status, t('Created the directory for the generated images for the test style.' )); $this->assertNotIdentical(FALSE, $status, t('Created the directory for the generated images for the test style.' ));
// Make two copies of the file... // Create a working copy of the file.
$file = reset($this->drupalGetTestFiles('image')); $file = reset($this->drupalGetTestFiles('image'));
$this->image_without_generated = file_unmanaged_copy($file->filepath, NULL, FILE_EXISTS_RENAME); $this->image_info = image_get_info($file->filepath);
$this->assertNotIdentical(FALSE, $this->image_without_generated, t('Created the without generated image file.')); $this->image_filepath = file_unmanaged_copy($file->filepath, NULL, FILE_EXISTS_RENAME);
$this->image_with_generated = file_unmanaged_copy($file->filepath, NULL, FILE_EXISTS_RENAME); $this->assertNotIdentical(FALSE, $this->image_filepath, t('Created the without generated image file.'));
$this->assertNotIdentical(FALSE, $this->image_with_generated, t('Created the with generated image file.'));
// and create a "generated" file for the one.
$status = file_unmanaged_copy($file->filepath, image_style_path($this->style_name, $this->image_with_generated), FILE_EXISTS_REPLACE);
$this->assertNotIdentical(FALSE, $status, t('Created a file where the generated image should be.'));
} }
/** /**
* Test image_style_path(). * Test image_style_path().
*/ */
function testImageStylePath() { function testImageStylePath() {
$actual = image_style_path($this->style_name, $this->image_without_generated); $actual = image_style_path($this->style_name, $this->image_filepath);
$expected = file_directory_path() . '/styles/' . $this->style_name . '/' . basename($this->image_without_generated); $expected = file_directory_path() . '/styles/' . $this->style_name . '/' . basename($this->image_filepath);
$this->assertEqual($actual, $expected, t('Got the path for a file.')); $this->assertEqual($actual, $expected, t('Got the path for a file.'));
} }
@ -76,15 +73,43 @@ class ImageStylesPathAndUrlUnitTest extends DrupalWebTestCase {
* Test image_style_url(). * Test image_style_url().
*/ */
function testImageStyleUrl() { function testImageStyleUrl() {
// Test it with no generated file. // Get the URL of a file that has not been generated yet and try to access
$actual = image_style_url($this->style_name, $this->image_without_generated); // it before image_style_url has been called.
$expected = url('image/generate/' . $this->style_name . '/' . $this->image_without_generated, array('absolute' => TRUE)); $generated_path = file_directory_path() . '/styles/' . $this->style_name . '/' . basename($this->image_filepath);
$this->assertEqual($actual, $expected, t('Got the generate URL for a non-existent file.')); $this->assertFalse(file_exists($generated_path), t('Generated file does not exist.'));
$expected_generate_url = url('image/generate/' . $this->style_name . '/' . $this->image_filepath, array('absolute' => TRUE));
$this->drupalGet($expected_generate_url);
$this->assertResponse(403, t('Access to generate URL was denied.'));
// Now test it with a generated file. // Check that a generate URL is returned.
$actual = image_style_url($this->style_name, $this->image_with_generated); $actual_generate_url = image_style_url($this->style_name, $this->image_filepath);
$expected = file_create_url(image_style_path($this->style_name, $this->image_with_generated)); $this->assertEqual($actual_generate_url, $expected_generate_url, t('Got the generate URL for a non-existent file.'));
$this->assertEqual($actual, $expected, t('Got the download URL for an existing file.'));
// Fetch the URL that generates the file while another process appears to
// be generating the same file (this is signaled using cache_image).
$cid = 'generate:' . $this->style_name . ':' . md5($this->image_filepath);
cache_set($cid, $generated_path, 'cache_image');
$this->drupalGet($expected_generate_url);
$this->assertResponse(503, t('Service Unavailable response received.'));
$this->assertTrue($this->drupalGetHeader('Retry-After'), t('Retry-After header received.'));
cache_clear_all($cid, 'cache_image');
// Fetch the URL that generates the file.
$this->drupalGet($expected_generate_url);
$this->assertTrue(file_exists($generated_path), t('Generated file was created.'));
$this->assertRaw(file_get_contents($generated_path), t('URL returns expected file.'));
$generated_image_info = image_get_info($generated_path);
$this->assertEqual($this->drupalGetHeader('Content-Type'), $generated_image_info['mime_type'], t('Expected Content-Type was reported.'));
$this->assertEqual($this->drupalGetHeader('Content-Length'), $generated_image_info['file_size'], t('Expected Content-Length was reported.'));
// Check that the URL points directly to the generated file.
$expected_generated_url = file_create_url($generated_path);
$actual_generated_url = image_style_url($this->style_name, $this->image_filepath);
$this->drupalGet($expected_generated_url);
$this->assertEqual($actual_generated_url, $expected_generated_url, t('Got the download URL for an existing file.'));
$this->assertRaw(file_get_contents($generated_path), t('URL returns expected file.'));
$this->assertEqual($this->drupalGetHeader('Content-Type'), $this->image_info['mime_type'], t('Expected Content-Type was reported.'));
$this->assertEqual($this->drupalGetHeader('Content-Length'), $this->image_info['file_size'], t('Expected Content-Length was reported.'));
} }
} }