Blame view

sources/apps/documents/lib/download.php 2.11 KB
923852aa1   Kload   Official Owncloud...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  <?php
  /**
   * ownCloud - Documents App
   *
   * @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.
   */
  
  namespace OCA\Documents;
  
  /**
   * Generic download class
   */
  class Download {
  	
  	/**
  	 * Filesystem view
  	 * @var \OC\Files\View
  	 */
  	protected $view;
  	
  	/**
  	 * Path to the File to be served, relative to the view
  	 * @var string
  	 */
  	protected $filepath;
  	
  	/**
  	 * Subclassed object
  	 * @var 
  	 */
  	protected $instance;
  
  	/**
  	 * Build download model according to the headers
  	 * @param type $view - filesystem view
  	 * @param type $filepath - path to the file relative to this view root
  	 */
  	public function __construct($owner, $filepath){
  		$this->filepath = $filepath;
  		
  		if (isset($_SERVER['HTTP_RANGE'])) {
  			$this->instance = new Download_Range($owner, $filepath);
  		} else {
  			$this->instance = new Download_Simple($owner, $filepath);
  		}
  		
  		$this->view = $this->getView($owner);
  	}
  	
  	protected function getView($owner){
  		return new View('/' . $owner);
  	}
  	
  	/**
  	 * Send the requested content
  	 */
  	public function sendResponse(){
  		\OCP\Response::disableCaching();
  		
  		if (!$this->fileExists()){
  			$this->sendNotFound();
  		}
  		
  		$this->instance->sendResponse();
  		exit();
  	}
  
  	/**
  	 * Get the name of the requested file
  	 * @return String 
  	 */
  	protected function getFilename(){
  		return basename($this->filepath);
  	}
  	
  	/**
  	 * Get the size of the requested file
  	 */
  	protected function getFilesize(){
  		return $this->view->filesize($this->filepath);
  	}
  	
  	/**
  	 * Get the mimetype of the requested file 
  	 * @return string
  	 */
  	protected function getMimeType(){
  		return $this->view->getMimeType($this->filepath);
  	}
  	
  	/**
  	 * Check if the requested file exists
  	 * @return bool
  	 */
  	protected function fileExists(){
  		return $this->view->file_exists($this->filepath);
  	}
  
  	/**
  	 * Send 404 Response
  	 */
  	protected function sendNotFound(){
  		header("HTTP/1.0 404 Not Found");
  		$tmpl = new \OCP\Template('', '404', 'guest');
  		$tmpl->assign('file', $this->filepath);
  		$tmpl->printPage();
  		exit;
  	}
  }