diff options
author | Joas Schilling <coding@schilljs.com> | 2016-09-08 09:19:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-08 09:19:33 +0200 |
commit | cdfe538452b709dd181ac11fcfca72a5221c79ad (patch) | |
tree | c99d33fd3c0d735af0fbc98868b2911a05a738a4 /apps/workflowengine | |
parent | b992bf21426cd23428b56d00b67ce100bf0da4be (diff) | |
parent | 2d61ee3c13cda96c0cd4401b78bd27a586c374a9 (diff) | |
download | nextcloud-server-cdfe538452b709dd181ac11fcfca72a5221c79ad.tar.gz nextcloud-server-cdfe538452b709dd181ac11fcfca72a5221c79ad.zip |
Merge pull request #1243 from nextcloud/fix-detection-of-file-types-a-bit
Fix detection of file types a bit
Diffstat (limited to 'apps/workflowengine')
-rw-r--r-- | apps/workflowengine/js/filemimetypeplugin.js | 2 | ||||
-rw-r--r-- | apps/workflowengine/lib/Check/FileMimeType.php | 93 |
2 files changed, 86 insertions, 9 deletions
diff --git a/apps/workflowengine/js/filemimetypeplugin.js b/apps/workflowengine/js/filemimetypeplugin.js index 33cbbd7fd7e..9fc9e3452f4 100644 --- a/apps/workflowengine/js/filemimetypeplugin.js +++ b/apps/workflowengine/js/filemimetypeplugin.js @@ -27,7 +27,7 @@ getCheck: function() { return { 'class': 'OCA\\WorkflowEngine\\Check\\FileMimeType', - 'name': t('workflowengine', 'File mime type (upload)'), + 'name': t('workflowengine', 'File mime type'), 'operators': [ {'operator': 'is', 'name': t('workflowengine', 'is')}, {'operator': '!is', 'name': t('workflowengine', 'is not')}, diff --git a/apps/workflowengine/lib/Check/FileMimeType.php b/apps/workflowengine/lib/Check/FileMimeType.php index 1de9a70a17d..4a985840e60 100644 --- a/apps/workflowengine/lib/Check/FileMimeType.php +++ b/apps/workflowengine/lib/Check/FileMimeType.php @@ -23,12 +23,13 @@ namespace OCA\WorkflowEngine\Check; use OCP\Files\IMimeTypeDetector; +use OCP\Files\Storage\IStorage; use OCP\IL10N; use OCP\IRequest; class FileMimeType extends AbstractStringCheck { - /** @var string */ + /** @var array */ protected $mimeType; /** @var IRequest */ @@ -37,6 +38,12 @@ class FileMimeType extends AbstractStringCheck { /** @var IMimeTypeDetector */ protected $mimeTypeDetector; + /** @var IStorage */ + protected $storage; + + /** @var string */ + protected $path; + /** * @param IL10N $l * @param IRequest $request @@ -49,26 +56,96 @@ class FileMimeType extends AbstractStringCheck { } /** + * @param IStorage $storage + * @param string $path + */ + public function setFileInfo(IStorage $storage, $path) { + $this->storage = $storage; + $this->path = $path; + if (!isset($this->mimeType[$this->storage->getId()][$this->path]) + || $this->mimeType[$this->storage->getId()][$this->path] === '') { + $this->mimeType[$this->storage->getId()][$this->path] = null; + } + } + + /** * @return string */ protected function getActualValue() { - if ($this->mimeType !== null) { - return $this->mimeType; + if ($this->mimeType[$this->storage->getId()][$this->path] !== null) { + return $this->mimeType[$this->storage->getId()][$this->path]; } - $this->mimeType = ''; + $this->mimeType[$this->storage->getId()][$this->path] = ''; if ($this->isWebDAVRequest()) { if ($this->request->getMethod() === 'PUT') { $path = $this->request->getPathInfo(); - $this->mimeType = $this->mimeTypeDetector->detectPath($path); + $this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($path); + return $this->mimeType[$this->storage->getId()][$this->path]; } - } else if (in_array($this->request->getMethod(), ['POST', 'PUT'])) { + } + + if (in_array($this->request->getMethod(), ['POST', 'PUT'])) { $files = $this->request->getUploadedFile('files'); if (isset($files['type'][0])) { - $this->mimeType = $files['type'][0]; + $mimeType = $files['type'][0]; + if ($this->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; + } + } + } + $this->mimeType[$this->storage->getId()][$this->path] = $mimeType; + return $mimeType; + } + } + + $this->mimeType[$this->storage->getId()][$this->path] = $this->storage->getMimeType($this->path); + if ($this->mimeType[$this->storage->getId()][$this->path] === 'application/octet-stream') { + $this->mimeType[$this->storage->getId()][$this->path] = $this->detectMimetypeFromPath(); + } + + return $this->mimeType[$this->storage->getId()][$this->path]; + } + + /** + * @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'; } - return $this->mimeType; } /** |