richdocuments/lib/download.php

103 lines
1.9 KiB
PHP
Raw Normal View History

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
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;
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-08-07 21:41:11 +03:00
public function __construct($view, $filepath){
2013-07-19 18:52:33 +03:00
$this->filepath = $filepath;
2013-08-07 21:41:11 +03:00
$this->view = $view;
2013-08-07 21:14:36 +03:00
2013-08-28 15:24:41 +02:00
if (isset($_SERVER['HTTP_RANGE'])) {
2013-08-07 21:41:11 +03:00
$this->instance = new Download\Range($view, $filepath);
2013-07-19 18:52:33 +03:00
} else {
2013-08-07 21:41:11 +03:00
$this->instance = new Download\Simple($view, $filepath);
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");
$tmpl = new OCP\Template('', '404', 'guest');
$tmpl->assign('file', $this->filepath);
$tmpl->printPage();
exit;
}
}