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 if(basename($stat['name']) == $bannerName)
34 $this->_hash
= md5_file('zip://' . $zipfile . '#' . $stat['name']);
35 $this->_destinationFileName
= $this->_hash
. '.' . pathinfo($bannerName, PATHINFO_EXTENSION
);
36 $result = copy('zip://' . $zipfile . '#' . $stat['name'], '../files/banners/' . $this->_destinationFileName
);
41 if(!isset($result) ||
!$result) return null
;
43 $finfo = new finfo(FILEINFO_MIME
);
44 $mimetype = $finfo->file('../files/banners/' . $this->_destinationFileName
);
45 $size = filesize('../files/banners/' . $this->_destinationFileName
);
46 /* @var $fff \Domain\Entities\FileStepByStepBuilder */
47 return $this->_builder
->With_Hash($this->_hash
)
48 ->With_Path('banners')
49 ->With_Filename($bannerName)
50 ->With_Mimetype($mimetype)
52 ->With_UploadDate(time())
56 public function extractPackBanner($zipfile, $packname)
59 $za = new ZipArchive();
60 //XXX: We assume all files are zips. Should be enforced by validation elsewhere.
61 $res = $za->open($zipfile);
63 if($res !== true
) throw new Exception ('Could not open zip for reading.');
65 for($i=0; $i<$za->numFiles
; $i++
)
67 $stat = $za->statIndex($i);
68 $type = @exif_imagetype
('zip://' . realpath($zipfile) . '#' . $stat['name']);
72 $pathComponents = explode('/',$stat['name']);
74 //replace 3spooty with packname variable
75 if(count($pathComponents) == 2 && $pathComponents[0] == $packname)
77 $this->_hash
= md5_file('zip://' . realpath($zipfile) . '#' . $stat['name']);
78 $this->_destinationFileName
= $this->_hash
. '.' . pathinfo($stat['name'], PATHINFO_EXTENSION
);
79 $bannerName = $pathComponents[1];
80 $result = copy('zip://' . realpath($zipfile) . '#' . $stat['name'], '../files/banners/' . $this->_destinationFileName
);
86 if(!isset($result) ||
!$result) return null
;
88 $finfo = new finfo(FILEINFO_MIME
);
89 $mimetype = $finfo->file('../files/banners/' . $this->_destinationFileName
);
90 $size = filesize('../files/banners/' . $this->_destinationFileName
);
91 /* @var $fff \Domain\Entities\FileStepByStepBuilder */
92 return $this->_builder
->With_Hash($this->_hash
)
93 ->With_Path('banners')
94 ->With_Filename($bannerName)
95 ->With_Mimetype($mimetype)
97 ->With_UploadDate(time())
101 private function randomFilename($seed)
103 return sha1(mt_rand(1, 9999) . $seed . uniqid() . time());