| 
									
										
										
										
											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-08-28 12:02:27 +02:00
										 |  |  | namespace OCA\Documents; | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-26 22:55:17 +03:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Generic download class | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | class Download { | 
					
						
							| 
									
										
										
										
											2013-08-16 18:54:31 +03:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Filesystem view | 
					
						
							|  |  |  | 	 * @var \OC\Files\View | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-08-07 21:14:36 +03:00
										 |  |  | 	protected $view; | 
					
						
							| 
									
										
										
										
											2013-08-16 18:54:31 +03:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Path to the File to be served, relative to the view | 
					
						
							|  |  |  | 	 * @var string | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 	protected $filepath; | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-09-26 22:55:17 +03:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Subclassed object | 
					
						
							|  |  |  | 	 * @var  | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 	protected $instance; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-16 18:54:31 +03:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Build download model according to the headers | 
					
						
							|  |  |  | 	 * @param type $view - filesystem view | 
					
						
							|  |  |  | 	 * @param type $filepath - path to the file relative to this view root | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-11-09 17:29:12 +03:00
										 |  |  | 	public function __construct($owner, $filepath){ | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 		$this->filepath = $filepath; | 
					
						
							| 
									
										
										
										
											2013-08-07 21:14:36 +03:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-08-28 15:24:41 +02:00
										 |  |  | 		if (isset($_SERVER['HTTP_RANGE'])) { | 
					
						
							| 
									
										
										
										
											2013-11-09 17:29:12 +03:00
										 |  |  | 			$this->instance = new Download_Range($owner, $filepath); | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2013-11-09 17:29:12 +03:00
										 |  |  | 			$this->instance = new Download_Simple($owner, $filepath); | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-11-09 17:29:12 +03:00
										 |  |  | 		 | 
					
						
							|  |  |  | 		$this->view = $this->getView($owner); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	protected function getView($owner){ | 
					
						
							|  |  |  | 		return new View('/' . $owner); | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-08-16 18:54:31 +03:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Send the requested content | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 	public function sendResponse(){ | 
					
						
							|  |  |  | 		\OCP\Response::disableCaching(); | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		if (!$this->fileExists()){ | 
					
						
							|  |  |  | 			$this->sendNotFound(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		$this->instance->sendResponse(); | 
					
						
							|  |  |  | 		exit(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-16 18:54:31 +03:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Get the name of the requested file | 
					
						
							|  |  |  | 	 * @return String  | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 	protected function getFilename(){ | 
					
						
							|  |  |  | 		return basename($this->filepath); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-08-16 18:54:31 +03:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Get the size of the requested file | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 	protected function getFilesize(){ | 
					
						
							| 
									
										
										
										
											2013-08-07 21:14:36 +03:00
										 |  |  | 		return $this->view->filesize($this->filepath); | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-08-16 18:54:31 +03:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Get the mimetype of the requested file  | 
					
						
							|  |  |  | 	 * @return string | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 	protected function getMimeType(){ | 
					
						
							| 
									
										
										
										
											2013-08-07 21:14:36 +03:00
										 |  |  | 		return $this->view->getMimeType($this->filepath); | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-08-16 18:54:31 +03:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Check if the requested file exists | 
					
						
							|  |  |  | 	 * @return bool | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 	protected function fileExists(){ | 
					
						
							| 
									
										
										
										
											2013-08-07 21:14:36 +03:00
										 |  |  | 		return $this->view->file_exists($this->filepath); | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-16 18:54:31 +03:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Send 404 Response | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 	protected function sendNotFound(){ | 
					
						
							|  |  |  | 		header("HTTP/1.0 404 Not Found"); | 
					
						
							| 
									
										
										
										
											2013-10-02 19:54:48 +03:00
										 |  |  | 		$tmpl = new \OCP\Template('', '404', 'guest'); | 
					
						
							| 
									
										
										
										
											2013-07-19 18:52:33 +03:00
										 |  |  | 		$tmpl->assign('file', $this->filepath); | 
					
						
							|  |  |  | 		$tmpl->printPage(); | 
					
						
							|  |  |  | 		exit; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |