5 use Controllers\IDivineController
;
6 use Services\Http\IHttpRequest
;
7 use Services\Http\IHttpResponse
;
8 use Services\IUserSession
;
9 use Services\IStatusReporter
;
10 use Services\IUserQuota
;
11 use Domain\Entities\IDownloadFactory
;
12 use DataAccess\IFileRepository
;
13 use DataAccess\IDownloadRepository
;
15 class FileController
implements IDivineController
17 private $_fileRepository;
20 private $_downloadRepository;
21 private $_downloadFactory;
22 private $_userSession;
24 private $_statusReporter;
26 public function __construct(
27 IHttpRequest
$request,
28 IHttpResponse
$response,
29 IFileRepository
$repository,
30 IDownloadFactory
$downloadFactory,
31 IDownloadRepository
$downloadRepository,
32 IUserSession
$userSession,
33 IUserQuota
$userQuota,
34 IStatusReporter
$statusReporter
36 $this->_request
= $request;
37 $this->_response
= $response;
38 $this->_fileRepository
= $repository;
39 $this->_downloadRepository
= $downloadRepository;
40 $this->_downloadFactory
= $downloadFactory;
41 $this->_userSession
= $userSession;
42 $this->_userQuota
= $userQuota;
43 $this->_statusReporter
= $statusReporter;
46 public function indexAction() {
51 public function serveBannerAction($hash)
53 $file = $this->_fileRepository
->findByHash($hash);
54 if($hash == 'default') $this->serveDefaultBanner();
55 if(!$file) $this->notFound();
57 $matches = glob(realpath('../files/' . $file->getPath()) . '/' . $file->getHash() . '.*');
58 $match = reset($matches);
60 $this->_response
->setHeader('Content-Type', $file->getMimetype())
61 ->setHeader('Content-Length', $file->getSize())
62 ->setBody(file_get_contents($match))
66 private function serveDefaultBanner()
68 $file = '../files/banners/default.png';
69 $this->_response
->setHeader('Content-Type', 'image/png')
70 ->setHeader('Content-Length', filesize($file))
71 ->setBody(file_get_contents($file))
76 public function serveSimfileOrPackAction($hash)
78 $file = $this->_fileRepository
->findByHash($hash);
79 $quotaRemaining = $this->_userQuota
->getCurrentUserQuotaRemaining();
81 if(!$file) $this->notFound();
82 if(!$quotaRemaining) $this->notAuthorised();
83 if($quotaRemaining < $file->getSize()) $this->notEnoughQuota();
86 $download = $this->_downloadFactory
->createInstance($this->_userSession
->getCurrentUser(),
89 $this->_request
->getIp());
91 $this->_downloadRepository
->save($download);
93 $zip = '../files/' . $file->getPath() . '/' . $file->getHash() . '.zip';
94 //TODO: This may not work on all browser or something. We'll have to see. Also it may hog ram so...
95 $this->_response
->setHeader('Content-Type', $file->getMimetype())
96 ->setHeader('Content-Length', $file->getSize())
97 ->setHeader('Content-Disposition', 'filename="' . $file->getFileName() . '";')
98 ->setHeader('Content-Transfer-Encoding', 'binary')
99 ->setBody(file_get_contents($zip))
103 private function notFound()
105 $this->_response
->setHeader('HTTP/1.0 404 Not Found', 'Nothing to see here')
106 ->setBody('Move along.')
111 private function notAuthorised()
113 $this->_response
->setHeader('Content-Type', 'application/json')
114 ->setBody($this->_statusReporter
->error('You must be authenticated to download files')->json())
119 private function notEnoughQuota()
121 $this->_response
->setHeader('Content-Type', 'application/json')
122 ->setBody($this->_statusReporter
->error('You don\'t have enough quota remaining for this file.')->json())