-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathFile.php
More file actions
181 lines (154 loc) · 6.04 KB
/
File.php
File metadata and controls
181 lines (154 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php namespace Codesleeve\Stapler\Factories;
use Codesleeve\Stapler\Stapler;
use Codesleeve\Stapler\File\Mime\MimeType;
use Codesleeve\Stapler\File\File as StaplerFile;
use Codesleeve\Stapler\Exceptions\FileException;
use Codesleeve\Stapler\File\UploadedFile as StaplerUploadedFile;
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
class File
{
/**
* A instance of Symfony's MIME type extension guesser interface.
*
* @var \Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface
*/
protected static $mimeTypeExtensionGuesser;
/**
* Build a Codesleeve\Stapler\UploadedFile object using various file input types.
*
* @param mixed $file
* @param boolean $testing
* @return \Codesleeve\Stapler\File\UploadedFile
*/
public static function create($file, $testing = false)
{
if ($file instanceof SymfonyUploadedFile) {
return static::createFromObject($file);
}
if (is_array($file)) {
return static::createFromArray($file, $testing);
}
if (substr($file, 0, 7) == "http://" || substr($file, 0, 8) == "https://") {
return static::createFromUrl($file);
}
if (preg_match('#^data:[a-z]+/[a-z]+;base64#', $file)) {
return static::createFromDataURI($file);
}
return static::createFromString($file);
}
/**
* Compose a \Codesleeve\Stapler\File\UploadedFile object from
* a \Symfony\Component\HttpFoundation\File\UploadedFile object.
*
* @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
* @return \Codesleeve\Stapler\File\UploadedFile
*/
protected static function createFromObject(SymfonyUploadedFile $file)
{
$staplerFile = new StaplerUploadedFile($file);
$staplerFile->validate();
return $staplerFile;
}
protected static function createFromDataURI($file) {
$fp = @fopen($file, 'r');
if (!$fp) {
throw new \Codesleeve\Stapler\Exceptions\FileException('Invalid data URI');
}
$meta = stream_get_meta_data($fp);
$extension = static::getMimeTypeExtensionGuesserInstance()->guess($meta['mediatype']);
$filePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . md5($meta['uri']) . "." . $extension;
file_put_contents($filePath, stream_get_contents($fp));
return new StaplerFile($filePath);
}
/**
* Build a Codesleeve\Stapler\File\File object from the
* raw php $_FILES array date. We assume here that the $_FILES array
* has been formated using the Stapler::arrangeFiles utility method.
*
* @param array $file
* @param boolean $testing
* @return \Codesleeve\Stapler\File\File
*/
protected static function createFromArray(array $file, $testing)
{
$file = new SymfonyUploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error'], $testing);
return static::createFromObject($file);
}
/**
* Fetch a remote file using a string URL and convert it into
* an instance of Codesleeve\Stapler\File\File.
*
* @param string $file
* @return \Codesleeve\Stapler\File\File
*/
protected static function createFromUrl($file)
{
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
if ($curl_options = Stapler::getConfigInstance()->get('stapler.curl_options')) {
curl_setopt_array($ch, $curl_options);
}
if (!$rawFile = curl_exec($ch)) {
$errMsg = "Unable to download file: $file\n";
throw new FileException($errMsg . curl_error($ch), curl_errno($ch));
}
curl_close($ch);
// Remove the query string if it exists
// We should do this before fetching the pathinfo() so that the extension is valid
if (strpos($file, '?') !== false) {
list($file, $queryString) = explode('?', $file);
}
// Get the original name of the file
$pathinfo = pathinfo($file);
$name = $pathinfo['basename'];
// Create a filepath for the file by storing it on disk.
$filePath = sys_get_temp_dir() . "/$name";
file_put_contents($filePath, $rawFile);
if (empty($pathinfo['extension']))
{
$mimeType = MimeTypeGuesser::getInstance()->guess($filePath);
$extension = static::getMimeTypeExtensionGuesserInstance()->guess($mimeType);
unlink($filePath);
$filePath = sys_get_temp_dir() . "/$name" . "." . $extension;
file_put_contents($filePath, $rawFile);
}
return new StaplerFile($filePath);
}
/**
* Fetch a local file using a string location and convert it into
* an instance of \Codesleeve\Stapler\File\File.
*
* @param string $file
* @return \Codesleeve\Stapler\File\File
*/
protected static function createFromString($file)
{
return new StaplerFile($file, pathinfo($file)['basename']);
}
/**
* Return an instance of the Symfony MIME type extension guesser.
*
* @return \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesserInterface
*/
public static function getMimeTypeExtensionGuesserInstance()
{
if (!static::$mimeTypeExtensionGuesser) {
static::$mimeTypeExtensionGuesser = new MimeTypeExtensionGuesser;
}
return static::$mimeTypeExtensionGuesser;
}
/**
* Set the configuration object instance.
*
* @param ConfigurableInterface $config
*/
public static function setConfigInstance(ConfigurableInterface $config){
static::$config = $config;
}
}