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 $match = reset(glob('../files/' . $file->getPath() . '/' . $file->getHash() . '.*'));
59 $this->_response
->setHeader('Content-Type', $file->getMimetype())
60 ->setHeader('Content-Length', $file->getSize())
61 ->setBody(file_get_contents($match))
65 private function serveDefaultBanner()
67 $file = '../files/banners/default.png';
68 $this->_response
->setHeader('Content-Type', 'image/png')
69 ->setHeader('Content-Length', filesize($file))
70 ->setBody(file_get_contents($file))
75 public function serveSimfileOrPackAction($hash)
77 $file = $this->_fileRepository
->findByHash($hash);
78 $quotaRemaining = $this->_userQuota
->getCurrentUserQuotaRemaining();
80 if(!$file) $this->notFound();
81 if(!$quotaRemaining) $this->notAuthorised();
82 if($quotaRemaining < $file->getSize()) $this->notEnoughQuota();
85 $download = $this->_downloadFactory
->createInstance($this->_userSession
->getCurrentUser(),
88 $this->_request
->getIp());
90 $this->_downloadRepository
->save($download);
92 $zip = '../files/' . $file->getPath() . '/' . $file->getHash() . '.zip';
93 //TODO: This may not work on all browser or something. We'll have to see. Also it may hog ram so...
94 $this->_response
->setHeader('Content-Type', $file->getMimetype())
95 ->setHeader('Content-Length', $file->getSize())
96 ->setHeader('Content-Disposition', 'filename="' . $file->getFileName() . '";')
97 ->setHeader('Content-Transfer-Encoding', 'binary')
98 ->setBody(file_get_contents($zip))
102 private function notFound()
104 $this->_response
->setHeader('HTTP/1.0 404 Not Found', 'Nothing to see here')
105 ->setBody('Move along.')
110 private function notAuthorised()
112 $this->_response
->setHeader('Content-Type', 'application/json')
113 ->setBody($this->_statusReporter
->error('You must be authenticated to download files')->json())
118 private function notEnoughQuota()
120 $this->_response
->setHeader('Content-Type', 'application/json')
121 ->setBody($this->_statusReporter
->error('You don\'t have enough quota remaining for this file.')->json())