|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Stinger Media Parser package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Oliver Kotte <[email protected]> |
|
7
|
|
|
* (c) Florian Meyer <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace StingerSoft\MediaParsingBundle\Parser; |
|
14
|
|
|
|
|
15
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
|
16
|
|
|
use StingerSoft\MediaParsingBundle\Parser\Information\FileInformation; |
|
17
|
|
|
|
|
18
|
|
|
class ParserChain implements ParserChainInterface{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* |
|
22
|
|
|
* @var MediaParserInterface[] |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
private $parser = array(); |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
public function addParser(MediaParserInterface $parser){ |
|
29
|
|
|
$this->parser[] = $parser; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* |
|
34
|
|
|
* @param File $file |
|
35
|
|
|
* @return MediaParserInterface|NULL |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getParser(File $file){ |
|
38
|
|
|
foreach($this->parser as $parser){ |
|
39
|
|
|
if($parser->canHandle($file)){ |
|
40
|
|
|
return $parser; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
return null; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* |
|
48
|
|
|
* @param string|File $file |
|
49
|
|
|
* @throws \Exception |
|
50
|
|
|
* @return MediaInformationInterface|boolean |
|
51
|
|
|
*/ |
|
52
|
|
|
public function parseFile($file){ |
|
53
|
|
|
if($file === null){ |
|
54
|
|
|
throw new \Exception("File is null!"); |
|
55
|
|
|
} |
|
56
|
|
|
if(\is_string($file)){ |
|
57
|
|
|
$file = new File($this->createPath($file)); |
|
58
|
|
|
} |
|
59
|
|
|
if(!$file->isReadable()){ |
|
60
|
|
|
throw new \Exception("Can't read file <".$file->getFilename().">"); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$parser = $this->getParser($file); |
|
64
|
|
|
if(!$parser){ |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$info = $parser->parseFile($file); |
|
69
|
|
|
|
|
70
|
|
|
//No real information found for the given file, so we fall back to a basic file |
|
71
|
|
|
//information object |
|
72
|
|
|
if(!$info){ |
|
73
|
|
|
$info = new FileInformation(); |
|
74
|
|
|
$info->setTitle($file->getFilename()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$info->setFilePath($file->getRealPath()); |
|
78
|
|
|
$info->setFileSize($file->getSize()); |
|
79
|
|
|
$lastModified = \DateTime::createFromFormat('U', $file->getMTime()); |
|
80
|
|
|
if($lastModified !== false){ |
|
81
|
|
|
$info->setLastModified($lastModified); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $info; |
|
85
|
|
|
} |
|
86
|
|
|
/* (non-PHPdoc) |
|
87
|
|
|
* @see \StingerSoft\MediaParsingBundle\Parser\ParserChainInterface::getAllParser() |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getAllParser(){ |
|
90
|
|
|
return $this->parser; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
protected function createPath($path){ |
|
94
|
|
|
if($this->startsWith($path, 'B64')){ |
|
95
|
|
|
$path = \base64_decode(\substr($path, 3)); |
|
96
|
|
|
} |
|
97
|
|
|
return $path; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function startsWith($haystack, $needle){ |
|
101
|
|
|
return !strncmp($haystack, $needle, strlen($needle)); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|