8 use Services\IBannerExtracter
;
9 use Services\IConfigManager
;
10 use Domain\Entities\IFileStepByStepBuilder
;
12 //TODO: This class can probably be refactored to be nicer. Also perhaps the methods could be static?
13 class BannerExtracter
implements IBannerExtracter
16 private $_destinationFileName;
18 private $_configManager;
20 public function __construct(IFileStepByStepBuilder
$builder, IConfigManager
$configManager) {
21 $this->_builder
= $builder;
22 $this->_configManager
= $configManager;
25 public function extractSongBanner($zipfile, $bannerName) {
26 $za = new ZipArchive();
27 //XXX: We assume all files are zips. Should be enforced by validation elsewhere.
28 $res = $za->open($zipfile);
30 if($res !== true
) throw new Exception ('Could not open zip for reading.');
32 for($i=0; $i<$za->numFiles
; $i++
)
34 $stat = $za->statIndex($i);
35 $type = @exif_imagetype
('zip://' . realpath($zipfile) . '#' . $stat['name']);
36 //Sometimes simfiles specify a video as their banner. Fuck dat.
37 if(basename($stat['name']) == $bannerName && $type !== false
)
39 $this->_hash
= md5_file('zip://' . $zipfile . '#' . $stat['name']);
40 $this->_destinationFileName
= $this->_hash
. '.' . pathinfo($bannerName, PATHINFO_EXTENSION
);
41 $result = copy('zip://' . $zipfile . '#' . $stat['name'], $this->_configManager
->getDirective('filesPath') . '/banners/' . $this->_destinationFileName
);
46 if(!isset($result) ||
!$result) return null
;
48 $finfo = new finfo(FILEINFO_MIME
);
49 $mimetype = $finfo->file($this->_configManager
->getDirective('filesPath') . '/banners/' . $this->_destinationFileName
);
50 $size = filesize($this->_configManager
->getDirective('filesPath') . '/banners/' . $this->_destinationFileName
);
51 /* @var $fff \Domain\Entities\FileStepByStepBuilder */
52 return $this->_builder
->With_Hash($this->_hash
)
53 ->With_Path('banners')
54 ->With_Filename($bannerName)
55 ->With_Mimetype($mimetype)
57 ->With_UploadDate(time())
61 public function extractPackBanner($zipfile, $packname)
64 $za = new ZipArchive();
65 //XXX: We assume all files are zips. Should be enforced by validation elsewhere.
66 $res = $za->open($zipfile);
68 if($res !== true
) throw new Exception ('Could not open zip for reading.');
70 for($i=0; $i<$za->numFiles
; $i++
)
72 $stat = $za->statIndex($i);
73 $type = @exif_imagetype
('zip://' . realpath($zipfile) . '#' . $stat['name']);
77 $pathComponents = explode('/',$stat['name']);
79 //replace 3spooty with packname variable
80 if(count($pathComponents) == 2 && $pathComponents[0] == $packname)
82 $this->_hash
= md5_file('zip://' . realpath($zipfile) . '#' . $stat['name']);
83 $this->_destinationFileName
= $this->_hash
. '.' . pathinfo($stat['name'], PATHINFO_EXTENSION
);
84 $bannerName = $pathComponents[1];
85 $result = copy('zip://' . realpath($zipfile) . '#' . $stat['name'], $this->_configManager
->getDirective('filesPath') . '/banners/' . $this->_destinationFileName
);
91 if(!isset($result) ||
!$result) return null
;
93 $finfo = new finfo(FILEINFO_MIME
);
94 $mimetype = $finfo->file($this->_configManager
->getDirective('filesPath') . '/banners/' . $this->_destinationFileName
);
95 $size = filesize($this->_configManager
->getDirective('filesPath') . '/banners/' . $this->_destinationFileName
);
96 /* @var $fff \Domain\Entities\FileStepByStepBuilder */
97 return $this->_builder
->With_Hash($this->_hash
)
98 ->With_Path('banners')
99 ->With_Filename($bannerName)
100 ->With_Mimetype($mimetype)
102 ->With_UploadDate(time())
106 private function randomFilename($seed)
108 return sha1(mt_rand(1, 9999) . $seed . uniqid() . time());