diff options
author | Roeland Jago Douma <rullzer@users.noreply.github.com> | 2019-12-12 11:19:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-12 11:19:55 +0100 |
commit | c6e51924c8615166c2533f83ecc6399d9cc56cbd (patch) | |
tree | 4628e29811b1b3b5831df2b18cafedbc2e7ee86b /apps/workflowengine | |
parent | 4f60609f83fde059e6f831c1b17b481e6604ce24 (diff) | |
parent | 4356c91ffd16f54a7bc67b7c62ef4f1110b29e9f (diff) | |
download | nextcloud-server-c6e51924c8615166c2533f83ecc6399d9cc56cbd.tar.gz nextcloud-server-c6e51924c8615166c2533f83ecc6399d9cc56cbd.zip |
Merge pull request #18236 from nextcloud/bugfix/noid/always-detect-mimetype-by-content-in-workflows
Allow to detect mimetype by content
Diffstat (limited to 'apps/workflowengine')
-rw-r--r-- | apps/workflowengine/lib/Check/FileMimeType.php | 107 | ||||
-rw-r--r-- | apps/workflowengine/lib/Check/TFileCheck.php | 7 | ||||
-rw-r--r-- | apps/workflowengine/lib/Service/RuleMatcher.php | 5 |
3 files changed, 31 insertions, 88 deletions
diff --git a/apps/workflowengine/lib/Check/FileMimeType.php b/apps/workflowengine/lib/Check/FileMimeType.php index feecd0997fa..77463d8960a 100644 --- a/apps/workflowengine/lib/Check/FileMimeType.php +++ b/apps/workflowengine/lib/Check/FileMimeType.php @@ -57,12 +57,18 @@ class FileMimeType extends AbstractStringCheck implements IFileCheck { /** * @param IStorage $storage * @param string $path + * @param bool $isDir */ - public function setFileInfo(IStorage $storage, string $path) { - $this->_setFileInfo($storage, $path); + public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void { + $this->_setFileInfo($storage, $path, $isDir); if (!isset($this->mimeType[$this->storage->getId()][$this->path]) || $this->mimeType[$this->storage->getId()][$this->path] === '') { - $this->mimeType[$this->storage->getId()][$this->path] = null; + + if ($isDir) { + $this->mimeType[$this->storage->getId()][$this->path] = 'httpd/unix-directory'; + } else { + $this->mimeType[$this->storage->getId()][$this->path] = null; + } } } @@ -101,93 +107,24 @@ class FileMimeType extends AbstractStringCheck implements IFileCheck { return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, 'httpd/unix-directory'); } - if ($this->isWebDAVRequest()) { - // Creating a folder - if ($this->request->getMethod() === 'MKCOL') { - return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, 'httpd/unix-directory'); - } - - if ($this->request->getMethod() === 'PUT' || $this->request->getMethod() === 'MOVE') { - if ($this->request->getMethod() === 'MOVE') { - $mimeType = $this->mimeTypeDetector->detectPath($this->path); - } else { - $path = $this->request->getPathInfo(); - $mimeType = $this->mimeTypeDetector->detectPath($path); - } - return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, $mimeType); - } - } else if ($this->isPublicWebDAVRequest()) { - if ($this->request->getMethod() === 'PUT') { - $path = $this->request->getPathInfo(); - if (strpos($path, '/webdav/') === 0) { - $path = substr($path, strlen('/webdav')); - } - $path = $this->path . $path; - $mimeType = $this->mimeTypeDetector->detectPath($path); - return $this->cacheAndReturnMimeType($this->storage->getId(), $path, $mimeType); - } + if ($this->storage->file_exists($this->path)) { + $path = $this->storage->getLocalFile($this->path); + $mimeType = $this->mimeTypeDetector->detectContent($path); + return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, $mimeType); } - if (in_array($this->request->getMethod(), ['POST', 'PUT'])) { - $files = $this->request->getUploadedFile('files'); - if (isset($files['type'][0])) { - $mimeType = $files['type'][0]; - if ($mimeType === 'application/octet-stream') { - // Maybe not... - $mimeTypeTest = $this->mimeTypeDetector->detectPath($files['name'][0]); - if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { - $mimeType = $mimeTypeTest; - } else { - $mimeTypeTest = $this->mimeTypeDetector->detect($files['tmp_name'][0]); - if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { - $mimeType = $mimeTypeTest; - } - } - } - return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, $mimeType); + if ($this->isWebDAVRequest() || $this->isPublicWebDAVRequest()) { + // Creating a folder + if ($this->request->getMethod() === 'MKCOL') { + return 'httpd/unix-directory'; } } - $mimeType = $this->storage->getMimeType($this->path); - if ($mimeType === 'application/octet-stream') { - $mimeType = $this->detectMimetypeFromPath(); - } - - return $this->cacheAndReturnMimeType($this->storage->getId(), $this->path, $mimeType); - } - - /** - * @return string - */ - protected function detectMimetypeFromPath() { - $mimeType = $this->mimeTypeDetector->detectPath($this->path); - if ($mimeType !== 'application/octet-stream' && $mimeType !== false) { - return $mimeType; - } - - if ($this->storage->instanceOfStorage('\OC\Files\Storage\Local') - || $this->storage->instanceOfStorage('\OC\Files\Storage\Home') - || $this->storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')) { - $localFile = $this->storage->getLocalFile($this->path); - if ($localFile !== false) { - $mimeType = $this->mimeTypeDetector->detect($localFile); - if ($mimeType !== false) { - return $mimeType; - } - } - - return 'application/octet-stream'; - } else { - $handle = $this->storage->fopen($this->path, 'r'); - $data = fread($handle, 8024); - fclose($handle); - $mimeType = $this->mimeTypeDetector->detectString($data); - if ($mimeType !== false) { - return $mimeType; - } - - return 'application/octet-stream'; - } + // We do not cache this, as the file did not exist yet. + // In case it does in the future, we will check with detectContent() + // again to get the real mimetype of the content, rather than + // guessing it from the path. + return $this->mimeTypeDetector->detectPath($this->path); } /** diff --git a/apps/workflowengine/lib/Check/TFileCheck.php b/apps/workflowengine/lib/Check/TFileCheck.php index 383c2d4ef5f..afaf46b52bf 100644 --- a/apps/workflowengine/lib/Check/TFileCheck.php +++ b/apps/workflowengine/lib/Check/TFileCheck.php @@ -37,14 +37,19 @@ trait TFileCheck { /** @var string */ protected $path; + /** @var bool */ + protected $isDir; + /** * @param IStorage $storage * @param string $path + * @param bool $isDir * @since 18.0.0 */ - public function setFileInfo(IStorage $storage, string $path) { + public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void { $this->storage = $storage; $this->path = $path; + $this->isDir = $isDir; } /** diff --git a/apps/workflowengine/lib/Service/RuleMatcher.php b/apps/workflowengine/lib/Service/RuleMatcher.php index 16f0e486aaa..b08bcbbe56b 100644 --- a/apps/workflowengine/lib/Service/RuleMatcher.php +++ b/apps/workflowengine/lib/Service/RuleMatcher.php @@ -71,9 +71,10 @@ class RuleMatcher implements IRuleMatcher { $this->l = $l; } - public function setFileInfo(IStorage $storage, string $path): void { + public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void { $this->fileInfo['storage'] = $storage; $this->fileInfo['path'] = $path; + $this->fileInfo['isDir'] = $isDir; } public function setEntitySubject(IEntity $entity, $subject): void { @@ -168,7 +169,7 @@ class RuleMatcher implements IRuleMatcher { if (empty($this->fileInfo)) { throw new RuntimeException('Must set file info before running the check'); } - $checkInstance->setFileInfo($this->fileInfo['storage'], $this->fileInfo['path']); + $checkInstance->setFileInfo($this->fileInfo['storage'], $this->fileInfo['path'], $this->fileInfo['isDir']); } elseif ($checkInstance instanceof IEntityCheck) { foreach($this->contexts as $entityInfo) { list($entity, $subject) = $entityInfo; |