-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathFileSystemStorage.php
More file actions
207 lines (148 loc) · 5.17 KB
/
Copy pathFileSystemStorage.php
File metadata and controls
207 lines (148 loc) · 5.17 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace Iphp\FileStoreBundle\FileStorage;
use Iphp\FileStoreBundle\FileStorage\FileStorageInterface;
use Iphp\FileStoreBundle\Mapping\PropertyMapping;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
/**
* FileSystemStorage.
*
* @author Vitiko <vitiko@mail.ru>
*/
class FileSystemStorage implements FileStorageInterface
{
protected $webDir;
protected $sameFileChecker;
/**
* Constructs a new instance of FileSystemStorage.
*
* @param
*/
public function __construct($webDir = null)
{
$this->webDir = $webDir;
// @codeCoverageIgnoreStart
$this->sameFileChecker = function (File $file, $fullFileName)
{
return $file->getRealPath() == realpath($fullFileName);
};
// @codeCoverageIgnoreEnd
}
public function setWebDir($webDir )
{
$this->webDir = $webDir;
}
public function getWebDir()
{
return $this->webDir;
}
public function setSameFileChecker (\Closure $checker)
{
$this->sameFileChecker = $checker;
}
protected function getOriginalName(File $file)
{
return $file instanceof UploadedFile ?
$file->getClientOriginalName() : $file->getFilename();
}
protected function getMimeType(File $file)
{
return $file instanceof UploadedFile ?
$file->getClientMimeType() : $file->getMimeType();
}
public function isSameFile (File $file, $fullFileName)
{
return call_user_func(
$this->sameFileChecker,
$file,
$fullFileName);
}
protected function copyFile($source, $directory, $name)
{
$this->checkDirectory($directory);
$target = $directory . DIRECTORY_SEPARATOR . basename($name);
if (!@copy($source, $target)) {
$error = error_get_last();
throw new FileException(sprintf('Could not copy the file "%s" to "%s" (%s)', $source, $target, strip_tags($error['message'])));
}
@chmod($target, 0666 & ~umask());
return new File($target);
}
protected function checkDirectory ($directory)
{
if (!is_dir($directory)) {
if (false === @mkdir($directory, 0777, true)) {
// @codeCoverageIgnoreStart
throw new FileException(sprintf('Unable to create the "%s" directory', $directory));
// @codeCoverageIgnoreEnd
}
} elseif (!is_writable($directory)) {
// @codeCoverageIgnoreStart
throw new FileException(sprintf('Unable to write in the "%s" directory', $directory));
// @codeCoverageIgnoreEnd
}
return true;
}
/**
* {@inheritDoc}
* File may be \Symfony\Component\HttpFoundation\File\File or \Symfony\Component\HttpFoundation\File\UploadedFile
*/
public function upload(PropertyMapping $mapping, File $file)
{
$originalName = $this->getOriginalName($file);
$mimeType = $this->getMimeType($file);
//transform filename and directory name if namer exists in mapping definition
list ($fileName, $webPath) = $mapping->prepareFileName($originalName, $this);
$fullFileName = $mapping->resolveFileName($fileName);
//check if file already placed in needed position
if (!$this->isSameFile($file, $fullFileName)) {
$fileInfo = pathinfo($fullFileName);
if ($file instanceof UploadedFile)
{
$this->checkDirectory($fileInfo['dirname']);
$file->move($fileInfo['dirname'], $fileInfo['basename']);
}
else $this->copyFile($file->getPathname(), $fileInfo['dirname'], $fileInfo['basename']);
}
$fileData = array(
'fileName' => $fileName,
'originalName' => $originalName,
'mimeType' => $mimeType,
'size' => filesize($fullFileName),
'path' => $webPath
);
if (!$fileData['path'])
$fileData['path'] = substr($fullFileName, strlen($this->webDir));
$ext = substr($originalName,strrpos ($originalName,'.')+1);
if ((in_array($fileData['mimeType'], array('image/png', 'image/jpeg', 'image/pjpeg')) ||
in_array ($ext,array ('jpeg','jpg','png')))
&& function_exists('getimagesize')
) {
list($width, $height, $type) = @getimagesize($fullFileName);
$fileData = array_merge($fileData, array(
'width' => $width, 'height' => $height
));
}
return $fileData;
}
/**
* {@inheritDoc}
*/
public function removeFile($fullFileName)
{
if ($fullFileName && file_exists($fullFileName)) {
@unlink($fullFileName);
return !file_exists($fullFileName);
}
return null;
}
/**
* {@inheritDoc}
*/
public function fileExists($fullFileName)
{
return file_exists($fullFileName);
}
}