Files config directive.
authorCameron Ball <cameron@getapproved.com.au>
Thu, 19 Feb 2015 04:29:14 +0000 (12:29 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Thu, 19 Feb 2015 04:29:14 +0000 (12:29 +0800)
Controllers/FileController.php
Controllers/SimfileController.php
Domain/Util.php
Services/BannerExtracter.php
Services/ZipParser.php

index bd0c9e6..744a5fc 100644 (file)
@@ -8,6 +8,7 @@ use Services\Http\IHttpResponse;
 use Services\IUserSession;
 use Services\IStatusReporter;
 use Services\IUserQuota;
+use Services\IConfigManager;
 use Domain\Entities\IDownloadFactory;
 use DataAccess\IFileRepository;
 use DataAccess\IDownloadRepository;
@@ -22,6 +23,7 @@ class FileController implements IDivineController
     private $_userSession;
     private $_userQuota;
     private $_statusReporter;
+    private $_configManager;
     
     public function __construct(
         IHttpRequest $request,
@@ -31,7 +33,8 @@ class FileController implements IDivineController
         IDownloadRepository $downloadRepository,
         IUserSession $userSession,
         IUserQuota $userQuota,
-        IStatusReporter $statusReporter
+        IStatusReporter $statusReporter,
+        IConfigManager $configManager
     ) {
         $this->_request = $request;
         $this->_response = $response;
@@ -41,6 +44,7 @@ class FileController implements IDivineController
         $this->_userSession = $userSession;
         $this->_userQuota = $userQuota;
         $this->_statusReporter = $statusReporter;
+        $this->_configManager = $configManager;
     }
     
     public function indexAction() {
@@ -61,7 +65,7 @@ class FileController implements IDivineController
             if($hash == 'default') $this->serveDefaultBanner();
             if(!$file) $this->notFound();
 
-            $matches = glob(realpath('../files/' . $file->getPath()) . '/' . $file->getHash() . '.*');
+            $matches = glob(realpath($this->_configManager->getDirective('filesPath') . '/' . $file->getPath()) . '/' . $file->getHash() . '.*');
             $match = reset($matches);
             
             $this->_response->setHeader('Content-Type', $file->getMimetype())
@@ -82,7 +86,7 @@ class FileController implements IDivineController
         {
             $this->_response->setHeader("HTTP/1.1 304 Not Modified", 'Nice meme!');
         } else {
-            $path = '../files/banners/default.png';
+            $path = $this->_configManager->getDirective('filesPath') . '/banners/default.png';
             $file = realpath($path);
             $this->_response->setHeader('Content-Type', 'image/png')
                             ->setHeader('Content-Length', filesize($file))
@@ -113,7 +117,7 @@ class FileController implements IDivineController
         
         $this->_downloadRepository->save($download);
         
-        $zip = '../files/' . $file->getPath() . '/' . $file->getHash() . '.zip';
+        $zip = $this->_configManager->getDirective('filesPath') . '/' . $file->getPath() . '/' . $file->getHash() . '.zip';
         //TODO: This may not work on all browser or something. We'll have to see. Also it may hog ram so...
         $this->_response->setHeader('Content-Type', $file->getMimetype())
                         ->setHeader('Content-Length', $file->getSize())
index 5e83f9f..3f32c09 100644 (file)
@@ -2,7 +2,6 @@
 \r
 namespace Controllers;\r
 \r
-use Exception;\r
 use Controllers\IDivineController;\r
 use Services\Http\IHttpResponse;\r
 use Services\Http\IHttpRequest;\r
@@ -11,13 +10,12 @@ use Services\IUserSession;
 use Services\IZipParser;\r
 use Services\ISMOMatcher;\r
 use Services\IStatusReporter;\r
+use Services\IConfigManager;\r
 use DataAccess\StepMania\ISimfileRepository;\r
 use DataAccess\StepMania\IPackRepository;\r
 use DataAccess\IFileRepository;\r
 use DataAccess\IDownloadRepository;\r
-use Domain\Entities\StepMania\ISimfile;\r
 use Domain\Entities\IFile;\r
-use Domain\Entities\StepMania\IPack;\r
 use Domain\Util;\r
 \r
 class SimfileController implements IDivineController\r
@@ -33,6 +31,7 @@ class SimfileController implements IDivineController
     private $_downloadRepository;\r
     private $_statusReporter;\r
     private $_userSession;\r
+    private $_configManager;\r
     \r
     public function __construct(\r
         IHttpResponse $response,\r
@@ -45,7 +44,8 @@ class SimfileController implements IDivineController
         IZipParser $zipParser,\r
         ISMOMatcher $smoMatcher,\r
         IDownloadRepository $downloadRepository,\r
-        IStatusReporter $statusReporter\r
+        IStatusReporter $statusReporter,\r
+        IConfigManager $configManager\r
     ) {\r
         $this->_response = $response;\r
         $this->_request = $request;\r
@@ -58,6 +58,7 @@ class SimfileController implements IDivineController
         $this->_downloadRepository = $downloadRepository;\r
         $this->_statusReporter = $statusReporter;\r
         $this->_userSession = $userSession;\r
+        $this->_configManager = $configManager;\r
     }\r
     \r
     public function indexAction() {\r
@@ -128,7 +129,7 @@ class SimfileController implements IDivineController
         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
+        $files = $this->_uploadManager->setFilesDirectory($this->_configManager->getDirective('filesPath'))\r
                                       ->setDestination('StepMania/')\r
                                       ->process();\r
 \r
index 1da2c8d..9a9713f 100644 (file)
@@ -354,6 +354,9 @@ class Util
             }
         }
 
+        usort($singleSteps, array("Self", "cmp"));
+        usort($doubleSteps, array("Self", "cmp"));
+        
         return array(            
             'title' => $simfile->getTitle(),
             'artist' => $simfile->getArtist()->getName(),
@@ -371,4 +374,18 @@ class Util
             'hash' => $simfile->getSimfile() ? $simfile->getSimfile()->getHash() : null
         );
     }
+    
+    private static function cmp($a, $b)
+    {
+        $order = array(
+            'Novice' => 0,
+            'Easy' => 1,
+            'Medium' => 2,
+            'Hard' => 3,
+            'Expert' => 4,
+            'Edit' => 5
+        );
+                
+        return $order[$a['difficulty']] - $order[$b['difficulty']];
+    }
 }
\ No newline at end of file
index 4133cfe..2c29373 100644 (file)
@@ -6,6 +6,7 @@ use ZipArchive;
 use finfo;
 use Exception;
 use Services\IBannerExtracter;
+use Services\IConfigManager;
 use Domain\Entities\IFileStepByStepBuilder;
 
 //TODO: This class can probably be refactored to be nicer. Also perhaps the methods could be static?
@@ -14,9 +15,11 @@ class BannerExtracter implements IBannerExtracter
     private $_builder;
     private $_destinationFileName;
     private $_hash;
+    private $_configManager;
 
-    public function __construct(IFileStepByStepBuilder $builder) {
+    public function __construct(IFileStepByStepBuilder $builder, IConfigManager $configManager) {
         $this->_builder = $builder;
+        $this->_configManager = $configManager;
     }
     
     public function extractSongBanner($zipfile, $bannerName) {
@@ -35,7 +38,7 @@ class BannerExtracter implements IBannerExtracter
             {
                 $this->_hash = md5_file('zip://' . $zipfile . '#' . $stat['name']);
                 $this->_destinationFileName = $this->_hash . '.' . pathinfo($bannerName, PATHINFO_EXTENSION);
-                $result = copy('zip://' . $zipfile . '#' . $stat['name'], '../files/banners/' . $this->_destinationFileName);
+                $result = copy('zip://' . $zipfile . '#' . $stat['name'], $this->_configManager->getDirective('filesPath') . '/banners/' . $this->_destinationFileName);
                 break;
             }
         }
@@ -43,8 +46,8 @@ class BannerExtracter implements IBannerExtracter
         if(!isset($result) || !$result) return null;
 
         $finfo = new finfo(FILEINFO_MIME);
-        $mimetype = $finfo->file('../files/banners/' . $this->_destinationFileName);
-        $size = filesize('../files/banners/' . $this->_destinationFileName);
+        $mimetype = $finfo->file($this->_configManager->getDirective('filesPath') . '/banners/' . $this->_destinationFileName);
+        $size = filesize($this->_configManager->getDirective('filesPath') . '/banners/' . $this->_destinationFileName);
         /* @var $fff \Domain\Entities\FileStepByStepBuilder */
         return $this->_builder->With_Hash($this->_hash)
                               ->With_Path('banners')
@@ -79,7 +82,7 @@ class BannerExtracter implements IBannerExtracter
                     $this->_hash = md5_file('zip://' . realpath($zipfile) . '#' . $stat['name']);
                     $this->_destinationFileName = $this->_hash . '.' . pathinfo($stat['name'], PATHINFO_EXTENSION);
                     $bannerName = $pathComponents[1];
-                    $result = copy('zip://' . realpath($zipfile) . '#' . $stat['name'], '../files/banners/' . $this->_destinationFileName);
+                    $result = copy('zip://' . realpath($zipfile) . '#' . $stat['name'], $this->_configManager->getDirective('filesPath') . '/banners/' . $this->_destinationFileName);
                     break;
                 }
             }
@@ -88,8 +91,8 @@ class BannerExtracter implements IBannerExtracter
         if(!isset($result) || !$result) return null;
         
         $finfo = new finfo(FILEINFO_MIME);
-        $mimetype = $finfo->file('../files/banners/' . $this->_destinationFileName);
-        $size = filesize('../files/banners/' . $this->_destinationFileName);
+        $mimetype = $finfo->file($this->_configManager->getDirective('filesPath') . '/banners/' . $this->_destinationFileName);
+        $size = filesize($this->_configManager->getDirective('filesPath') . '/banners/' . $this->_destinationFileName);
         /* @var $fff \Domain\Entities\FileStepByStepBuilder */
         return $this->_builder->With_Hash($this->_hash)
                               ->With_Path('banners')
index 528771c..2286eb5 100644 (file)
@@ -15,6 +15,7 @@ use Services\IZipParser;
 use Services\IUserSession;
 use Services\IStatusReporter;
 use Services\InvalidSmFileException;
+use Services\IConfigManager;
 
 class ZipParser implements IZipParser
 {
@@ -27,6 +28,7 @@ class ZipParser implements IZipParser
     private $_userSession;
     private $_file;
     private $_statusReporter;
+    private $_configManager;
     
     public function __construct(
         ISimfileParser $smParser,
@@ -34,7 +36,8 @@ class ZipParser implements IZipParser
         IPackStepByStepBuilder $packBuilder,
         IBannerExtracter $bannerExtracter,
         IUserSession $userSession,
-        IStatusReporter $statusReporter
+        IStatusReporter $statusReporter,
+        IConfigManager $configManager
     ) {
         $this->_smParser = $smParser;
         $this->_smBuilder = $smBuilder;
@@ -42,6 +45,7 @@ class ZipParser implements IZipParser
         $this->_bannerExtracter = $bannerExtracter;
         $this->_userSession = $userSession;
         $this->_statusReporter = $statusReporter;
+        $this->_configManager = $configManager;
     }
     
     public function parse(IFile $file)
@@ -49,7 +53,7 @@ class ZipParser implements IZipParser
         $this->_file = $file;
         $this->_za = new ZipArchive();
         //XXX: We assume all files are zips. Should be enforced by validation elsewhere.
-        $res = $this->_za->open('../files/StepMania/' . $file->getHash() . '.zip');
+        $res = $this->_za->open($this->_configManager->getDirective('filesPath') . '/StepMania/' . $file->getHash() . '.zip');
 
         if($res !== true) throw new Exception ('Could not open zip for reading.');
         $this->findSms();
@@ -60,7 +64,7 @@ class ZipParser implements IZipParser
         if(count($this->_smFiles) > 1)
         {         
             $packname = $this->packNameFromFiles();
-            $banner = $this->_bannerExtracter->extractPackBanner('../files/StepMania/' . $this->_file->getHash() . '.zip', $packname);
+            $banner = $this->_bannerExtracter->extractPackBanner($this->_configManager->getDirective('filesPath') . '/StepMania/' . $this->_file->getHash() . '.zip', $packname);
 
             /* @var $builder \Domain\Entities\StepMania\PackStepByStepBuilder */
             $builder = $this->_packBuilder;
@@ -96,7 +100,7 @@ class ZipParser implements IZipParser
             $stat = $this->_za->statIndex($i);
             if(pathinfo($stat['name'], PATHINFO_EXTENSION) == 'sm')
             {
-                $path = realpath('../files/StepMania/' . $this->_file->getHash() . '.zip');
+                $path = realpath($this->_configManager->getDirective('filesPath') . '/StepMania/' . $this->_file->getHash() . '.zip');
                 $smData = file_get_contents('zip://' . $path . '#' . $stat['name']);
                 $this->_smFiles[$stat['name']] = $smData;
             }
@@ -149,7 +153,7 @@ class ZipParser implements IZipParser
     {
         $parser = $this->_smParser;
         $parser->parse($smData);
-        $banner = $this->_bannerExtracter->extractSongBanner(realpath('../files/StepMania/' . $this->_file->getHash() . '.zip'), $parser->banner());
+        $banner = $this->_bannerExtracter->extractSongBanner(realpath($this->_configManager->getDirective('filesPath') . '/StepMania/' . $this->_file->getHash() . '.zip'), $parser->banner());
         $file = $this->isPack() ? null : $this->_file;
 
         return $this->_smBuilder->With_Title($parser->title())