From c1a1dc67713f9480bb4279c4e460169c39271e4a Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Thu, 19 Feb 2015 12:29:14 +0800 Subject: [PATCH] Files config directive. --- Controllers/FileController.php | 12 ++++++++---- Controllers/SimfileController.php | 11 ++++++----- Domain/Util.php | 17 +++++++++++++++++ Services/BannerExtracter.php | 17 ++++++++++------- Services/ZipParser.php | 14 +++++++++----- 5 files changed, 50 insertions(+), 21 deletions(-) diff --git a/Controllers/FileController.php b/Controllers/FileController.php index bd0c9e6..744a5fc 100644 --- a/Controllers/FileController.php +++ b/Controllers/FileController.php @@ -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()) diff --git a/Controllers/SimfileController.php b/Controllers/SimfileController.php index 5e83f9f..3f32c09 100644 --- a/Controllers/SimfileController.php +++ b/Controllers/SimfileController.php @@ -2,7 +2,6 @@ namespace Controllers; -use Exception; use Controllers\IDivineController; use Services\Http\IHttpResponse; use Services\Http\IHttpRequest; @@ -11,13 +10,12 @@ use Services\IUserSession; use Services\IZipParser; use Services\ISMOMatcher; use Services\IStatusReporter; +use Services\IConfigManager; use DataAccess\StepMania\ISimfileRepository; use DataAccess\StepMania\IPackRepository; use DataAccess\IFileRepository; use DataAccess\IDownloadRepository; -use Domain\Entities\StepMania\ISimfile; use Domain\Entities\IFile; -use Domain\Entities\StepMania\IPack; use Domain\Util; class SimfileController implements IDivineController @@ -33,6 +31,7 @@ class SimfileController implements IDivineController private $_downloadRepository; private $_statusReporter; private $_userSession; + private $_configManager; public function __construct( IHttpResponse $response, @@ -45,7 +44,8 @@ class SimfileController implements IDivineController IZipParser $zipParser, ISMOMatcher $smoMatcher, IDownloadRepository $downloadRepository, - IStatusReporter $statusReporter + IStatusReporter $statusReporter, + IConfigManager $configManager ) { $this->_response = $response; $this->_request = $request; @@ -58,6 +58,7 @@ class SimfileController implements IDivineController $this->_downloadRepository = $downloadRepository; $this->_statusReporter = $statusReporter; $this->_userSession = $userSession; + $this->_configManager = $configManager; } public function indexAction() { @@ -128,7 +129,7 @@ class SimfileController implements IDivineController if(!$this->_userSession->getCurrentUser()) $this->_statusReporter->error('You must be authenticated to upload files'); //TODO: Put directory in config ? - $files = $this->_uploadManager->setFilesDirectory('../files') + $files = $this->_uploadManager->setFilesDirectory($this->_configManager->getDirective('filesPath')) ->setDestination('StepMania/') ->process(); diff --git a/Domain/Util.php b/Domain/Util.php index 1da2c8d..9a9713f 100644 --- a/Domain/Util.php +++ b/Domain/Util.php @@ -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 diff --git a/Services/BannerExtracter.php b/Services/BannerExtracter.php index 4133cfe..2c29373 100644 --- a/Services/BannerExtracter.php +++ b/Services/BannerExtracter.php @@ -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') diff --git a/Services/ZipParser.php b/Services/ZipParser.php index 528771c..2286eb5 100644 --- a/Services/ZipParser.php +++ b/Services/ZipParser.php @@ -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()) -- 2.11.0