| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2013-08-08 00:22:21 +03:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2013-08-28 12:02:27 +02:00
										 |  |  |  * ownCloud - Documents App | 
					
						
							| 
									
										
										
										
											2013-08-08 00:22:21 +03:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @author Victor Dubiniuk | 
					
						
							|  |  |  |  * @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is licensed under the Affero General Public License version 3 or | 
					
						
							|  |  |  |  * later. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-07 20:05:35 +03:00
										 |  |  | namespace OCA\Documents; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Download_Range extends \OCA\Documents\Download { | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Start of the range
 | 
					
						
							|  |  |  | 	protected $start; | 
					
						
							|  |  |  | 	// End of the range
 | 
					
						
							|  |  |  | 	protected $end; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-16 18:54:31 +03:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Build download model to serve HTTP_RANGE | 
					
						
							|  |  |  | 	 * @param type $view - filesystem view | 
					
						
							|  |  |  | 	 * @param type $filepath - path to the file relative to this view root | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-08-07 21:41:11 +03:00
										 |  |  | 	public function __construct($view, $filepath){ | 
					
						
							|  |  |  | 		$this->view = $view; | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 		$this->filepath = $filepath; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-16 18:54:31 +03:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Send the requested parts of the file | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 	public function sendResponse(){ | 
					
						
							|  |  |  | 		if (!preg_match('/^bytes=\d*-\d*(,\d*-\d*)*$/', $_SERVER['HTTP_RANGE'])){ | 
					
						
							|  |  |  | 			$this->sendNotSatisfiable(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		$ranges = explode(',', substr($_SERVER['HTTP_RANGE'], 6)); | 
					
						
							|  |  |  | 		foreach ($ranges as $range){ | 
					
						
							|  |  |  | 			$parts = explode('-', $range); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-19 19:53:39 +03:00
										 |  |  | 			$start = isset($parts[0]) ? $parts[0] : 0; | 
					
						
							|  |  |  | 			$end = isset($parts[1]) ? $parts[1] : $this->getFilesize() - 1; | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if ($start > $end){ | 
					
						
							|  |  |  | 				$this->sendNotSatisfiable(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-07 21:14:36 +03:00
										 |  |  | 			$handle = $this->view->fopen($this->filepath, 'rb'); | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 			\fseek($handle, $start); | 
					
						
							|  |  |  | 			$buffer = \fread($handle, $end - $start); | 
					
						
							|  |  |  | 			$md5Sum = md5($buffer); | 
					
						
							|  |  |  | 			\fclose($handle); | 
					
						
							|  |  |  | 			// send the headers and data 
 | 
					
						
							|  |  |  | 			header("Content-Length: " . $end - $start); | 
					
						
							|  |  |  | 			header("Content-md5: " . $md5Sum); | 
					
						
							|  |  |  | 			header("Accept-Ranges: bytes"); | 
					
						
							|  |  |  | 			header('Content-Range: bytes ' . $start . '-' . ($end) . '/' . $this->getFilesize()); | 
					
						
							|  |  |  | 			header("Connection: close"); | 
					
						
							|  |  |  | 			header("Content-type: " . $this->getMimeType()); | 
					
						
							|  |  |  | 			header('Content-Disposition: attachment; filename=' . $this->getFilename()); | 
					
						
							| 
									
										
										
										
											2013-07-19 19:53:39 +03:00
										 |  |  | 			\OC_Util::obEnd(); | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 			echo $buffer; | 
					
						
							|  |  |  | 			flush(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-16 18:54:31 +03:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Send 416 if we can't satisfy the requested ranges | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 	protected function sendNotSatisfiable(){ | 
					
						
							|  |  |  | 		header('HTTP/1.1 416 Requested Range Not Satisfiable'); | 
					
						
							|  |  |  | 		header('Content-Range: bytes */' . $this->getFilesize()); // Required in 416.
 | 
					
						
							|  |  |  | 		exit; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |