Merge changes from home PC
authorCameron Ball <c.ball1729@gmail.com>
Fri, 20 Feb 2015 12:19:52 +0000 (20:19 +0800)
committerCameron Ball <c.ball1729@gmail.com>
Fri, 20 Feb 2015 12:19:52 +0000 (20:19 +0800)
DataAccess/DataMapper/DataMapper.php
Services/BannerExtracter.php
Services/IBannerExtracter.php
Services/ZipParser.php
files/banners/default.png [deleted file]
public_html/upload.html

index 5c817dc..a4e474c 100644 (file)
@@ -153,14 +153,17 @@ class DataMapper implements IDataMapper
                     implode(', ', $info['columns']));\r
                 }\r
 \r
-                $queries[$index] = $query;\r
+                //The files table has hash and filename as a unique index.\r
+                //Some packs share the same banner for every file, and we only\r
+                //ever pull them out by hash. So this saves having loads of entries\r
+                $queries[$index] = $query . ' ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id)';\r
             }\r
 \r
            // if($queries['TYPE'] == AbstractPopulationHelper::QUERY_TYPE_CREATE)\r
            // {\r
                 $idMap = [];\r
                 foreach($queries as $index => $query)\r
-                {                                \r
+                {\r
                     $runQuery = true;\r
                     //originally was preg_quote('%').'(.*?)'.preg_quote('%') but that failed with things like:\r
                     //...VALUES ('Voyager Full 50%', %INDEX_REF_0%\r
index 2c29373..c5fdc50 100644 (file)
@@ -22,7 +22,7 @@ class BannerExtracter implements IBannerExtracter
         $this->_configManager = $configManager;
     }
     
-    public function extractSongBanner($zipfile, $bannerName) {
+    public function extractSongBanner($zipfile, $bannerIndex) {
         $za = new ZipArchive();
         //XXX: We assume all files are zips. Should be enforced by validation elsewhere.
         $res = $za->open($zipfile);
@@ -34,10 +34,10 @@ class BannerExtracter implements IBannerExtracter
             $stat = $za->statIndex($i);
             $type = @exif_imagetype('zip://' . realpath($zipfile) . '#' . $stat['name']);
             //Sometimes simfiles specify a video as their banner. Fuck dat.
-            if(basename($stat['name']) == $bannerName && $type !== false)
+            if($stat['name'] == $bannerIndex && $type !== false)
             {
                 $this->_hash = md5_file('zip://' . $zipfile . '#' . $stat['name']);
-                $this->_destinationFileName = $this->_hash . '.' . pathinfo($bannerName, PATHINFO_EXTENSION);
+                $this->_destinationFileName = $this->_hash . '.' . pathinfo($bannerIndex, PATHINFO_EXTENSION);
                 $result = copy('zip://' . $zipfile . '#' . $stat['name'], $this->_configManager->getDirective('filesPath') . '/banners/' . $this->_destinationFileName);
                 break;
             }
@@ -51,7 +51,7 @@ class BannerExtracter implements IBannerExtracter
         /* @var $fff \Domain\Entities\FileStepByStepBuilder */
         return $this->_builder->With_Hash($this->_hash)
                               ->With_Path('banners')
-                              ->With_Filename($bannerName)
+                              ->With_Filename(basename($bannerIndex))
                               ->With_Mimetype($mimetype)
                               ->With_Size($size)
                               ->With_UploadDate(time())
index a56a31f..ec963ee 100644 (file)
@@ -4,6 +4,6 @@ namespace Services;
 
 interface IBannerExtracter
 {
-    public function extractSongBanner($zipfile, $bannerName);
+    public function extractSongBanner($zipfile, $bannerIndex);
     public function extractPackBanner($zipfile, $packname);
 }
index 2286eb5..e349e6a 100644 (file)
@@ -114,7 +114,7 @@ class ZipParser implements IZipParser
         {
             try
             {
-                $this->_smFiles[$index] = $this->SmDataToSmClass($data);
+                $this->_smFiles[$index] = $this->SmDataToSmClass($data, $index);
             } catch(Exception $e) {
                 //Exceptions we care about at this stage
                 if(!$e instanceof InvalidSmFileException && 
@@ -149,11 +149,14 @@ class ZipParser implements IZipParser
         return $packName;
     }
         
-    private function SmDataToSmClass($smData)
-    {
+    private function SmDataToSmClass($smData, $index)
+    {      
         $parser = $this->_smParser;
         $parser->parse($smData);
-        $banner = $this->_bannerExtracter->extractSongBanner(realpath($this->_configManager->getDirective('filesPath') . '/StepMania/' . $this->_file->getHash() . '.zip'), $parser->banner());
+
+        $bannerIndex = $this->resolveBannerIndex($parser->banner(), $index);
+        $banner = $this->_bannerExtracter->extractSongBanner(realpath($this->_configManager->getDirective('filesPath') . '/StepMania/' . $this->_file->getHash() . '.zip'), $bannerIndex);
+
         $file = $this->isPack() ? null : $this->_file;
 
         return $this->_smBuilder->With_Title($parser->title())
@@ -169,4 +172,15 @@ class ZipParser implements IZipParser
                                 ->With_Banner($banner)
                                 ->build();
     }
+    
+    private function resolveBannerIndex($bannerDirective, $smFileIndex)
+    {
+        if(strpos($bannerDirective, '../') !== false || strpos($bannerDirective, '..\\') !== false)
+        {
+            $pathInfo = pathinfo($smFileIndex);
+            return str_replace('..', dirname($pathInfo['dirname']), $bannerDirective);
+        }
+        
+        return dirname($smFileIndex) . '/' . $bannerDirective;
+    }
 }
\ No newline at end of file
diff --git a/files/banners/default.png b/files/banners/default.png
deleted file mode 100644 (file)
index 68df6b7..0000000
Binary files a/files/banners/default.png and /dev/null differ
index a551bb2..eb59711 100644 (file)
@@ -2,7 +2,7 @@
     <body>
         <form action="/simfiles/upload" method="post" enctype="multipart/form-data">
             <input type="file" name="file" id="file" />
-            <input type="hidden" name="token" value="CAAFdJomTiBABAJqHJZBRqz3CMkFMfTlSbtUJsEeZCBUuPxl1ugT9Q4mVfwnYLdfAlwblEjFcYqliNwZBNIplbEZCNCEZBZCjX2Xp3bIvflBv0YYAATZCRgJkzq0S2SdfOY5pFEvRCWxZCH9SHBh1R6eqMmuDR1h5vSq4SDY9sMpf0G1nwtw5VRGz" />
+            <input type="hidden" name="token" value="CAAFdJomTiBABAMx0eKHYDJ89mTFFTUirrFQhlziCHoUlbl5aNWbRZA9fLQxLfKDhS8KPRblAdZBXt6kDkYRiNH1v0WDDqgEDekIgv76wUelNCpi0x2PKxZCJQxpwop0N4MqpwAYgqlGHEOz4Ns4h66q2wJrujfCPExJyQrFaUbRZB5Li2FZCTkJKW5ZBUbY9AZD" />
             <input type="submit" name="submit" value="submit" />
         </form>
     </body>