@@ -29,148 +29,148 @@ |
||
| 29 | 29 | use OCP\WorkflowEngine\IFileCheck; |
| 30 | 30 | |
| 31 | 31 | class FileMimeType extends AbstractStringCheck implements IFileCheck { |
| 32 | - use TFileCheck { |
|
| 33 | - setFileInfo as _setFileInfo; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** @var array */ |
|
| 37 | - protected $mimeType; |
|
| 38 | - |
|
| 39 | - /** @var IRequest */ |
|
| 40 | - protected $request; |
|
| 41 | - |
|
| 42 | - /** @var IMimeTypeDetector */ |
|
| 43 | - protected $mimeTypeDetector; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param IL10N $l |
|
| 47 | - * @param IRequest $request |
|
| 48 | - * @param IMimeTypeDetector $mimeTypeDetector |
|
| 49 | - */ |
|
| 50 | - public function __construct(IL10N $l, IRequest $request, IMimeTypeDetector $mimeTypeDetector) { |
|
| 51 | - parent::__construct($l); |
|
| 52 | - $this->request = $request; |
|
| 53 | - $this->mimeTypeDetector = $mimeTypeDetector; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @param IStorage $storage |
|
| 58 | - * @param string $path |
|
| 59 | - * @param bool $isDir |
|
| 60 | - */ |
|
| 61 | - public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void { |
|
| 62 | - $this->_setFileInfo($storage, $path, $isDir); |
|
| 63 | - if (!isset($this->mimeType[$this->storage->getId()][$this->path]) |
|
| 64 | - || $this->mimeType[$this->storage->getId()][$this->path] === '') { |
|
| 65 | - if ($isDir) { |
|
| 66 | - $this->mimeType[$this->storage->getId()][$this->path] = 'httpd/unix-directory'; |
|
| 67 | - } else { |
|
| 68 | - $this->mimeType[$this->storage->getId()][$this->path] = null; |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * The mimetype is only cached if the file exists. Otherwise files access |
|
| 75 | - * control will cache "application/octet-stream" for all the target node on: |
|
| 76 | - * rename, move, copy and all other methods which create a new item |
|
| 77 | - * |
|
| 78 | - * To check this: |
|
| 79 | - * 1. Add an automated tagging rule which tags on mimetype NOT "httpd/unix-directory" |
|
| 80 | - * 2. Add an access control rule which checks for any mimetype |
|
| 81 | - * 3. Create a folder and rename it, the folder should not be tagged, but it is |
|
| 82 | - * |
|
| 83 | - * @param string $storageId |
|
| 84 | - * @param string|null $path |
|
| 85 | - * @param string $mimeType |
|
| 86 | - * @return string |
|
| 87 | - */ |
|
| 88 | - protected function cacheAndReturnMimeType(string $storageId, ?string $path, string $mimeType): string { |
|
| 89 | - if ($path !== null && $this->storage->file_exists($path)) { |
|
| 90 | - $this->mimeType[$storageId][$path] = $mimeType; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - return $mimeType; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Make sure that even though the content based check returns an application/octet-stream can still be checked based on mimetypemappings of their extension |
|
| 98 | - * |
|
| 99 | - * @param string $operator |
|
| 100 | - * @param string $value |
|
| 101 | - * @return bool |
|
| 102 | - */ |
|
| 103 | - public function executeCheck($operator, $value) { |
|
| 104 | - $actualValue = $this->getActualValue(); |
|
| 105 | - $plainMimetypeResult = $this->executeStringCheck($operator, $value, $actualValue); |
|
| 106 | - if ($actualValue === 'httpd/unix-directory') { |
|
| 107 | - return $plainMimetypeResult; |
|
| 108 | - } |
|
| 109 | - $detectMimetypeBasedOnFilenameResult = $this->executeStringCheck($operator, $value, $this->mimeTypeDetector->detectPath($this->path)); |
|
| 110 | - return $plainMimetypeResult || $detectMimetypeBasedOnFilenameResult; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @return string |
|
| 115 | - */ |
|
| 116 | - protected function getActualValue() { |
|
| 117 | - if ($this->mimeType[$this->storage->getId()][$this->path] !== null) { |
|
| 118 | - return $this->mimeType[$this->storage->getId()][$this->path]; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - if ($this->storage->is_dir($this->path)) { |
|
| 122 | - return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, 'httpd/unix-directory'); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - if ($this->storage->file_exists($this->path)) { |
|
| 126 | - $path = $this->storage->getLocalFile($this->path); |
|
| 127 | - $mimeType = $this->mimeTypeDetector->detectContent($path); |
|
| 128 | - return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, $mimeType); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - if ($this->isWebDAVRequest() || $this->isPublicWebDAVRequest()) { |
|
| 132 | - // Creating a folder |
|
| 133 | - if ($this->request->getMethod() === 'MKCOL') { |
|
| 134 | - return 'httpd/unix-directory'; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - // We do not cache this, as the file did not exist yet. |
|
| 139 | - // In case it does in the future, we will check with detectContent() |
|
| 140 | - // again to get the real mimetype of the content, rather than |
|
| 141 | - // guessing it from the path. |
|
| 142 | - return $this->mimeTypeDetector->detectPath($this->path); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * @return bool |
|
| 147 | - */ |
|
| 148 | - protected function isWebDAVRequest() { |
|
| 149 | - return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && ( |
|
| 150 | - $this->request->getPathInfo() === '/webdav' || |
|
| 151 | - strpos($this->request->getPathInfo(), '/webdav/') === 0 || |
|
| 152 | - $this->request->getPathInfo() === '/dav/files' || |
|
| 153 | - strpos($this->request->getPathInfo(), '/dav/files/') === 0 || |
|
| 154 | - $this->request->getPathInfo() === '/dav/uploads' || |
|
| 155 | - strpos($this->request->getPathInfo(), '/dav/uploads/') === 0 |
|
| 156 | - ); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @return bool |
|
| 161 | - */ |
|
| 162 | - protected function isPublicWebDAVRequest() { |
|
| 163 | - return substr($this->request->getScriptName(), 0 - strlen('/public.php')) === '/public.php' && ( |
|
| 164 | - $this->request->getPathInfo() === '/webdav' || |
|
| 165 | - strpos($this->request->getPathInfo(), '/webdav/') === 0 |
|
| 166 | - ); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - public function supportedEntities(): array { |
|
| 170 | - return [ File::class ]; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - public function isAvailableForScope(int $scope): bool { |
|
| 174 | - return true; |
|
| 175 | - } |
|
| 32 | + use TFileCheck { |
|
| 33 | + setFileInfo as _setFileInfo; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** @var array */ |
|
| 37 | + protected $mimeType; |
|
| 38 | + |
|
| 39 | + /** @var IRequest */ |
|
| 40 | + protected $request; |
|
| 41 | + |
|
| 42 | + /** @var IMimeTypeDetector */ |
|
| 43 | + protected $mimeTypeDetector; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param IL10N $l |
|
| 47 | + * @param IRequest $request |
|
| 48 | + * @param IMimeTypeDetector $mimeTypeDetector |
|
| 49 | + */ |
|
| 50 | + public function __construct(IL10N $l, IRequest $request, IMimeTypeDetector $mimeTypeDetector) { |
|
| 51 | + parent::__construct($l); |
|
| 52 | + $this->request = $request; |
|
| 53 | + $this->mimeTypeDetector = $mimeTypeDetector; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @param IStorage $storage |
|
| 58 | + * @param string $path |
|
| 59 | + * @param bool $isDir |
|
| 60 | + */ |
|
| 61 | + public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void { |
|
| 62 | + $this->_setFileInfo($storage, $path, $isDir); |
|
| 63 | + if (!isset($this->mimeType[$this->storage->getId()][$this->path]) |
|
| 64 | + || $this->mimeType[$this->storage->getId()][$this->path] === '') { |
|
| 65 | + if ($isDir) { |
|
| 66 | + $this->mimeType[$this->storage->getId()][$this->path] = 'httpd/unix-directory'; |
|
| 67 | + } else { |
|
| 68 | + $this->mimeType[$this->storage->getId()][$this->path] = null; |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * The mimetype is only cached if the file exists. Otherwise files access |
|
| 75 | + * control will cache "application/octet-stream" for all the target node on: |
|
| 76 | + * rename, move, copy and all other methods which create a new item |
|
| 77 | + * |
|
| 78 | + * To check this: |
|
| 79 | + * 1. Add an automated tagging rule which tags on mimetype NOT "httpd/unix-directory" |
|
| 80 | + * 2. Add an access control rule which checks for any mimetype |
|
| 81 | + * 3. Create a folder and rename it, the folder should not be tagged, but it is |
|
| 82 | + * |
|
| 83 | + * @param string $storageId |
|
| 84 | + * @param string|null $path |
|
| 85 | + * @param string $mimeType |
|
| 86 | + * @return string |
|
| 87 | + */ |
|
| 88 | + protected function cacheAndReturnMimeType(string $storageId, ?string $path, string $mimeType): string { |
|
| 89 | + if ($path !== null && $this->storage->file_exists($path)) { |
|
| 90 | + $this->mimeType[$storageId][$path] = $mimeType; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + return $mimeType; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Make sure that even though the content based check returns an application/octet-stream can still be checked based on mimetypemappings of their extension |
|
| 98 | + * |
|
| 99 | + * @param string $operator |
|
| 100 | + * @param string $value |
|
| 101 | + * @return bool |
|
| 102 | + */ |
|
| 103 | + public function executeCheck($operator, $value) { |
|
| 104 | + $actualValue = $this->getActualValue(); |
|
| 105 | + $plainMimetypeResult = $this->executeStringCheck($operator, $value, $actualValue); |
|
| 106 | + if ($actualValue === 'httpd/unix-directory') { |
|
| 107 | + return $plainMimetypeResult; |
|
| 108 | + } |
|
| 109 | + $detectMimetypeBasedOnFilenameResult = $this->executeStringCheck($operator, $value, $this->mimeTypeDetector->detectPath($this->path)); |
|
| 110 | + return $plainMimetypeResult || $detectMimetypeBasedOnFilenameResult; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @return string |
|
| 115 | + */ |
|
| 116 | + protected function getActualValue() { |
|
| 117 | + if ($this->mimeType[$this->storage->getId()][$this->path] !== null) { |
|
| 118 | + return $this->mimeType[$this->storage->getId()][$this->path]; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + if ($this->storage->is_dir($this->path)) { |
|
| 122 | + return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, 'httpd/unix-directory'); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + if ($this->storage->file_exists($this->path)) { |
|
| 126 | + $path = $this->storage->getLocalFile($this->path); |
|
| 127 | + $mimeType = $this->mimeTypeDetector->detectContent($path); |
|
| 128 | + return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, $mimeType); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + if ($this->isWebDAVRequest() || $this->isPublicWebDAVRequest()) { |
|
| 132 | + // Creating a folder |
|
| 133 | + if ($this->request->getMethod() === 'MKCOL') { |
|
| 134 | + return 'httpd/unix-directory'; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + // We do not cache this, as the file did not exist yet. |
|
| 139 | + // In case it does in the future, we will check with detectContent() |
|
| 140 | + // again to get the real mimetype of the content, rather than |
|
| 141 | + // guessing it from the path. |
|
| 142 | + return $this->mimeTypeDetector->detectPath($this->path); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * @return bool |
|
| 147 | + */ |
|
| 148 | + protected function isWebDAVRequest() { |
|
| 149 | + return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && ( |
|
| 150 | + $this->request->getPathInfo() === '/webdav' || |
|
| 151 | + strpos($this->request->getPathInfo(), '/webdav/') === 0 || |
|
| 152 | + $this->request->getPathInfo() === '/dav/files' || |
|
| 153 | + strpos($this->request->getPathInfo(), '/dav/files/') === 0 || |
|
| 154 | + $this->request->getPathInfo() === '/dav/uploads' || |
|
| 155 | + strpos($this->request->getPathInfo(), '/dav/uploads/') === 0 |
|
| 156 | + ); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @return bool |
|
| 161 | + */ |
|
| 162 | + protected function isPublicWebDAVRequest() { |
|
| 163 | + return substr($this->request->getScriptName(), 0 - strlen('/public.php')) === '/public.php' && ( |
|
| 164 | + $this->request->getPathInfo() === '/webdav' || |
|
| 165 | + strpos($this->request->getPathInfo(), '/webdav/') === 0 |
|
| 166 | + ); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + public function supportedEntities(): array { |
|
| 170 | + return [ File::class ]; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + public function isAvailableForScope(int $scope): bool { |
|
| 174 | + return true; |
|
| 175 | + } |
|
| 176 | 176 | } |