richdocuments/lib/download.php

60 lines
1.1 KiB
PHP
Raw Normal View History

2013-07-19 18:52:33 +03:00
<?php
namespace OCA\Office;
class Download {
2013-08-07 21:14:36 +03:00
protected $view;
2013-07-19 18:52:33 +03:00
// File to be served
protected $filepath;
protected $instance;
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-07-19 18:52:33 +03: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
}
}
public function sendResponse(){
\OCP\Response::disableCaching();
2013-08-07 21:41:11 +03:00
2013-07-19 18:52:33 +03:00
if (!$this->fileExists()){
$this->sendNotFound();
}
$this->instance->sendResponse();
exit();
}
protected function getFilename(){
return basename($this->filepath);
}
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
}
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
}
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
}
protected function sendNotFound(){
header("HTTP/1.0 404 Not Found");
$tmpl = new OCP\Template('', '404', 'guest');
$tmpl->assign('file', $this->filepath);
$tmpl->printPage();
exit;
}
}