Index: modules/core/classes/Gallery.class
===================================================================
--- modules/core/classes/Gallery.class	(Revision 18435)
+++ modules/core/classes/Gallery.class	(Arbeitskopie)
@@ -860,7 +860,21 @@
 
 	$base = $this->getConfig('data.gallery.base');
 	$path = $base . $relativePath;
-	if (file_exists($path) && $fd = fopen($path, 'rb')) {
+
+	if ($this->getConfig('useSendFile') && file_exists($path)) {
+		header('Content-Disposition: inline; filename="' . $filename . '"');
+		if (strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') === 0) {
+			/* lighttpd *should* support X-Sendfile, but 1.4.13 does not */
+			header('X-LIGHTTPD-send-file: ' . $path);
+			header('Last-Modified: ' . $lastModified);
+		} else {
+			header('X-Sendfile: ' . $path);
+			/* Apache does add a Last-Modified header itself */
+		}
+		header('Content-type: ' . $mimeType);
+		/* Content-Length is added by Apache and lighttpd itself */
+		return true;
+	} elseif (file_exists($path) && $fd = fopen($path, 'rb')) {
 	    header('Content-Disposition: inline; filename="' . $filename . '"');
 	    header('Last-Modified: ' . $lastModified);
 	    header('Content-type: ' . $mimeType);
Index: modules/core/DownloadItem.inc
===================================================================
--- modules/core/DownloadItem.inc	(Revision 18435)
+++ modules/core/DownloadItem.inc	(Arbeitskopie)
@@ -164,6 +164,17 @@
 	    if ($stats[7] > 0) {
 		$phpVm->header('Content-length: ' . $stats[7]);
 	    }
+
+	    if ($gallery->getConfig('useSendFile')) {
+	    	if (strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') === 0) {
+		    /* lighttpd *should* support X-Sendfile, but 1.4.13 does not */
+		    $phpVm->header('X-LIGHTTPD-send-file: ' . $data['derivativePath']);
+		} else {
+		    $phpVm->header('X-Sendfile: ' . $data['derivativePath']);
+		}
+	        return null;
+	    }
+
 	    /*
 	     * Don't use readfile() because it buffers the entire file in memory.  Profiling shows
 	     * that this approach is as efficient as fpassthru() but we get to call

