@@ -29,165 +29,165 @@ |
||
| 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->isWebDAVRequest()) { |
|
| 80 | - // Creating a folder |
|
| 81 | - if ($this->request->getMethod() === 'MKCOL') { |
|
| 82 | - $this->mimeType[$this->storage->getId()][$this->path] = 'httpd/unix-directory'; |
|
| 83 | - return $this->mimeType[$this->storage->getId()][$this->path]; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - if ($this->request->getMethod() === 'PUT' || $this->request->getMethod() === 'MOVE') { |
|
| 87 | - if ($this->request->getMethod() === 'MOVE') { |
|
| 88 | - $this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($this->path); |
|
| 89 | - } else { |
|
| 90 | - $path = $this->request->getPathInfo(); |
|
| 91 | - $this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($path); |
|
| 92 | - } |
|
| 93 | - return $this->mimeType[$this->storage->getId()][$this->path]; |
|
| 94 | - } |
|
| 95 | - } else if ($this->isPublicWebDAVRequest()) { |
|
| 96 | - if ($this->request->getMethod() === 'PUT') { |
|
| 97 | - $path = $this->request->getPathInfo(); |
|
| 98 | - if (strpos($path, '/webdav/') === 0) { |
|
| 99 | - $path = substr($path, strlen('/webdav')); |
|
| 100 | - } |
|
| 101 | - $path = $this->path . $path; |
|
| 102 | - $this->mimeType[$this->storage->getId()][$path] = $this->mimeTypeDetector->detectPath($path); |
|
| 103 | - return $this->mimeType[$this->storage->getId()][$path]; |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - if (in_array($this->request->getMethod(), ['POST', 'PUT'])) { |
|
| 108 | - $files = $this->request->getUploadedFile('files'); |
|
| 109 | - if (isset($files['type'][0])) { |
|
| 110 | - $mimeType = $files['type'][0]; |
|
| 111 | - if ($this->mimeType === 'application/octet-stream') { |
|
| 112 | - // Maybe not... |
|
| 113 | - $mimeTypeTest = $this->mimeTypeDetector->detectPath($files['name'][0]); |
|
| 114 | - if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { |
|
| 115 | - $mimeType = $mimeTypeTest; |
|
| 116 | - } else { |
|
| 117 | - $mimeTypeTest = $this->mimeTypeDetector->detect($files['tmp_name'][0]); |
|
| 118 | - if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { |
|
| 119 | - $mimeType = $mimeTypeTest; |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - $this->mimeType[$this->storage->getId()][$this->path] = $mimeType; |
|
| 124 | - return $mimeType; |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - $this->mimeType[$this->storage->getId()][$this->path] = $this->storage->getMimeType($this->path); |
|
| 129 | - if ($this->mimeType[$this->storage->getId()][$this->path] === 'application/octet-stream') { |
|
| 130 | - $this->mimeType[$this->storage->getId()][$this->path] = $this->detectMimetypeFromPath(); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - return $this->mimeType[$this->storage->getId()][$this->path]; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * @return string |
|
| 138 | - */ |
|
| 139 | - protected function detectMimetypeFromPath() { |
|
| 140 | - $mimeType = $this->mimeTypeDetector->detectPath($this->path); |
|
| 141 | - if ($mimeType !== 'application/octet-stream' && $mimeType !== false) { |
|
| 142 | - return $mimeType; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - if ($this->storage->instanceOfStorage('\OC\Files\Storage\Local') |
|
| 146 | - || $this->storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
| 147 | - || $this->storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')) { |
|
| 148 | - $localFile = $this->storage->getLocalFile($this->path); |
|
| 149 | - if ($localFile !== false) { |
|
| 150 | - $mimeType = $this->mimeTypeDetector->detect($localFile); |
|
| 151 | - if ($mimeType !== false) { |
|
| 152 | - return $mimeType; |
|
| 153 | - } |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - return 'application/octet-stream'; |
|
| 157 | - } else { |
|
| 158 | - $handle = $this->storage->fopen($this->path, 'r'); |
|
| 159 | - $data = fread($handle, 8024); |
|
| 160 | - fclose($handle); |
|
| 161 | - $mimeType = $this->mimeTypeDetector->detectString($data); |
|
| 162 | - if ($mimeType !== false) { |
|
| 163 | - return $mimeType; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - return 'application/octet-stream'; |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @return bool |
|
| 172 | - */ |
|
| 173 | - protected function isWebDAVRequest() { |
|
| 174 | - return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && ( |
|
| 175 | - $this->request->getPathInfo() === '/webdav' || |
|
| 176 | - strpos($this->request->getPathInfo(), '/webdav/') === 0 || |
|
| 177 | - $this->request->getPathInfo() === '/dav/files' || |
|
| 178 | - strpos($this->request->getPathInfo(), '/dav/files/') === 0 || |
|
| 179 | - $this->request->getPathInfo() === '/dav/uploads' || |
|
| 180 | - strpos($this->request->getPathInfo(), '/dav/uploads/') === 0 |
|
| 181 | - ); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * @return bool |
|
| 186 | - */ |
|
| 187 | - protected function isPublicWebDAVRequest() { |
|
| 188 | - return substr($this->request->getScriptName(), 0 - strlen('/public.php')) === '/public.php' && ( |
|
| 189 | - $this->request->getPathInfo() === '/webdav' || |
|
| 190 | - strpos($this->request->getPathInfo(), '/webdav/') === 0 |
|
| 191 | - ); |
|
| 192 | - } |
|
| 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->isWebDAVRequest()) { |
|
| 80 | + // Creating a folder |
|
| 81 | + if ($this->request->getMethod() === 'MKCOL') { |
|
| 82 | + $this->mimeType[$this->storage->getId()][$this->path] = 'httpd/unix-directory'; |
|
| 83 | + return $this->mimeType[$this->storage->getId()][$this->path]; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + if ($this->request->getMethod() === 'PUT' || $this->request->getMethod() === 'MOVE') { |
|
| 87 | + if ($this->request->getMethod() === 'MOVE') { |
|
| 88 | + $this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($this->path); |
|
| 89 | + } else { |
|
| 90 | + $path = $this->request->getPathInfo(); |
|
| 91 | + $this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($path); |
|
| 92 | + } |
|
| 93 | + return $this->mimeType[$this->storage->getId()][$this->path]; |
|
| 94 | + } |
|
| 95 | + } else if ($this->isPublicWebDAVRequest()) { |
|
| 96 | + if ($this->request->getMethod() === 'PUT') { |
|
| 97 | + $path = $this->request->getPathInfo(); |
|
| 98 | + if (strpos($path, '/webdav/') === 0) { |
|
| 99 | + $path = substr($path, strlen('/webdav')); |
|
| 100 | + } |
|
| 101 | + $path = $this->path . $path; |
|
| 102 | + $this->mimeType[$this->storage->getId()][$path] = $this->mimeTypeDetector->detectPath($path); |
|
| 103 | + return $this->mimeType[$this->storage->getId()][$path]; |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + if (in_array($this->request->getMethod(), ['POST', 'PUT'])) { |
|
| 108 | + $files = $this->request->getUploadedFile('files'); |
|
| 109 | + if (isset($files['type'][0])) { |
|
| 110 | + $mimeType = $files['type'][0]; |
|
| 111 | + if ($this->mimeType === 'application/octet-stream') { |
|
| 112 | + // Maybe not... |
|
| 113 | + $mimeTypeTest = $this->mimeTypeDetector->detectPath($files['name'][0]); |
|
| 114 | + if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { |
|
| 115 | + $mimeType = $mimeTypeTest; |
|
| 116 | + } else { |
|
| 117 | + $mimeTypeTest = $this->mimeTypeDetector->detect($files['tmp_name'][0]); |
|
| 118 | + if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { |
|
| 119 | + $mimeType = $mimeTypeTest; |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + $this->mimeType[$this->storage->getId()][$this->path] = $mimeType; |
|
| 124 | + return $mimeType; |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + $this->mimeType[$this->storage->getId()][$this->path] = $this->storage->getMimeType($this->path); |
|
| 129 | + if ($this->mimeType[$this->storage->getId()][$this->path] === 'application/octet-stream') { |
|
| 130 | + $this->mimeType[$this->storage->getId()][$this->path] = $this->detectMimetypeFromPath(); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + return $this->mimeType[$this->storage->getId()][$this->path]; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * @return string |
|
| 138 | + */ |
|
| 139 | + protected function detectMimetypeFromPath() { |
|
| 140 | + $mimeType = $this->mimeTypeDetector->detectPath($this->path); |
|
| 141 | + if ($mimeType !== 'application/octet-stream' && $mimeType !== false) { |
|
| 142 | + return $mimeType; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + if ($this->storage->instanceOfStorage('\OC\Files\Storage\Local') |
|
| 146 | + || $this->storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
| 147 | + || $this->storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')) { |
|
| 148 | + $localFile = $this->storage->getLocalFile($this->path); |
|
| 149 | + if ($localFile !== false) { |
|
| 150 | + $mimeType = $this->mimeTypeDetector->detect($localFile); |
|
| 151 | + if ($mimeType !== false) { |
|
| 152 | + return $mimeType; |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + return 'application/octet-stream'; |
|
| 157 | + } else { |
|
| 158 | + $handle = $this->storage->fopen($this->path, 'r'); |
|
| 159 | + $data = fread($handle, 8024); |
|
| 160 | + fclose($handle); |
|
| 161 | + $mimeType = $this->mimeTypeDetector->detectString($data); |
|
| 162 | + if ($mimeType !== false) { |
|
| 163 | + return $mimeType; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + return 'application/octet-stream'; |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @return bool |
|
| 172 | + */ |
|
| 173 | + protected function isWebDAVRequest() { |
|
| 174 | + return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && ( |
|
| 175 | + $this->request->getPathInfo() === '/webdav' || |
|
| 176 | + strpos($this->request->getPathInfo(), '/webdav/') === 0 || |
|
| 177 | + $this->request->getPathInfo() === '/dav/files' || |
|
| 178 | + strpos($this->request->getPathInfo(), '/dav/files/') === 0 || |
|
| 179 | + $this->request->getPathInfo() === '/dav/uploads' || |
|
| 180 | + strpos($this->request->getPathInfo(), '/dav/uploads/') === 0 |
|
| 181 | + ); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * @return bool |
|
| 186 | + */ |
|
| 187 | + protected function isPublicWebDAVRequest() { |
|
| 188 | + return substr($this->request->getScriptName(), 0 - strlen('/public.php')) === '/public.php' && ( |
|
| 189 | + $this->request->getPathInfo() === '/webdav' || |
|
| 190 | + strpos($this->request->getPathInfo(), '/webdav/') === 0 |
|
| 191 | + ); |
|
| 192 | + } |
|
| 193 | 193 | } |