From 01e44e9c1739d454bbf7d98e1dcba3e2a13679b2 Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Thu, 11 Dec 2014 15:28:39 +0800 Subject: [PATCH] Improvements based on import test. --- .gitignore | 1 + Controllers/FileController.php | 14 ++++++---- Controllers/SimfileController.php | 19 +++++++++++-- Services/IStatusReporter.php | 13 +++++++++ Services/StatusReporter.php | 57 ++++++++++++++++++++++++++++++++++++++ Services/Uploads/UploadManager.php | 12 ++++---- Services/UserSession.php | 14 ++++++---- Services/ZipParser.php | 3 +- config/DI.php | 1 + config/app.php.example | 6 ++++ public_html/index.php | 13 +++++---- 11 files changed, 127 insertions(+), 26 deletions(-) create mode 100644 Services/IStatusReporter.php create mode 100644 Services/StatusReporter.php create mode 100644 config/app.php.example diff --git a/.gitignore b/.gitignore index 008e5e6..575a306 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ vendor db.php files FacebookApp.php +app.php diff --git a/Controllers/FileController.php b/Controllers/FileController.php index 6fe626e..c50a623 100644 --- a/Controllers/FileController.php +++ b/Controllers/FileController.php @@ -6,6 +6,7 @@ use Controllers\IDivineController; use Services\Http\IHttpRequest; use Services\Http\IHttpResponse; use Services\IUserSession; +use Services\IStatusReporter; use Services\IUserQuota; use Domain\Entities\IDownloadFactory; use DataAccess\IFileRepository; @@ -20,6 +21,7 @@ class FileController implements IDivineController private $_downloadFactory; private $_userSession; private $_userQuota; + private $_statusReporter; public function __construct( IHttpRequest $request, @@ -28,7 +30,8 @@ class FileController implements IDivineController IDownloadFactory $downloadFactory, IDownloadRepository $downloadRepository, IUserSession $userSession, - IUserQuota $userQuota + IUserQuota $userQuota, + IStatusReporter $statusReporter ) { $this->_request = $request; $this->_response = $response; @@ -37,6 +40,7 @@ class FileController implements IDivineController $this->_downloadFactory = $downloadFactory; $this->_userSession = $userSession; $this->_userQuota = $userQuota; + $this->_statusReporter = $statusReporter; } public function indexAction() { @@ -106,16 +110,16 @@ class FileController implements IDivineController private function notAuthorised() { $this->_response->setHeader('Content-Type', 'application/json') - ->setBody(json_encode(array('error' => 'You must be authenticated to download files'))) - ->sendResponse(); + ->setBody($this->_statusReporter->error('You must be authenticated to download files')->json()) + ->sendResponse(); exit(); } private function notEnoughQuota() { $this->_response->setHeader('Content-Type', 'application/json') - ->setBody(json_encode(array('error' => 'You don\'t have enough quota remaining for this file.'))) - ->sendResponse(); + ->setBody($this->_statusReporter->error('You don\'t have enough quota remaining for this file.')->json()) + ->sendResponse(); exit(); } } diff --git a/Controllers/SimfileController.php b/Controllers/SimfileController.php index 06fe869..b9ca15e 100644 --- a/Controllers/SimfileController.php +++ b/Controllers/SimfileController.php @@ -2,12 +2,14 @@ namespace Controllers; +use Exception; use Controllers\IDivineController; use Services\Http\IHttpResponse; use Services\Uploads\IUploadManager; use Services\IUserSession; use Services\IZipParser; use Services\ISMOMatcher; +use Services\IStatusReporter; use DataAccess\StepMania\ISimfileRepository; use DataAccess\StepMania\IPackRepository; use DataAccess\IFileRepository; @@ -27,6 +29,8 @@ class SimfileController implements IDivineController private $_zipParser; private $_smoMatcher; private $_downloadRepository; + private $_statusReporter; + private $_userSession; public function __construct( IHttpResponse $response, @@ -37,7 +41,8 @@ class SimfileController implements IDivineController IUserSession $userSession, IZipParser $zipParser, ISMOMatcher $smoMatcher, - IDownloadRepository $downloadRepository + IDownloadRepository $downloadRepository, + IStatusReporter $statusReporter ) { $this->_response = $response; $this->_uploadManager = $uploadManager; @@ -47,6 +52,8 @@ class SimfileController implements IDivineController $this->_zipParser = $zipParser; $this->_smoMatcher = $smoMatcher; $this->_downloadRepository = $downloadRepository; + $this->_statusReporter = $statusReporter; + $this->_userSession = $userSession; } public function indexAction() { @@ -147,7 +154,9 @@ class SimfileController implements IDivineController } public function uploadAction() - { + { + if(!$this->_userSession->getCurrentUser()) $this->_statusReporter->error('You must be authenticated to upload files'); + //TODO: Put directory in config ? $files = $this->_uploadManager->setFilesDirectory('../files') ->setDestination('StepMania/') @@ -157,7 +166,9 @@ class SimfileController implements IDivineController { $zipParser = $this->_zipParser; $zipParser->parse($file); - + + if(!$zipParser->simfiles()) $this->_statusReporter->error('That zip doesn\'t seem to have any simfiles in it.'); + //save the actual zip in the db $this->findAndAddSmoMirror($file); $this->_fileRepository->save($file); @@ -181,6 +192,8 @@ class SimfileController implements IDivineController $this->_simfileRepository->save($simfile); } } + + $this->_statusReporter->success('Uploaded succesfully'); } private function getPackMirrorsArray(IPack $pack) diff --git a/Services/IStatusReporter.php b/Services/IStatusReporter.php new file mode 100644 index 0000000..97d5da8 --- /dev/null +++ b/Services/IStatusReporter.php @@ -0,0 +1,13 @@ +_response = $response; + } + + public function error($message) + { + $this->_message = $message; + $this->_type = self::ERROR; + $this->_response->setHeader('Content-Type', 'application/json') + ->setBody($this->json()) + ->sendResponse(); + exit(); + } + + public function success($message) + { + $this->_message = $message; + $this->_type = self::SUCCESS; + $this->_response->setHeader('Content-Type', 'application/json') + ->setBody($this->json()) + ->sendResponse(); + exit(); + } + + //no need to exit here, exceptions stop the program. + public function exception(Exception $exception) + { + //we'll be instatic context here so I have to do it this way. + header('Content-Type: application/json'); + echo json_encode(array(self::EXCEPTION => $exception->getMessage())); + } + + public function json() + { + return json_encode( + array($this->_type => $this->_message) + ); + } +} diff --git a/Services/Uploads/UploadManager.php b/Services/Uploads/UploadManager.php index 8530c2b..81c12c7 100644 --- a/Services/Uploads/UploadManager.php +++ b/Services/Uploads/UploadManager.php @@ -92,12 +92,12 @@ class UploadManager implements IUploadManager{ /* @var $file \Services\Uploads\IFile */ $results[] = $this->_fileBuilder->With_Hash($hash) - ->With_Path(rtrim($this->_destination, '/')) - ->With_Filename($file->getName()) - ->With_Mimetype($file->getType()) - ->With_Size($file->getSize()) - ->With_UploadDate(time()) - ->build(); + ->With_Path(rtrim($this->_destination, '/')) + ->With_Filename($file->getName()) + ->With_Mimetype($file->getType()) + ->With_Size($file->getSize()) + ->With_UploadDate(time()) + ->build(); } return $results; diff --git a/Services/UserSession.php b/Services/UserSession.php index 04505a0..ead323f 100644 --- a/Services/UserSession.php +++ b/Services/UserSession.php @@ -27,17 +27,19 @@ class UserSession implements IUserSession private function findToken() { - if($this->_request->isPost()) - { + //if($this->_request->isPost()) + //{ + //Try post $request = $this->_request->post(); if(!empty($request['token'])) return $request['token']; - } + //} - if($this->_request->isGet()) - { + //if($this->_request->isGet()) + //{ + //Try get $request = $this->_request->get(); if(!empty($request['token'])) return $request['token']; - } + //} //no good, try the body $body = json_decode($this->_request->getBody(), true); diff --git a/Services/ZipParser.php b/Services/ZipParser.php index d0418a7..f3a55f4 100644 --- a/Services/ZipParser.php +++ b/Services/ZipParser.php @@ -98,7 +98,8 @@ class ZipParser implements IZipParser //or single, but to do that we simply check the number of found sm files. To overcome this //first populate the smFiles array with the raw sm data, then apply SmDataToSmClass on each //array element. This way the check is accurate and the array gets populated as expected. - $this->_smFiles = array_map(array($this, 'SmDataToSmClass'), $this->_smFiles); + //@ing it because when exceptions are thrown this still produces a warning and I dont want that. + $this->_smFiles = @array_map(array($this, 'SmDataToSmClass'), $this->_smFiles); } private function packNameFromFiles() diff --git a/config/DI.php b/config/DI.php index 5d0838b..bbfd1d7 100644 --- a/config/DI.php +++ b/config/DI.php @@ -41,6 +41,7 @@ return [ 'Services\IZipParser' => DI\object('Services\ZipParser'), 'Services\IBannerExtracter' => DI\object('Services\BannerExtracter'), 'Services\ISMOMatcher' => DI\object('Services\SMOMatcher'), + 'Services\IStatusReporter' => DI\object('Services\StatusReporter'), //DA 'DataAccess\StepMania\ISimfileRepository' => DI\object('DataAccess\StepMania\SimfileRepository'), diff --git a/config/app.php.example b/config/app.php.example new file mode 100644 index 0000000..3b505ae --- /dev/null +++ b/config/app.php.example @@ -0,0 +1,6 @@ + 'http://roll.divinelegy.meeples:8000', + 'mode' => 'production' +]; \ No newline at end of file diff --git a/public_html/index.php b/public_html/index.php index d4843d3..2aa5c39 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -1,12 +1,15 @@