2c29373bd97e675416aaff7e938d4eadebadc87e
[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 Services\IConfigManager;
10 use Domain\Entities\IFileStepByStepBuilder;
11
12 //TODO: This class can probably be refactored to be nicer. Also perhaps the methods could be static?
13 class BannerExtracter implements IBannerExtracter
14 {
15 private $_builder;
16 private $_destinationFileName;
17 private $_hash;
18 private $_configManager;
19
20 public function __construct(IFileStepByStepBuilder $builder, IConfigManager $configManager) {
21 $this->_builder = $builder;
22 $this->_configManager = $configManager;
23 }
24
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);
29
30 if($res !== true) throw new Exception ('Could not open zip for reading.');
31
32 for($i=0; $i<$za->numFiles; $i++)
33 {
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)
38 {
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);
42 break;
43 }
44 }
45
46 if(!isset($result) || !$result) return null;
47
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)
56 ->With_Size($size)
57 ->With_UploadDate(time())
58 ->build();
59 }
60
61 public function extractPackBanner($zipfile, $packname)
62 {
63 $bannerName = '';
64 $za = new ZipArchive();
65 //XXX: We assume all files are zips. Should be enforced by validation elsewhere.
66 $res = $za->open($zipfile);
67
68 if($res !== true) throw new Exception ('Could not open zip for reading.');
69
70 for($i=0; $i<$za->numFiles; $i++)
71 {
72 $stat = $za->statIndex($i);
73 $type = @exif_imagetype('zip://' . realpath($zipfile) . '#' . $stat['name']);
74
75 if($type !== false)
76 {
77 $pathComponents = explode('/',$stat['name']);
78
79 //replace 3spooty with packname variable
80 if(count($pathComponents) == 2 && $pathComponents[0] == $packname)
81 {
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);
86 break;
87 }
88 }
89 }
90
91 if(!isset($result) || !$result) return null;
92
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)
101 ->With_Size($size)
102 ->With_UploadDate(time())
103 ->build();
104 }
105
106 private function randomFilename($seed)
107 {
108 return sha1(mt_rand(1, 9999) . $seed . uniqid() . time());
109 }
110 }