6 use Controllers\IDivineController
;
7 use Services\Http\IHttpResponse
;
8 use Services\Http\IHttpRequest
;
9 use Services\Uploads\IUploadManager
;
10 use Services\IUserSession
;
11 use Services\IZipParser
;
12 use Services\ISMOMatcher
;
13 use Services\IStatusReporter
;
14 use DataAccess\StepMania\ISimfileRepository
;
15 use DataAccess\StepMania\IPackRepository
;
16 use DataAccess\IFileRepository
;
17 use DataAccess\IDownloadRepository
;
18 use Domain\Entities\StepMania\ISimfile
;
19 use Domain\Entities\IFile
;
20 use Domain\Entities\StepMania\IPack
;
23 class SimfileController
implements IDivineController
25 private $_simfileRepository;
26 private $_packRepository;
27 private $_fileRepository;
30 private $_uploadManager;
33 private $_downloadRepository;
34 private $_statusReporter;
35 private $_userSession;
37 public function __construct(
38 IHttpResponse
$response,
39 IHttpRequest
$request,
40 IUploadManager
$uploadManager,
41 ISimfileRepository
$simfileRepository,
42 IPackRepository
$packRepository,
43 IFileRepository
$fileRepository,
44 IUserSession
$userSession,
45 IZipParser
$zipParser,
46 ISMOMatcher
$smoMatcher,
47 IDownloadRepository
$downloadRepository,
48 IStatusReporter
$statusReporter
50 $this->_response
= $response;
51 $this->_request
= $request;
52 $this->_uploadManager
= $uploadManager;
53 $this->_simfileRepository
= $simfileRepository;
54 $this->_packRepository
= $packRepository;
55 $this->_fileRepository
= $fileRepository;
56 $this->_zipParser
= $zipParser;
57 $this->_smoMatcher
= $smoMatcher;
58 $this->_downloadRepository
= $downloadRepository;
59 $this->_statusReporter
= $statusReporter;
60 $this->_userSession
= $userSession;
63 public function indexAction() {
68 public function listAction()
70 $file = '../SimfileCache/simfiles.json';
71 $path = realpath($file);
73 //Always set incase list changes
74 $hash = md5_file($path);
75 $this->_response
->setHeader('etag', $hash)
76 ->setHeader('last-modified', gmdate("D, d M Y H:i:s", filemtime($file)) . " GMT")
77 ->setHeader('cache-control', 'max-age=-1');
79 if($this->_request
->getHeader('HTTP_IF_NONE_MATCH') == $hash)
81 $this->_response
->setHeader("HTTP/1.1 304 Not Modified", 'Nice meme!');
83 $this->_response
->setHeader('Content-Type', 'application/json')
84 ->setBody(file_get_contents($path));
87 $this->_response
->sendResponse();
90 public function latestSimfileAction()
92 $simfile = $this->_simfileRepository
->findRange(0, -1);
93 $this->_response
->setHeader('Content-Type', 'application/json')
94 ->setBody(json_encode(Util
::simfileToArray(reset($simfile))))
98 public function latestPackAction()
100 $pack = $this->_packRepository
->findRange(0, -1);
101 $this->_response
->setHeader('Content-Type', 'application/json')
102 ->setBody(json_encode(Util
::packToArray(reset($pack))))
106 public function popularAction()
108 $returnArray = array();
109 $popularDownloads = $this->_downloadRepository
->findPopular();
110 $packOrFileId = reset($popularDownloads)->getFile()->getId();
112 $simfile = $this->_simfileRepository
->findByFileId($packOrFileId);
115 $returnArray = Util
::simfileToArray(reset($simfile));
117 $pack = $this->_packRepository
->findByFileId($packOrFileId);
118 $returnArray = Util
::packToArray(reset($pack));
121 $this->_response
->setHeader('Content-Type', 'application/json')
122 ->setBody(json_encode($returnArray))
126 public function uploadAction()
128 if(!$this->_userSession
->getCurrentUser()) $this->_statusReporter
->error('You must be authenticated to upload files');
130 //TODO: Put directory in config ?
131 $files = $this->_uploadManager
->setFilesDirectory('../files')
132 ->setDestination('StepMania/')
135 //TODO: This should be in a try-catch and if it fails the file should
136 //be deleted from the filesystem and database.
137 foreach($files as $file)
139 $zipParser = $this->_zipParser
;
140 $zipParser->parse($file);
142 if(!$zipParser->simfiles()) $this->_statusReporter
->error('That zip doesn\'t seem to have any simfiles in it.');
144 //save the actual zip in the db
145 $this->findAndAddSmoMirror($file);
146 $this->_fileRepository
->save($file);
148 if($zipParser->isPack())
150 //XXX: Tricky! pack() uses packbuilder and so returns a new pack each time.
151 //I tried to be clever and call pack() multiple times thinking I was getting the same
152 //object. Should I cache it in zipparser?
153 $pack = $zipParser->pack();
154 $packBanner = $pack->getBanner() ?
$this->_fileRepository
->save($pack->getBanner()) : null
;
155 $this->_packRepository
->save($pack);
158 foreach($zipParser->simfiles() as $simfile)
160 $banner = $simfile->getBanner() ?
$this->_fileRepository
->save($simfile->getBanner()) : null
;
161 $simfileZip = $simfile->getSimfile() ?
$this->_fileRepository
->save($simfile->getSimfile()) : null
;
163 if(isset($pack)) $simfile->addToPack($pack);
164 $this->_simfileRepository
->save($simfile);
168 $this->_statusReporter
->success('Uploaded succesfully');
171 private function findAndAddSmoMirror(IFile
$file)
173 $basename = pathinfo($file->getFilename(), PATHINFO_FILENAME
);
174 $match = $this->_smoMatcher
->match($basename, $file->getSize());
176 //XXX: Direct instantiation of FileMirror bad?
177 if($match && $match['confidence'] > 90)
179 $file->addMirror(new \Domain\VOs\
FileMirror($match['href'], 'Stepmania Online'));