From b5724467626b777839406780963c393312763415 Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Fri, 7 Nov 2014 14:22:30 +0800 Subject: [PATCH] Changes to entities. File framework in place. --- Controllers/FileController.php | 50 ++++++++++++++++++ Controllers/SimfileController.php | 31 ++++++++++- DataAccess/DataMapper/Helpers/EntityMapsHelper.php | 6 ++- DataAccess/FileRepository.php | 49 +++++++++++++++++ DataAccess/IFileRepository.php | 12 +++++ DataAccess/IUserRepository.php | 1 - Domain/Entities/File.php | 61 ++++++++++++++++++++++ Domain/Entities/IFile.php | 13 +++++ Domain/Entities/StepMania/ISimfile.php | 1 + Domain/Entities/StepMania/Simfile.php | 9 ++++ config/DI.php | 1 + config/DataMaps.php | 15 ++++++ config/Routes.php | 6 +++ public_html/index.php | 2 +- 14 files changed, 252 insertions(+), 5 deletions(-) create mode 100644 Controllers/FileController.php create mode 100644 DataAccess/FileRepository.php create mode 100644 DataAccess/IFileRepository.php create mode 100644 Domain/Entities/File.php create mode 100644 Domain/Entities/IFile.php diff --git a/Controllers/FileController.php b/Controllers/FileController.php new file mode 100644 index 0000000..444ee85 --- /dev/null +++ b/Controllers/FileController.php @@ -0,0 +1,50 @@ +_request = $request; + $this->_response = $response; + $this->_fileRepository = $repository; + } + + public function indexAction() { + ; + } + + // list simfiles + public function serveBannerAction($hash) + { + $file = $this->_fileRepository->findByHash($hash); + + if(!$file) + { + $this->_response->setHeader('HTTP/1.0 404 Not Found', 'Nothing to see here') + ->setBody('Move along.') + ->sendResponse(); + + return; + } + + $match = reset(glob('../files/' . $file->getPath() . '/' . $file->getHash() . '.*')); + $this->_response->setHeader('Content-Type', $file->getMimetype()) + ->setHeader('Content-Length', $file->getSize()) + ->setBody(file_get_contents($match)) + ->sendResponse(); + } +} diff --git a/Controllers/SimfileController.php b/Controllers/SimfileController.php index b56cb4a..3a042d7 100644 --- a/Controllers/SimfileController.php +++ b/Controllers/SimfileController.php @@ -40,9 +40,36 @@ class SimfileController implements IDivineController foreach($simfiles as $simfile) { + $singleSteps = array(); + $doubleSteps = array(); + + foreach($simfile->getSteps() as $steps) + { + $stepDetails = array( + 'artist' => $steps->getArtist()->getTag(), + 'difficulty' => $steps->getDifficulty()->getITGName(), + 'rating' => $steps->getRating() + ); + + if($steps->getMode()->getPrettyName() == 'Single') + { + $singleSteps[] = $stepDetails; + } else { + $doubleSteps[] = $stepDetails; + } + } + $returnArray[] = array( + 'title' => $simfile->getTitle(), 'artist' => $simfile->getArtist()->getName(), - 'title' => $simfile->getTitle() + 'steps' => array( + 'single' => $singleSteps, + 'double' => $doubleSteps + ), + 'bgChanges' => $simfile->hasBgChanges() ? 'Yes' : 'No', + 'fgChanges' => $simfile->hasFgChanges() ? 'Yes' : 'No', + 'bpmChanges' => $simfile->hasBPMChanges() ? 'Yes' : 'No', + 'banner' => $simfile->getBanner() ? 'files/banner/' . $simfile->getBanner()->getHash() : 'files/banner/default' ); } @@ -59,6 +86,8 @@ class SimfileController implements IDivineController $filenames = $this->_uploadManager->setDestination('../files/StepMania/') ->process(); + //parse .sm files and save to DB. should use SimfileParser service + echo '
';
         print_r($filenames);
         echo '
'; diff --git a/DataAccess/DataMapper/Helpers/EntityMapsHelper.php b/DataAccess/DataMapper/Helpers/EntityMapsHelper.php index fe45000..f06975f 100644 --- a/DataAccess/DataMapper/Helpers/EntityMapsHelper.php +++ b/DataAccess/DataMapper/Helpers/EntityMapsHelper.php @@ -48,7 +48,7 @@ class EntityMapsHelper { $className = $maps[$this->_entityName]['class']; $table = $maps[$this->_entityName]['table']; - + // If the table we already have contains the id of a row we need in // another table if(isset($row[$this->_tableName . '_id'])) { @@ -58,8 +58,10 @@ class EntityMapsHelper $join_id)); $statement->execute(); $row = $statement->fetch(); + } else { + return null; } - + $constructors = AbstractPopulationHelper::getConstrutorArray($maps, $this->_entityName, $row, $db); if(count($constructors) == 0) diff --git a/DataAccess/FileRepository.php b/DataAccess/FileRepository.php new file mode 100644 index 0000000..2e8f9a4 --- /dev/null +++ b/DataAccess/FileRepository.php @@ -0,0 +1,49 @@ +_dataMapper = $dataMapper; + $this->_queryBuilderFactory = $queryBuilderFactory; + } + + public function findById($id) { + $queryBuilder = $this->_queryBuilderFactory->createInstance(); + $queryBuilder->where('id', '=', "$id"); + + return $this->_dataMapper->map( + 'File', + $queryBuilder + ); + } + + public function findRange($id, $limit) + { + return $this->_dataMapper->findRange( + 'User', + 'SELECT * FROM %s WHERE id>=' . $id . ' LIMIT ' . $limit + ); + } + + public function findByHash($hash) + { + $queryBuilder = $this->_queryBuilderFactory->createInstance(); + $queryBuilder->where('hash', '=', "$hash"); + + $results = $this->_dataMapper->map( + 'File', + $queryBuilder + ); + + return reset($results); + } +} diff --git a/DataAccess/IFileRepository.php b/DataAccess/IFileRepository.php new file mode 100644 index 0000000..fb82c76 --- /dev/null +++ b/DataAccess/IFileRepository.php @@ -0,0 +1,12 @@ +_hash = $hash; + $this->_path = $path; + $this->_filename = $filename; + $this->_mimetype = $mimetype; + $this->_size = $size; + $this->_uploadDate = $uploadDate; + } + + public function getFilename() + { + return $this->_filename; + } + + public function getHash() + { + return $this->_hash; + } + + public function getPath() + { + return $this->_path; + } + + public function getMimetype() + { + return $this->_mimetype; + } + + public function getSize() + { + return $this->_size; + } + + public function getUploadDate() + { + return $this->_uploadDate; + } +} \ No newline at end of file diff --git a/Domain/Entities/IFile.php b/Domain/Entities/IFile.php new file mode 100644 index 0000000..4ce49c3 --- /dev/null +++ b/Domain/Entities/IFile.php @@ -0,0 +1,13 @@ +_title = $title; @@ -42,6 +45,7 @@ class Simfile extends AbstractEntity implements ISimfile $this->_stops = $stops; $this->_fgChanges = $fgChanges; $this->_bgChanges = $bgChanges; + $this->_banner = $banner; foreach($steps as $stepChart) { if(!$stepChart instanceof IStepChart) { @@ -96,6 +100,11 @@ class Simfile extends AbstractEntity implements ISimfile $this->_steps[] = $stepChart; } + public function getBanner() + { + return $this->_banner; + } + public function getSteps() { return $this->_steps; diff --git a/config/DI.php b/config/DI.php index 49627d5..86dbb1f 100644 --- a/config/DI.php +++ b/config/DI.php @@ -26,6 +26,7 @@ return [ //DA 'DataAccess\StepMania\ISimfileRepository' => DI\object('DataAccess\StepMania\SimfileRepository'), 'DataAccess\IUserRepository' => DI\object('DataAccess\UserRepository'), + 'DataAccess\IFileRepository' => DI\object('DataAccess\FileRepository'), 'DataAccess\IDatabaseFactory' => DI\object('DataAccess\DatabaseFactory') ->constructor(DI\link('db.credentials')), 'DataAccess\DataMapper\IDataMapper' => DI\object('DataAccess\DataMapper\DataMapper') diff --git a/config/DataMaps.php b/config/DataMaps.php index 559e7ef..d18492f 100644 --- a/config/DataMaps.php +++ b/config/DataMaps.php @@ -20,6 +20,7 @@ return [ 'stops' => DataAccess\Int('stops', 'hasStops'), 'fgChanges' => DataAccess\Int('fg_changes', 'hasFgChanges'), 'bgChanges' => DataAccess\Int('bg_changes', 'hasBgChanges'), + 'banner' => DataAccess\Entity('File', 'getBanner', 'banner_file'), 'steps' => DataAccess\VOArray('StepChart', 'getSteps') ] ], @@ -111,5 +112,19 @@ return [ 'maps' => [ 'stepManiaName' => DataAccess\Varchar('difficulty', 'getStepManiaName') ] + ], + + 'File' => [ + 'class' => 'Domain\Entities\File', + 'table' => 'files', + 'maps' => [ + 'hash' => DataAccess\Varchar('hash'), + 'path' => DataAccess\Varchar('path'), + 'filename' => DataAccess\Varchar('filename'), + 'mimetype' => DataAccess\Varchar('mimetype'), + 'size' => DataAccess\Int('size'), + //TODO: actually this is stored as a datetime, might need datetime helper for stuff + 'uploadDate' => DataAccess\Varchar('uploaded') + ] ] ]; diff --git a/config/Routes.php b/config/Routes.php index 73c3cae..1d0eda5 100644 --- a/config/Routes.php +++ b/config/Routes.php @@ -28,5 +28,11 @@ return [ 'method' => ['GET'], 'controller' => 'User', 'action' => 'getUser' + ], + + '/files/banner/:hash' => [ + 'method' => ['GET'], + 'controller' => 'File', + 'action' => 'serveBanner' ] ]; diff --git a/public_html/index.php b/public_html/index.php index 56627e8..17e529f 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -3,7 +3,7 @@ //TODO: Config this header("Access-Control-Allow-Origin: http://172.17.12.110:8000"); header("Access-Control-Allow-Origin: http://roll.divinelegy.meeples:8000"); -header("Access-Control-Allow-Origin: http://roll.divinelegy.dev:8000"); +//header("Access-Control-Allow-Origin: http://roll.divinelegy.dev:8000"); require_once('../vendor/autoload.php'); -- 2.11.0