Fucking Windows bullshit.
[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 $matches = glob(realpath('../files/' . $file->getPath()) . '/' . $file->getHash() . '.*');
58 $match = reset($matches);
59
60 $this->_response->setHeader('Content-Type', $file->getMimetype())
61 ->setHeader('Content-Length', $file->getSize())
62 ->setBody(file_get_contents($match))
63 ->sendResponse();
64 }
65
66 private function serveDefaultBanner()
67 {
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))
72 ->sendResponse();
73 exit();
74 }
75
76 public function serveSimfileOrPackAction($hash)
77 {
78 $file = $this->_fileRepository->findByHash($hash);
79 $quotaRemaining = $this->_userQuota->getCurrentUserQuotaRemaining();
80
81 if(!$file) $this->notFound();
82 if(!$quotaRemaining) $this->notAuthorised();
83 if($quotaRemaining < $file->getSize()) $this->notEnoughQuota();
84
85 // TODO: Builder?
86 $download = $this->_downloadFactory->createInstance($this->_userSession->getCurrentUser(),
87 $file,
88 time(),
89 $this->_request->getIp());
90
91 $this->_downloadRepository->save($download);
92
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))
100 ->sendResponse();
101 }
102
103 private function notFound()
104 {
105 $this->_response->setHeader('HTTP/1.0 404 Not Found', 'Nothing to see here')
106 ->setBody('Move along.')
107 ->sendResponse();
108 exit();
109 }
110
111 private function notAuthorised()
112 {
113 $this->_response->setHeader('Content-Type', 'application/json')
114 ->setBody($this->_statusReporter->error('You must be authenticated to download files')->json())
115 ->sendResponse();
116 exit();
117 }
118
119 private function notEnoughQuota()
120 {
121 $this->_response->setHeader('Content-Type', 'application/json')
122 ->setBody($this->_statusReporter->error('You don\'t have enough quota remaining for this file.')->json())
123 ->sendResponse();
124 exit();
125 }
126 }