File upload stuff.
authorCameron Ball <cameron@getapproved.com.au>
Fri, 26 Sep 2014 04:41:44 +0000 (12:41 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Fri, 26 Sep 2014 04:41:44 +0000 (12:41 +0800)
16 files changed:
Controllers/IndexController.php
Controllers/SimfileController.php
DataAccess/StepMania/ISimfileRepository.php
DataAccess/StepMania/SimfileRepository.php
Services/Uploads/File.php [new file with mode: 0644]
Services/Uploads/FileFactory.php [new file with mode: 0644]
Services/Uploads/IFile.php [new file with mode: 0644]
Services/Uploads/IFileFactory.php [new file with mode: 0644]
Services/Uploads/IUploadManager.php [new file with mode: 0644]
Services/Uploads/UploadManager.php [new file with mode: 0644]
config/DI.php
config/Routes.php
files/StepMania/4e8b9a3fc8f0a6473e4734c979c57ab83606ba85.jpg [new file with mode: 0644]
files/StepMania/667fd7f14dc18941b6f0c338e0278e89b19cb46d.png [new file with mode: 0644]
public_html/index.php
public_html/upload.html [new file with mode: 0644]

index 101d71b..8587184 100644 (file)
@@ -28,7 +28,7 @@ class IndexController implements IDivineController
         $queryConstraints = new SimfileQueryConstraints();
         $queryConstraints->stepsHaveRating(15);
 
-        $simfiles = $this->_simfileRepository->findByTitle('a');
+        $simfiles = $this->_simfileRepository->findByBeginnerMeter(2);
 
         foreach($simfiles as $simfile)
         {
index 5bf7e78..84bdfdf 100644 (file)
@@ -5,6 +5,7 @@ namespace Controllers;
 use Controllers\IDivineController;\r
 use Services\Http\IHttpRequest;\r
 use Services\Http\IHttpResponse;\r
+use Services\Uploads\IUploadManager;\r
 use DataAccess\StepMania\ISimfileRepository;\r
 \r
 class SimfileController implements IDivineController\r
@@ -12,14 +13,17 @@ class SimfileController implements IDivineController
     private $_simfileRepository;\r
     private $_response;\r
     private $_request;\r
+    private $_uploadManager;\r
     \r
     public function __construct(\r
         IHttpRequest $request,\r
         IHttpResponse $response,\r
+        IUploadManager $uploadManager,\r
         ISimfileRepository $repository\r
     ) {\r
         $this->_request = $request;\r
         $this->_response = $response;\r
+        $this->_uploadManager = $uploadManager;\r
         $this->_simfileRepository = $repository;\r
     }\r
     \r
@@ -44,6 +48,19 @@ class SimfileController implements IDivineController
                         ->sendResponse();\r
     }\r
     \r
+    public function uploadAction()\r
+    {        \r
+        //logic for if pack or individual file\r
+        \r
+        //TODO: Put directory in config ?\r
+        $filenames = $this->_uploadManager->setDestination('../files/StepMania/')\r
+                                          ->process();\r
+        \r
+        echo '<pre>';\r
+        print_r($filenames);\r
+        echo '</pre>';\r
+    }\r
+    \r
     public function testAction($testArg)\r
     {\r
         $this->_response->setHeader('Content-Type', 'application/json')\r
index 940bcc0..bf15db2 100644 (file)
@@ -9,14 +9,14 @@ use Domain\Entities\StepMania\ISimfile;
 interface ISimfileRepository extends IRepository
 {
     public function findByTitle($title, ISimfileQueryConstraints $constraints);
-    public function findByArtist($artist);
-    public function findByBpm($high, $low);
-    public function findByStepArtist($artistName);
-    public function findByLightMeter($feet);
-    public function findByBeginnerMeter($feet);
-    public function findByMediumMeter($feet);
-    public function findByHardMeter($feet);
-    public function findByExpertMeter($feet);
+    public function findByArtist($artist, ISimfileQueryConstraints $constraints);
+    public function findByBpm($high, $low, ISimfileQueryConstraints $constraints);
+    public function findByStepArtist($artistName, ISimfileQueryConstraints $constraints);
+    public function findByLightMeter($feet, ISimfileQueryConstraints $constraints);
+    public function findByBeginnerMeter($feet, ISimfileQueryConstraints $constraints);
+    public function findByMediumMeter($feet, ISimfileQueryConstraints $constraints);
+    public function findByHardMeter($feet, ISimfileQueryConstraints $constraints);
+    public function findByExpertMeter($feet, ISimfileQueryConstraints $constraints);
     public function save(ISimfile $entity);
     public function remove(ISimfile $entity);
 }
index 7cd8522..87e6eaf 100644 (file)
@@ -5,6 +5,7 @@ namespace DataAccess\StepMania;
 use DataAccess\StepMania\ISimfileRepository;\r
 use DataAccess\DataMapper\IDataMapper;\r
 use DataAccess\Queries\IQueryBuilderFactory;\r
+use DataAccess\Queries\IQueryBuilder;\r
 use DataAccess\Queries\StepMania\ISimfileQueryConstraints;\r
 use Domain\Entities\StepMania\ISimfile;\r
 \r
@@ -43,25 +44,87 @@ class SimfileRepository implements ISimfileRepository
         ;\r
     }\r
     \r
+    private function applyConstraintsAndReturn(ISimfileQueryConstraints $constraints = NULL, IQueryBuilder $queryBuilder)\r
+    {\r
+        if($constraints)\r
+        {\r
+            $constraints->applyTo($queryBuilder);\r
+        }\r
+        \r
+        return $this->_dataMapper->map('Simfile', $queryBuilder);\r
+    }\r
+    \r
     public function findByTitle($title, ISimfileQueryConstraints $constraints = NULL)\r
     {\r
         $queryBuilder = $this->_queryBuilderFactory->createInstance();\r
         $queryBuilder->where('title', 'LIKE', "%%$title%%");\r
         \r
-        if($constraints)\r
+        return $this->applyConstraintsAndReturn($constraints, $queryBuilder);\r
+    }\r
+    \r
+    public function findByArtist($artist, ISimfileQueryConstraints $constraints = NULL)\r
+    {\r
+        $queryBuilder = $this->_queryBuilderFactory->createInstance();\r
+        $queryBuilder->where('artist', 'LIKE', "%%$artist%%");\r
+        \r
+        return $this->applyConstraintsAndReturn($constraints, $queryBuilder);\r
+    }\r
+    \r
+    public function findByBpm($high, $low = null, ISimfileQueryConstraints $constraints = NULL)\r
+    {\r
+        $queryBuilder = $this->_queryBuilderFactory->createInstance();\r
+        $queryBuilder->where('bpm_high', '=', $high);\r
+                \r
+        if($low)\r
         {\r
-            $constraints->applyTo($queryBuilder);\r
+            $queryBuilder->where('bpm_low', '=', $low);\r
         }\r
-\r
-        return $this->_dataMapper->map('Simfile', $queryBuilder);\r
+        \r
+        return $this->applyConstraintsAndReturn($constraints, $queryBuilder);\r
     }\r
     \r
-    public function findByArtist($artist){}\r
-    public function findByBpm($high, $low){}\r
-    public function findByStepArtist($artistName){}\r
-    public function findByLightMeter($feet){}\r
-    public function findByBeginnerMeter($feet){}\r
-    public function findByMediumMeter($feet){}\r
-    public function findByHardMeter($feet){}\r
-    public function findByExpertMeter($feet){}\r
+    public function findByStepArtist($artistName, ISimfileQueryConstraints $constraints = null)\r
+    {\r
+        $queryBuilder = $this->_queryBuilderFactory->createInstance();\r
+        $queryBuilder->join('inner', 'simfiles', 'id', 'steps', 'simfile_id')\r
+                     ->join('inner', 'steps', 'step_artist_id', 'step_artists', 'id')\r
+                     ->where('tag', 'LIKE', "%%$artistName%%");\r
+        \r
+        return $this->applyConstraintsAndReturn($constraints, $queryBuilder);\r
+    }\r
+    \r
+    private function findByDifficultyAndRating($difficulty, $rating, ISimfileQueryConstraints $constraints = null)\r
+    {\r
+        $queryBuilder = $this->_queryBuilderFactory->createInstance();\r
+        $queryBuilder->join('inner', 'simfiles', 'id', 'steps', 'simfile_id')\r
+                     ->where('difficulty', '=', $difficulty)\r
+                     ->where('rating', '=', $rating);\r
+        \r
+        return $this->applyConstraintsAndReturn($constraints, $queryBuilder);\r
+    }\r
+    \r
+    public function findByLightMeter($feet, ISimfileQueryConstraints $constraints = null)\r
+    {\r
+        return $this->findByDifficultyAndRating('light', $feet, $constraints);\r
+    }\r
+    \r
+    public function findByBeginnerMeter($feet, ISimfileQueryConstraints $constraints = null)\r
+    {\r
+        return $this->findByDifficultyAndRating('beginner', $feet, $constraints);\r
+    }\r
+    \r
+    public function findByMediumMeter($feet, ISimfileQueryConstraints $constraints = null)\r
+    {\r
+        return $this->findByDifficultyAndRating('medium', $feet, $constraints);\r
+    }\r
+    \r
+    public function findByHardMeter($feet, ISimfileQueryConstraints $constraints = null)\r
+    {\r
+        return $this->findByDifficultyAndRating('challenge', $feet, $constraints);\r
+    }\r
+    \r
+    public function findByExpertMeter($feet, ISimfileQueryConstraints $constraints = null)\r
+    {\r
+        return $this->findByDifficultyAndRating('expert', $feet, $constraints);\r
+    }\r
 }\r
diff --git a/Services/Uploads/File.php b/Services/Uploads/File.php
new file mode 100644 (file)
index 0000000..084ef75
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+
+namespace Services\Uploads;
+
+use Services\Uploads\IFile;
+
+class File implements IFile {
+    
+    private $_size;
+    private $_name;
+    private $_tempName;
+    private $_type;
+    
+    public function __construct($name, $type, $tempName, $size) {
+        $this->_name = $name;
+        $this->_type = $type;
+        $this->_tempName = $tempName;
+        $this->_size = $size;
+    }
+            
+    public function getExtension()
+    {
+        return pathinfo($this->_name, PATHINFO_EXTENSION);
+    }
+    
+    public function getTempName()
+    {
+        return $this->_tempName;
+    }
+    
+    public function getName()
+    {
+        return $this->_name;
+    }
+}
diff --git a/Services/Uploads/FileFactory.php b/Services/Uploads/FileFactory.php
new file mode 100644 (file)
index 0000000..8d90c8f
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+
+namespace Services\Uploads;
+
+use Services\Uploads\IFileFactory;
+use Services\Uploads\File;
+
+class FileFactory implements IFileFactory
+{
+    public function createInstance($name, $type, $tempName, $size) {
+        return new File($name, $type, $tempName, $size);
+    }
+}
diff --git a/Services/Uploads/IFile.php b/Services/Uploads/IFile.php
new file mode 100644 (file)
index 0000000..7a624c5
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+
+namespace Services\Uploads;
+
+interface IFile {
+    public function getExtension();
+    public function getName();
+    public function getTempName();
+}
\ No newline at end of file
diff --git a/Services/Uploads/IFileFactory.php b/Services/Uploads/IFileFactory.php
new file mode 100644 (file)
index 0000000..9ec8aca
--- /dev/null
@@ -0,0 +1,8 @@
+<?php
+
+namespace Services\Uploads;
+
+interface IFileFactory
+{
+    public function createInstance($name, $type, $tempName, $size);
+}
\ No newline at end of file
diff --git a/Services/Uploads/IUploadManager.php b/Services/Uploads/IUploadManager.php
new file mode 100644 (file)
index 0000000..29ac7a7
--- /dev/null
@@ -0,0 +1,8 @@
+<?php
+
+namespace Services\Uploads;
+
+interface IUploadManager {
+    public function setDestination($path);
+    public function process();
+}
diff --git a/Services/Uploads/UploadManager.php b/Services/Uploads/UploadManager.php
new file mode 100644 (file)
index 0000000..be9bc76
--- /dev/null
@@ -0,0 +1,79 @@
+<?php
+
+namespace Services\Uploads;
+
+use Services\Uploads\IUploadManager;
+use Services\Uploads\IFileFactory;
+use Services\Uploads\IFile;
+use Exception;
+
+class UploadManager implements IUploadManager{        
+    
+    private $_files= array();
+    private $_fileFactory;
+    private $_destination;
+        
+    public function __construct(IFileFactory $fileFactory) {
+        $this->_fileFactory = $fileFactory;
+        
+        if($_FILES) {
+            foreach($_FILES as $file)
+            {
+                $this->_files[] = $this->_fileFactory->createInstance(
+                    $file['name'],
+                    $file['type'],
+                    $file['tmp_name'],
+                    $file['size']
+                );
+            }
+        }
+    }
+    
+   public function setDestination($path) {
+        if(!$this->destinationExists($path))
+        {
+            throw new Exception('Invalid path. Path does not exist.');
+        }
+        
+        $this->_destination = $path;
+        
+        return $this;
+    }
+    
+    private function destinationExists($path) {
+        return file_exists($path);
+    }
+    
+    private function saveFile(IFile $file)
+    {
+        if($this->_destination)
+        {
+            $randomName = $this->randomFilename();
+            $result = move_uploaded_file($file->getTempName(), $this->_destination . '/' . $randomName . '.' . $file->getExtension());
+        }
+        
+        if(!$result)
+        {
+            throw new Exception("Could not save file.");
+        }
+        
+        return $randomName;
+    }
+    
+    private function randomFilename()
+    {
+        return sha1(mt_rand(1, 9999) . $this->_destination . uniqid() . time());
+    }
+    
+    public function process()
+    {
+        $results = array();
+        
+        foreach($this->_files as $file)
+        {
+            $results[$file->getName()] = $this->saveFile($file);
+        }
+        
+        return $results;
+    }
+}
index 2061c4f..992b015 100644 (file)
@@ -14,6 +14,8 @@ return [
     'Services\Http\IHttpRequest' => DI\object('Services\Http\HttpRequest'),\r
     'Services\Routing\IRouter' => DI\object('Services\Routing\Router')\r
         ->constructor(DI\link('router.maps')),\r
+    'Services\Uploads\IUploadManager' => DI\object('Services\Uploads\UploadManager'),\r
+    'Services\Uploads\IFileFactory' => DI\object('Services\Uploads\FileFactory'),\r
     \r
     //DA\r
     'DataAccess\StepMania\ISimfileRepository' => DI\object('DataAccess\StepMania\SimfileRepository'),\r
index a38b782..32a4255 100644 (file)
@@ -7,6 +7,12 @@ return [
         'action' => 'list'\r
     ],\r
     \r
+    '/simfiles/upload' => [\r
+        'methods' => ['POST'],\r
+        'controller' => 'Simfile',\r
+        'action' => 'upload'\r
+    ],\r
+        \r
     '/simfiles/argTest/:testarg' => [\r
         'methods' => ['GET'],\r
         'controller' => 'Simfile',\r
diff --git a/files/StepMania/4e8b9a3fc8f0a6473e4734c979c57ab83606ba85.jpg b/files/StepMania/4e8b9a3fc8f0a6473e4734c979c57ab83606ba85.jpg
new file mode 100644 (file)
index 0000000..2de58fd
Binary files /dev/null and b/files/StepMania/4e8b9a3fc8f0a6473e4734c979c57ab83606ba85.jpg differ
diff --git a/files/StepMania/667fd7f14dc18941b6f0c338e0278e89b19cb46d.png b/files/StepMania/667fd7f14dc18941b6f0c338e0278e89b19cb46d.png
new file mode 100644 (file)
index 0000000..e7b2ba1
Binary files /dev/null and b/files/StepMania/667fd7f14dc18941b6f0c338e0278e89b19cb46d.png differ
index 0118b64..d892c87 100644 (file)
@@ -1,4 +1,8 @@
 <?php\r
+\r
+//TODO: Config this\r
+header("Access-Control-Allow-Origin: http://172.17.12.110:8000");\r
+\r
 require_once('../vendor/autoload.php');\r
 \r
 // Set up the DI container\r
diff --git a/public_html/upload.html b/public_html/upload.html
new file mode 100644 (file)
index 0000000..953aeb4
--- /dev/null
@@ -0,0 +1,8 @@
+<html>
+    <body>
+        <form action="/simfiles/upload" method="post" enctype="multipart/form-data">
+            <input type="file" name="file" id="file" />
+            <input type="submit" name="submit" value="submit" />
+        </form>
+    </body>
+</html>
\ No newline at end of file