3b2928e9c10f997bca6a2d70c6ed56cb9042cb47
[rock.divinelegy.git] / Services / BannerExtracter.php
1 <?php
2
3 namespace Services;
4
5 use ZipArchive;
6 use finfo;
7 use Exception;
8 use Services\IBannerExtracter;
9 use Domain\Entities\IFileStepByStepBuilder;
10
11 //TODO: This class can probably be refactored to be nicer. Also perhaps the methods could be static?
12 class BannerExtracter implements IBannerExtracter
13 {
14 private $_builder;
15 private $_destinationFileName;
16 private $_hash;
17
18 public function __construct(IFileStepByStepBuilder $builder) {
19 $this->_builder = $builder;
20 }
21
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);
26
27 if($res !== true) throw new Exception ('Could not open zip for reading.');
28
29 for($i=0; $i<$za->numFiles; $i++)
30 {
31 $stat = $za->statIndex($i);
32 if(basename($stat['name']) == $bannerName)
33 {
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);
37 break;
38 }
39 }
40
41 if(!isset($result) || !$result) return null;
42
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)
51 ->With_Size($size)
52 ->With_UploadDate(time())
53 ->build();
54 }
55
56 public function extractPackBanner($zipfile, $packname)
57 {
58 $bannerName = '';
59 $za = new ZipArchive();
60 //XXX: We assume all files are zips. Should be enforced by validation elsewhere.
61 $res = $za->open($zipfile);
62
63 if($res !== true) throw new Exception ('Could not open zip for reading.');
64
65 for($i=0; $i<$za->numFiles; $i++)
66 {
67 $stat = $za->statIndex($i);
68 $type = @exif_imagetype('zip://' . realpath($zipfile) . '#' . $stat['name']);
69
70 if($type !== false)
71 {
72 $pathComponents = explode('/',$stat['name']);
73
74 //replace 3spooty with packname variable
75 if(count($pathComponents) == 2 && $pathComponents[0] == $packname)
76 {
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);
81 break;
82 }
83 }
84 }
85
86 if(!isset($result) || !$result) return null;
87
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)
96 ->With_Size($size)
97 ->With_UploadDate(time())
98 ->build();
99 }
100
101 private function randomFilename($seed)
102 {
103 return sha1(mt_rand(1, 9999) . $seed . uniqid() . time());
104 }
105 }