@@ -29,170 +29,170 @@ |
||
29 | 29 | |
30 | 30 | class FileMimeType extends AbstractStringCheck { |
31 | 31 | |
32 | - /** @var array */ |
|
33 | - protected $mimeType; |
|
34 | - |
|
35 | - /** @var IRequest */ |
|
36 | - protected $request; |
|
37 | - |
|
38 | - /** @var IMimeTypeDetector */ |
|
39 | - protected $mimeTypeDetector; |
|
40 | - |
|
41 | - /** @var IStorage */ |
|
42 | - protected $storage; |
|
43 | - |
|
44 | - /** @var string */ |
|
45 | - protected $path; |
|
46 | - |
|
47 | - /** |
|
48 | - * @param IL10N $l |
|
49 | - * @param IRequest $request |
|
50 | - * @param IMimeTypeDetector $mimeTypeDetector |
|
51 | - */ |
|
52 | - public function __construct(IL10N $l, IRequest $request, IMimeTypeDetector $mimeTypeDetector) { |
|
53 | - parent::__construct($l); |
|
54 | - $this->request = $request; |
|
55 | - $this->mimeTypeDetector = $mimeTypeDetector; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @param IStorage $storage |
|
60 | - * @param string $path |
|
61 | - */ |
|
62 | - public function setFileInfo(IStorage $storage, $path) { |
|
63 | - $this->storage = $storage; |
|
64 | - $this->path = $path; |
|
65 | - if (!isset($this->mimeType[$this->storage->getId()][$this->path]) |
|
66 | - || $this->mimeType[$this->storage->getId()][$this->path] === '') { |
|
67 | - $this->mimeType[$this->storage->getId()][$this->path] = null; |
|
68 | - } |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @return string |
|
73 | - */ |
|
74 | - protected function getActualValue() { |
|
75 | - if ($this->mimeType[$this->storage->getId()][$this->path] !== null) { |
|
76 | - return $this->mimeType[$this->storage->getId()][$this->path]; |
|
77 | - } |
|
78 | - |
|
79 | - if ($this->storage->is_dir($this->path)) { |
|
80 | - $this->mimeType[$this->storage->getId()][$this->path] = 'httpd/unix-directory'; |
|
81 | - return $this->mimeType[$this->storage->getId()][$this->path]; |
|
82 | - } |
|
83 | - |
|
84 | - if ($this->isWebDAVRequest()) { |
|
85 | - // Creating a folder |
|
86 | - if ($this->request->getMethod() === 'MKCOL') { |
|
87 | - $this->mimeType[$this->storage->getId()][$this->path] = 'httpd/unix-directory'; |
|
88 | - return $this->mimeType[$this->storage->getId()][$this->path]; |
|
89 | - } |
|
90 | - |
|
91 | - if ($this->request->getMethod() === 'PUT' || $this->request->getMethod() === 'MOVE') { |
|
92 | - if ($this->request->getMethod() === 'MOVE') { |
|
93 | - $this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($this->path); |
|
94 | - } else { |
|
95 | - $path = $this->request->getPathInfo(); |
|
96 | - $this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($path); |
|
97 | - } |
|
98 | - return $this->mimeType[$this->storage->getId()][$this->path]; |
|
99 | - } |
|
100 | - } else if ($this->isPublicWebDAVRequest()) { |
|
101 | - if ($this->request->getMethod() === 'PUT') { |
|
102 | - $path = $this->request->getPathInfo(); |
|
103 | - if (strpos($path, '/webdav/') === 0) { |
|
104 | - $path = substr($path, strlen('/webdav')); |
|
105 | - } |
|
106 | - $path = $this->path . $path; |
|
107 | - $this->mimeType[$this->storage->getId()][$path] = $this->mimeTypeDetector->detectPath($path); |
|
108 | - return $this->mimeType[$this->storage->getId()][$path]; |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - if (in_array($this->request->getMethod(), ['POST', 'PUT'])) { |
|
113 | - $files = $this->request->getUploadedFile('files'); |
|
114 | - if (isset($files['type'][0])) { |
|
115 | - $mimeType = $files['type'][0]; |
|
116 | - if ($this->mimeType === 'application/octet-stream') { |
|
117 | - // Maybe not... |
|
118 | - $mimeTypeTest = $this->mimeTypeDetector->detectPath($files['name'][0]); |
|
119 | - if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { |
|
120 | - $mimeType = $mimeTypeTest; |
|
121 | - } else { |
|
122 | - $mimeTypeTest = $this->mimeTypeDetector->detect($files['tmp_name'][0]); |
|
123 | - if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { |
|
124 | - $mimeType = $mimeTypeTest; |
|
125 | - } |
|
126 | - } |
|
127 | - } |
|
128 | - $this->mimeType[$this->storage->getId()][$this->path] = $mimeType; |
|
129 | - return $mimeType; |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - $this->mimeType[$this->storage->getId()][$this->path] = $this->storage->getMimeType($this->path); |
|
134 | - if ($this->mimeType[$this->storage->getId()][$this->path] === 'application/octet-stream') { |
|
135 | - $this->mimeType[$this->storage->getId()][$this->path] = $this->detectMimetypeFromPath(); |
|
136 | - } |
|
137 | - |
|
138 | - return $this->mimeType[$this->storage->getId()][$this->path]; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @return string |
|
143 | - */ |
|
144 | - protected function detectMimetypeFromPath() { |
|
145 | - $mimeType = $this->mimeTypeDetector->detectPath($this->path); |
|
146 | - if ($mimeType !== 'application/octet-stream' && $mimeType !== false) { |
|
147 | - return $mimeType; |
|
148 | - } |
|
149 | - |
|
150 | - if ($this->storage->instanceOfStorage('\OC\Files\Storage\Local') |
|
151 | - || $this->storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
152 | - || $this->storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')) { |
|
153 | - $localFile = $this->storage->getLocalFile($this->path); |
|
154 | - if ($localFile !== false) { |
|
155 | - $mimeType = $this->mimeTypeDetector->detect($localFile); |
|
156 | - if ($mimeType !== false) { |
|
157 | - return $mimeType; |
|
158 | - } |
|
159 | - } |
|
160 | - |
|
161 | - return 'application/octet-stream'; |
|
162 | - } else { |
|
163 | - $handle = $this->storage->fopen($this->path, 'r'); |
|
164 | - $data = fread($handle, 8024); |
|
165 | - fclose($handle); |
|
166 | - $mimeType = $this->mimeTypeDetector->detectString($data); |
|
167 | - if ($mimeType !== false) { |
|
168 | - return $mimeType; |
|
169 | - } |
|
170 | - |
|
171 | - return 'application/octet-stream'; |
|
172 | - } |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * @return bool |
|
177 | - */ |
|
178 | - protected function isWebDAVRequest() { |
|
179 | - return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && ( |
|
180 | - $this->request->getPathInfo() === '/webdav' || |
|
181 | - strpos($this->request->getPathInfo(), '/webdav/') === 0 || |
|
182 | - $this->request->getPathInfo() === '/dav/files' || |
|
183 | - strpos($this->request->getPathInfo(), '/dav/files/') === 0 || |
|
184 | - $this->request->getPathInfo() === '/dav/uploads' || |
|
185 | - strpos($this->request->getPathInfo(), '/dav/uploads/') === 0 |
|
186 | - ); |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * @return bool |
|
191 | - */ |
|
192 | - protected function isPublicWebDAVRequest() { |
|
193 | - return substr($this->request->getScriptName(), 0 - strlen('/public.php')) === '/public.php' && ( |
|
194 | - $this->request->getPathInfo() === '/webdav' || |
|
195 | - strpos($this->request->getPathInfo(), '/webdav/') === 0 |
|
196 | - ); |
|
197 | - } |
|
32 | + /** @var array */ |
|
33 | + protected $mimeType; |
|
34 | + |
|
35 | + /** @var IRequest */ |
|
36 | + protected $request; |
|
37 | + |
|
38 | + /** @var IMimeTypeDetector */ |
|
39 | + protected $mimeTypeDetector; |
|
40 | + |
|
41 | + /** @var IStorage */ |
|
42 | + protected $storage; |
|
43 | + |
|
44 | + /** @var string */ |
|
45 | + protected $path; |
|
46 | + |
|
47 | + /** |
|
48 | + * @param IL10N $l |
|
49 | + * @param IRequest $request |
|
50 | + * @param IMimeTypeDetector $mimeTypeDetector |
|
51 | + */ |
|
52 | + public function __construct(IL10N $l, IRequest $request, IMimeTypeDetector $mimeTypeDetector) { |
|
53 | + parent::__construct($l); |
|
54 | + $this->request = $request; |
|
55 | + $this->mimeTypeDetector = $mimeTypeDetector; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @param IStorage $storage |
|
60 | + * @param string $path |
|
61 | + */ |
|
62 | + public function setFileInfo(IStorage $storage, $path) { |
|
63 | + $this->storage = $storage; |
|
64 | + $this->path = $path; |
|
65 | + if (!isset($this->mimeType[$this->storage->getId()][$this->path]) |
|
66 | + || $this->mimeType[$this->storage->getId()][$this->path] === '') { |
|
67 | + $this->mimeType[$this->storage->getId()][$this->path] = null; |
|
68 | + } |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @return string |
|
73 | + */ |
|
74 | + protected function getActualValue() { |
|
75 | + if ($this->mimeType[$this->storage->getId()][$this->path] !== null) { |
|
76 | + return $this->mimeType[$this->storage->getId()][$this->path]; |
|
77 | + } |
|
78 | + |
|
79 | + if ($this->storage->is_dir($this->path)) { |
|
80 | + $this->mimeType[$this->storage->getId()][$this->path] = 'httpd/unix-directory'; |
|
81 | + return $this->mimeType[$this->storage->getId()][$this->path]; |
|
82 | + } |
|
83 | + |
|
84 | + if ($this->isWebDAVRequest()) { |
|
85 | + // Creating a folder |
|
86 | + if ($this->request->getMethod() === 'MKCOL') { |
|
87 | + $this->mimeType[$this->storage->getId()][$this->path] = 'httpd/unix-directory'; |
|
88 | + return $this->mimeType[$this->storage->getId()][$this->path]; |
|
89 | + } |
|
90 | + |
|
91 | + if ($this->request->getMethod() === 'PUT' || $this->request->getMethod() === 'MOVE') { |
|
92 | + if ($this->request->getMethod() === 'MOVE') { |
|
93 | + $this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($this->path); |
|
94 | + } else { |
|
95 | + $path = $this->request->getPathInfo(); |
|
96 | + $this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($path); |
|
97 | + } |
|
98 | + return $this->mimeType[$this->storage->getId()][$this->path]; |
|
99 | + } |
|
100 | + } else if ($this->isPublicWebDAVRequest()) { |
|
101 | + if ($this->request->getMethod() === 'PUT') { |
|
102 | + $path = $this->request->getPathInfo(); |
|
103 | + if (strpos($path, '/webdav/') === 0) { |
|
104 | + $path = substr($path, strlen('/webdav')); |
|
105 | + } |
|
106 | + $path = $this->path . $path; |
|
107 | + $this->mimeType[$this->storage->getId()][$path] = $this->mimeTypeDetector->detectPath($path); |
|
108 | + return $this->mimeType[$this->storage->getId()][$path]; |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + if (in_array($this->request->getMethod(), ['POST', 'PUT'])) { |
|
113 | + $files = $this->request->getUploadedFile('files'); |
|
114 | + if (isset($files['type'][0])) { |
|
115 | + $mimeType = $files['type'][0]; |
|
116 | + if ($this->mimeType === 'application/octet-stream') { |
|
117 | + // Maybe not... |
|
118 | + $mimeTypeTest = $this->mimeTypeDetector->detectPath($files['name'][0]); |
|
119 | + if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { |
|
120 | + $mimeType = $mimeTypeTest; |
|
121 | + } else { |
|
122 | + $mimeTypeTest = $this->mimeTypeDetector->detect($files['tmp_name'][0]); |
|
123 | + if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { |
|
124 | + $mimeType = $mimeTypeTest; |
|
125 | + } |
|
126 | + } |
|
127 | + } |
|
128 | + $this->mimeType[$this->storage->getId()][$this->path] = $mimeType; |
|
129 | + return $mimeType; |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + $this->mimeType[$this->storage->getId()][$this->path] = $this->storage->getMimeType($this->path); |
|
134 | + if ($this->mimeType[$this->storage->getId()][$this->path] === 'application/octet-stream') { |
|
135 | + $this->mimeType[$this->storage->getId()][$this->path] = $this->detectMimetypeFromPath(); |
|
136 | + } |
|
137 | + |
|
138 | + return $this->mimeType[$this->storage->getId()][$this->path]; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @return string |
|
143 | + */ |
|
144 | + protected function detectMimetypeFromPath() { |
|
145 | + $mimeType = $this->mimeTypeDetector->detectPath($this->path); |
|
146 | + if ($mimeType !== 'application/octet-stream' && $mimeType !== false) { |
|
147 | + return $mimeType; |
|
148 | + } |
|
149 | + |
|
150 | + if ($this->storage->instanceOfStorage('\OC\Files\Storage\Local') |
|
151 | + || $this->storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
152 | + || $this->storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')) { |
|
153 | + $localFile = $this->storage->getLocalFile($this->path); |
|
154 | + if ($localFile !== false) { |
|
155 | + $mimeType = $this->mimeTypeDetector->detect($localFile); |
|
156 | + if ($mimeType !== false) { |
|
157 | + return $mimeType; |
|
158 | + } |
|
159 | + } |
|
160 | + |
|
161 | + return 'application/octet-stream'; |
|
162 | + } else { |
|
163 | + $handle = $this->storage->fopen($this->path, 'r'); |
|
164 | + $data = fread($handle, 8024); |
|
165 | + fclose($handle); |
|
166 | + $mimeType = $this->mimeTypeDetector->detectString($data); |
|
167 | + if ($mimeType !== false) { |
|
168 | + return $mimeType; |
|
169 | + } |
|
170 | + |
|
171 | + return 'application/octet-stream'; |
|
172 | + } |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * @return bool |
|
177 | + */ |
|
178 | + protected function isWebDAVRequest() { |
|
179 | + return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && ( |
|
180 | + $this->request->getPathInfo() === '/webdav' || |
|
181 | + strpos($this->request->getPathInfo(), '/webdav/') === 0 || |
|
182 | + $this->request->getPathInfo() === '/dav/files' || |
|
183 | + strpos($this->request->getPathInfo(), '/dav/files/') === 0 || |
|
184 | + $this->request->getPathInfo() === '/dav/uploads' || |
|
185 | + strpos($this->request->getPathInfo(), '/dav/uploads/') === 0 |
|
186 | + ); |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * @return bool |
|
191 | + */ |
|
192 | + protected function isPublicWebDAVRequest() { |
|
193 | + return substr($this->request->getScriptName(), 0 - strlen('/public.php')) === '/public.php' && ( |
|
194 | + $this->request->getPathInfo() === '/webdav' || |
|
195 | + strpos($this->request->getPathInfo(), '/webdav/') === 0 |
|
196 | + ); |
|
197 | + } |
|
198 | 198 | } |