Massive number horse shit.
[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 $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)
35 {
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);
39 break;
40 }
41 }
42
43 if(!isset($result) || !$result) return null;
44
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)
53 ->With_Size($size)
54 ->With_UploadDate(time())
55 ->build();
56 }
57
58 public function extractPackBanner($zipfile, $packname)
59 {
60 $bannerName = '';
61 $za = new ZipArchive();
62 //XXX: We assume all files are zips. Should be enforced by validation elsewhere.
63 $res = $za->open($zipfile);
64
65 if($res !== true) throw new Exception ('Could not open zip for reading.');
66
67 for($i=0; $i<$za->numFiles; $i++)
68 {
69 $stat = $za->statIndex($i);
70 $type = @exif_imagetype('zip://' . realpath($zipfile) . '#' . $stat['name']);
71
72 if($type !== false)
73 {
74 $pathComponents = explode('/',$stat['name']);
75
76 //replace 3spooty with packname variable
77 if(count($pathComponents) == 2 && $pathComponents[0] == $packname)
78 {
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);
83 break;
84 }
85 }
86 }
87
88 if(!isset($result) || !$result) return null;
89
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)
98 ->With_Size($size)
99 ->With_UploadDate(time())
100 ->build();
101 }
102
103 private function randomFilename($seed)
104 {
105 return sha1(mt_rand(1, 9999) . $seed . uniqid() . time());
106 }
107 }