db.php
files
FacebookApp.php
+app.php
use Services\Http\IHttpRequest;
use Services\Http\IHttpResponse;
use Services\IUserSession;
+use Services\IStatusReporter;
use Services\IUserQuota;
use Domain\Entities\IDownloadFactory;
use DataAccess\IFileRepository;
private $_downloadFactory;
private $_userSession;
private $_userQuota;
+ private $_statusReporter;
public function __construct(
IHttpRequest $request,
IDownloadFactory $downloadFactory,
IDownloadRepository $downloadRepository,
IUserSession $userSession,
- IUserQuota $userQuota
+ IUserQuota $userQuota,
+ IStatusReporter $statusReporter
) {
$this->_request = $request;
$this->_response = $response;
$this->_downloadFactory = $downloadFactory;
$this->_userSession = $userSession;
$this->_userQuota = $userQuota;
+ $this->_statusReporter = $statusReporter;
}
public function indexAction() {
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();
}
}
\r
namespace Controllers;\r
\r
+use Exception;\r
use Controllers\IDivineController;\r
use Services\Http\IHttpResponse;\r
use Services\Uploads\IUploadManager;\r
use Services\IUserSession;\r
use Services\IZipParser;\r
use Services\ISMOMatcher;\r
+use Services\IStatusReporter;\r
use DataAccess\StepMania\ISimfileRepository;\r
use DataAccess\StepMania\IPackRepository;\r
use DataAccess\IFileRepository;\r
private $_zipParser;\r
private $_smoMatcher;\r
private $_downloadRepository;\r
+ private $_statusReporter;\r
+ private $_userSession;\r
\r
public function __construct(\r
IHttpResponse $response,\r
IUserSession $userSession,\r
IZipParser $zipParser,\r
ISMOMatcher $smoMatcher,\r
- IDownloadRepository $downloadRepository\r
+ IDownloadRepository $downloadRepository,\r
+ IStatusReporter $statusReporter\r
) {\r
$this->_response = $response;\r
$this->_uploadManager = $uploadManager;\r
$this->_zipParser = $zipParser;\r
$this->_smoMatcher = $smoMatcher;\r
$this->_downloadRepository = $downloadRepository;\r
+ $this->_statusReporter = $statusReporter;\r
+ $this->_userSession = $userSession;\r
}\r
\r
public function indexAction() {\r
}\r
\r
public function uploadAction()\r
- { \r
+ {\r
+ if(!$this->_userSession->getCurrentUser()) $this->_statusReporter->error('You must be authenticated to upload files');\r
+ \r
//TODO: Put directory in config ?\r
$files = $this->_uploadManager->setFilesDirectory('../files')\r
->setDestination('StepMania/')\r
{\r
$zipParser = $this->_zipParser;\r
$zipParser->parse($file);\r
- \r
+ \r
+ if(!$zipParser->simfiles()) $this->_statusReporter->error('That zip doesn\'t seem to have any simfiles in it.');\r
+ \r
//save the actual zip in the db\r
$this->findAndAddSmoMirror($file);\r
$this->_fileRepository->save($file); \r
$this->_simfileRepository->save($simfile);\r
}\r
}\r
+ \r
+ $this->_statusReporter->success('Uploaded succesfully');\r
}\r
\r
private function getPackMirrorsArray(IPack $pack)\r
--- /dev/null
+<?php
+
+namespace Services;
+
+use Exception;
+
+interface IStatusReporter
+{
+ public function success($message);
+ public function error($message);
+ public function exception(Exception $exception);
+ public function json();
+}
--- /dev/null
+<?php
+
+namespace Services;
+
+use Exception;
+use Services\Http\IHttpResponse;
+use Services\IStatusReporter;
+
+class StatusReporter implements IStatusReporter
+{
+ private $_message;
+ private $_type;
+ private $_response;
+
+ const ERROR = 'error';
+ const SUCCESS = 'success';
+ const EXCEPTION = 'exception';
+
+ public function __construct(IHttpResponse $response) {
+ $this->_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)
+ );
+ }
+}
/* @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;
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);
//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()
'Services\IZipParser' => DI\object('Services\ZipParser'),\r
'Services\IBannerExtracter' => DI\object('Services\BannerExtracter'),\r
'Services\ISMOMatcher' => DI\object('Services\SMOMatcher'),\r
+ 'Services\IStatusReporter' => DI\object('Services\StatusReporter'),\r
\r
//DA\r
'DataAccess\StepMania\ISimfileRepository' => DI\object('DataAccess\StepMania\SimfileRepository'),\r
--- /dev/null
+<?php
+
+return [
+ 'allow-origin' => 'http://roll.divinelegy.meeples:8000',
+ 'mode' => 'production'
+];
\ No newline at end of file
<?php\r
\r
-//TODO: Config this\r
-header("Access-Control-Allow-Origin: http://172.17.12.110:8000");\r
-header("Access-Control-Allow-Origin: http://roll.divinelegy.meeples:8000");\r
-//header("Access-Control-Allow-Origin: http://roll.divinelegy.dev:8000");\r
-\r
require_once('../vendor/autoload.php');\r
\r
+$config = require('../config/app.php');\r
+\r
+// Allow these origins to do cross domain JS.\r
+header("Access-Control-Allow-Origin: " . $config['allow-origin']);\r
+\r
+// Nice exceptions\r
+if($config['mode'] == 'production') set_exception_handler(array('\Services\StatusReporter', 'exception'));\r
+\r
// Everything time related should be UTC+0 based\r
date_default_timezone_set('UTC');\r
\r