images: Use the database's stored filesize and timestamps to eliminate a stat()

Also, improve handling of if-modified-since header.
This commit is contained in:
Solomon Peachy 2015-02-21 10:26:56 -05:00
parent 396ea6c6b9
commit 06338cb102
2 changed files with 24 additions and 33 deletions

View File

@ -28,6 +28,8 @@ v2.38 (Unreleased)
[add] Added support for JPEG-XR, WebP, and MJPEG/MJPEG-2000 formats.
[add] Beginnings of a JSON-RPC implementation
[add] Remove some of the roadblocks from serving non-JPEG images.
[misc] Fix handling of if-modified-since header when serving images
[misc] Use the database's stored file size and timestamps
v2.37.1 (December 3, 2012)

View File

@ -46,10 +46,6 @@ if ($image_size && !is_numeric($image_size)) {
exit();
}
/* Figure out SQL based on image */
$photo_sel = " photo.identifier = '$photo_id' ";
$photo_sel .= $version ? " and photo_version.identifier='$version' " : " and photo_version.master='t' ";
/* Translate image sizes */
switch ($image_size) {
case FALSE:
@ -62,18 +58,25 @@ switch ($image_size) {
break;
}
/* Set up page */
$compress_pages = FALSE;
ini_set('zlib.output_compression', 'Off');
$database = site_prolog();
$cache_ctrl = FALSE;
/* Figure out SQL based on image */
$photo_sel = " photo.identifier = '$photo_id' ";
$photo_sel .= $version ? " and photo_version.identifier='$version' " : " and photo_version.master='t' ";
$photo_sel .= " and size = '$image_size' ";
$photo_data = pg_fetch_assoc(pg_query($database, "
select users, access_rights, hide_original, original_image_name,
get_image_path(photo_version.identifier, $image_size) as path,
created, filesize, path,
can_access_photo(photo.identifier, $po_user[id], '{".$passwords."}') as ok
from photo left join photo_version on photo.identifier = photo_version.photo
from photo
right join photo_version on photo.identifier = photo_version.photo
right join files on photo_version.identifier = files.version
where $photo_sel"));
if (!$photo_data) {
header("HTTP/1.1 404 Not found");
@ -143,40 +146,26 @@ if ($increment_counter &&
site_epilog($database);
if (!is_readable($file_name)) {
header("HTTP/1.1 500 Internal Server Error");
exit(0);
}
/* If we're given an If-Modified-Since header, use it */
$stat = stat($file_name);
if ($stat === FALSE) {
header("HTTP/1.1 404 Not found");
exit();
}
$time_of_last_modification = $stat[9];
$file_length = $stat[7];
$if_modified_since = FALSE;
/* Figure out timestamps */
$time_of_last_modification = strtotime($photo_data['created']);
$file_length = $photo_data['filesize'];
$rfc1123 = gmdate("r", $time_of_last_modification) .' GMT';
$rfc1036 = gmdate('l, d-M-y H:i:s ', $time_of_last_modification) . ' GMT';
$ctime = gmdate('D M j H:i:s', $time_of_last_modification);
/* If we're given an If-Modified-Since header, use it */
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
$if_modified_since = stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']);
}
if ($if_modified_since !== FALSE) {
foreach (array($rfc1123, $rfc1036, $ctime) as $d) {
if ($d == $if_modified_since) {
header("HTTP/1.1 304 Not modified");
exit(0);
}
if ($if_modified_since !== FALSE) {
$if_modified_since = strtotime($if_modified_since);
foreach (array($rfc1123, $rfc1036, $ctime) as $d) {
if ($d == $if_modified_since) {
header("HTTP/1.1 304 Not modified");
exit(0);
}
}
}
}
}
switch ($photo_data['access_rights']) {
case $access['public']: