stream large photos through buffered I/O if possible
This commit is contained in:
		| @@ -2,6 +2,7 @@ | |||||||
| namespace Zotlabs\Module; | namespace Zotlabs\Module; | ||||||
|  |  | ||||||
| require_once('include/security.php'); | require_once('include/security.php'); | ||||||
|  | require_once('include/attach.php'); | ||||||
| require_once('include/photo/photo_driver.php'); | require_once('include/photo/photo_driver.php'); | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -10,6 +11,8 @@ class Photo extends \Zotlabs\Web\Controller { | |||||||
| 	function init() { | 	function init() { | ||||||
| 	 | 	 | ||||||
| 		$prvcachecontrol = false; | 		$prvcachecontrol = false; | ||||||
|  | 		$streaming = null; | ||||||
|  | 		$channel = null; | ||||||
| 	 | 	 | ||||||
| 		switch(argc()) { | 		switch(argc()) { | ||||||
| 			case 4: | 			case 4: | ||||||
| @@ -131,6 +134,8 @@ class Photo extends \Zotlabs\Web\Controller { | |||||||
| 	 | 	 | ||||||
| 				$sql_extra = permissions_sql($r[0]['uid']); | 				$sql_extra = permissions_sql($r[0]['uid']); | ||||||
| 	 | 	 | ||||||
|  | 				$channel = channelx_by_n($r[0]['uid']); | ||||||
|  |  | ||||||
| 				// Now we'll see if we can access the photo | 				// Now we'll see if we can access the photo | ||||||
| 	 | 	 | ||||||
| 				$r = q("SELECT * FROM photo WHERE resource_id = '%s' AND imgscale = %d $sql_extra LIMIT 1", | 				$r = q("SELECT * FROM photo WHERE resource_id = '%s' AND imgscale = %d $sql_extra LIMIT 1", | ||||||
| @@ -141,8 +146,9 @@ class Photo extends \Zotlabs\Web\Controller { | |||||||
| 				if($r && $allowed) { | 				if($r && $allowed) { | ||||||
| 					$data = dbunescbin($r[0]['content']); | 					$data = dbunescbin($r[0]['content']); | ||||||
| 					$mimetype = $r[0]['mimetype']; | 					$mimetype = $r[0]['mimetype']; | ||||||
| 					if(intval($r[0]['os_storage'])) | 					if(intval($r[0]['os_storage'])) { | ||||||
| 						$data = file_get_contents($data); | 						$streaming = $data; | ||||||
|  | 					} | ||||||
| 				} | 				} | ||||||
| 				else { | 				else { | ||||||
| 	 | 	 | ||||||
| @@ -242,7 +248,25 @@ class Photo extends \Zotlabs\Web\Controller { | |||||||
| 			header("Cache-Control: max-age=" . $cache); | 			header("Cache-Control: max-age=" . $cache); | ||||||
| 	 | 	 | ||||||
| 		} | 		} | ||||||
| 		echo $data; |  | ||||||
|  | 		// If it's a file resource, stream it.  | ||||||
|  |  | ||||||
|  | 		if($streaming && $channel) { | ||||||
|  | 			if(strpos($streaming,'store') !== false) | ||||||
|  | 				$istream = fopen($streaming,'rb'); | ||||||
|  | 			else | ||||||
|  | 				$istream = fopen('store/' . $channel['channel_address'] . '/' . $streaming,'rb'); | ||||||
|  | 			$ostream = fopen('php://output','wb'); | ||||||
|  | 			if($istream && $ostream) { | ||||||
|  | 				pipe_streams($istream,$ostream); | ||||||
|  | 				fclose($istream); | ||||||
|  | 				fclose($ostream); | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		else { | ||||||
|  | 			echo $data; | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		killme(); | 		killme(); | ||||||
| 		// NOTREACHED | 		// NOTREACHED | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -1468,7 +1468,7 @@ function find_filename_by_hash($channel_id, $attachHash) { | |||||||
| function pipe_streams($in, $out) { | function pipe_streams($in, $out) { | ||||||
| 	$size = 0; | 	$size = 0; | ||||||
| 	while (!feof($in)) | 	while (!feof($in)) | ||||||
| 		$size += fwrite($out, fread($in, 8192)); | 		$size += fwrite($out, fread($in, 16384)); | ||||||
|  |  | ||||||
| 	return $size; | 	return $size; | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user