8 use Services\IBannerExtracter
;
9 use Domain\Entities\IFileStepByStepBuilder
;
11 //TODO: This class can probably be refactored to be nicer. Also perhaps the methods could be static?
12 class BannerExtracter
implements IBannerExtracter
15 private $_destinationFileName;
18 public function __construct(IFileStepByStepBuilder
$builder) {
19 $this->_builder
= $builder;
22 public function extractSongBanner($zipfile, $bannerName) {
23 $za = new ZipArchive();
24 //XXX: We assume all files are zips. Should be enforced by validation elsewhere.
25 $res = $za->open($zipfile);
27 if($res !== true
) throw new Exception ('Could not open zip for reading.');
29 for($i=0; $i<$za->numFiles
; $i++
)
31 $stat = $za->statIndex($i);
32 $type = @exif_imagetype
('zip://' . realpath($zipfile) . '#' . $stat['name']);
33 //Sometimes simfiles specify a video as their banner. Fuck dat.
34 if(basename($stat['name']) == $bannerName && $type !== false
)
36 $this->_hash
= md5_file('zip://' . $zipfile . '#' . $stat['name']);
37 $this->_destinationFileName
= $this->_hash
. '.' . pathinfo($bannerName, PATHINFO_EXTENSION
);
38 $result = copy('zip://' . $zipfile . '#' . $stat['name'], '../files/banners/' . $this->_destinationFileName
);
43 if(!isset($result) ||
!$result) return null
;
45 $finfo = new finfo(FILEINFO_MIME
);
46 $mimetype = $finfo->file('../files/banners/' . $this->_destinationFileName
);
47 $size = filesize('../files/banners/' . $this->_destinationFileName
);
48 /* @var $fff \Domain\Entities\FileStepByStepBuilder */
49 return $this->_builder
->With_Hash($this->_hash
)
50 ->With_Path('banners')
51 ->With_Filename($bannerName)
52 ->With_Mimetype($mimetype)
54 ->With_UploadDate(time())
58 public function extractPackBanner($zipfile, $packname)
61 $za = new ZipArchive();
62 //XXX: We assume all files are zips. Should be enforced by validation elsewhere.
63 $res = $za->open($zipfile);
65 if($res !== true
) throw new Exception ('Could not open zip for reading.');
67 for($i=0; $i<$za->numFiles
; $i++
)
69 $stat = $za->statIndex($i);
70 $type = @exif_imagetype
('zip://' . realpath($zipfile) . '#' . $stat['name']);
74 $pathComponents = explode('/',$stat['name']);
76 //replace 3spooty with packname variable
77 if(count($pathComponents) == 2 && $pathComponents[0] == $packname)
79 $this->_hash
= md5_file('zip://' . realpath($zipfile) . '#' . $stat['name']);
80 $this->_destinationFileName
= $this->_hash
. '.' . pathinfo($stat['name'], PATHINFO_EXTENSION
);
81 $bannerName = $pathComponents[1];
82 $result = copy('zip://' . realpath($zipfile) . '#' . $stat['name'], '../files/banners/' . $this->_destinationFileName
);
88 if(!isset($result) ||
!$result) return null
;
90 $finfo = new finfo(FILEINFO_MIME
);
91 $mimetype = $finfo->file('../files/banners/' . $this->_destinationFileName
);
92 $size = filesize('../files/banners/' . $this->_destinationFileName
);
93 /* @var $fff \Domain\Entities\FileStepByStepBuilder */
94 return $this->_builder
->With_Hash($this->_hash
)
95 ->With_Path('banners')
96 ->With_Filename($bannerName)
97 ->With_Mimetype($mimetype)
99 ->With_UploadDate(time())
103 private function randomFilename($seed)
105 return sha1(mt_rand(1, 9999) . $seed . uniqid() . time());