Improvements based on import test.
[rock.divinelegy.git] / Controllers / FileController.php
1 <?php
2
3 namespace Controllers;
4
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;
14
15 class FileController implements IDivineController
16 {
17 private $_fileRepository;
18 private $_response;
19 private $_request;
20 private $_downloadRepository;
21 private $_downloadFactory;
22 private $_userSession;
23 private $_userQuota;
24 private $_statusReporter;
25
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
35 ) {
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;
44 }
45
46 public function indexAction() {
47 ;
48 }
49
50 // list simfiles
51 public function serveBannerAction($hash)
52 {
53 $file = $this->_fileRepository->findByHash($hash);
54 if($hash == 'default') $this->serveDefaultBanner();
55 if(!$file) $this->notFound();
56
57 $match = reset(glob('../files/' . $file->getPath() . '/' . $file->getHash() . '.*'));
58
59 $this->_response->setHeader('Content-Type', $file->getMimetype())
60 ->setHeader('Content-Length', $file->getSize())
61 ->setBody(file_get_contents($match))
62 ->sendResponse();
63 }
64
65 private function serveDefaultBanner()
66 {
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))
71 ->sendResponse();
72 exit();
73 }
74
75 public function serveSimfileOrPackAction($hash)
76 {
77 $file = $this->_fileRepository->findByHash($hash);
78 $quotaRemaining = $this->_userQuota->getCurrentUserQuotaRemaining();
79
80 if(!$file) $this->notFound();
81 if(!$quotaRemaining) $this->notAuthorised();
82 if($quotaRemaining < $file->getSize()) $this->notEnoughQuota();
83
84 // TODO: Builder?
85 $download = $this->_downloadFactory->createInstance($this->_userSession->getCurrentUser(),
86 $file,
87 time(),
88 $this->_request->getIp());
89
90 $this->_downloadRepository->save($download);
91
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))
99 ->sendResponse();
100 }
101
102 private function notFound()
103 {
104 $this->_response->setHeader('HTTP/1.0 404 Not Found', 'Nothing to see here')
105 ->setBody('Move along.')
106 ->sendResponse();
107 exit();
108 }
109
110 private function notAuthorised()
111 {
112 $this->_response->setHeader('Content-Type', 'application/json')
113 ->setBody($this->_statusReporter->error('You must be authenticated to download files')->json())
114 ->sendResponse();
115 exit();
116 }
117
118 private function notEnoughQuota()
119 {
120 $this->_response->setHeader('Content-Type', 'application/json')
121 ->setBody($this->_statusReporter->error('You don\'t have enough quota remaining for this file.')->json())
122 ->sendResponse();
123 exit();
124 }
125 }