]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add file mime type
authorJoas Schilling <coding@schilljs.com>
Thu, 28 Jul 2016 09:44:22 +0000 (11:44 +0200)
committerJoas Schilling <coding@schilljs.com>
Wed, 3 Aug 2016 12:40:29 +0000 (14:40 +0200)
apps/workflowengine/css/admin.css
apps/workflowengine/js/admin.js
apps/workflowengine/js/filemimetypeplugin.js [new file with mode: 0644]
apps/workflowengine/js/filesizeplugin.js
apps/workflowengine/js/filesystemtagsplugin.js
apps/workflowengine/js/usergroupmembershipplugin.js
apps/workflowengine/lib/AppInfo/Application.php
apps/workflowengine/lib/Check/AbstractStringCheck.php [new file with mode: 0644]
apps/workflowengine/lib/Check/FileMimeType.php [new file with mode: 0644]
apps/workflowengine/tests/Check/AbstractStringCheckTest.php [new file with mode: 0644]

index 1d94fced003d00f4aaaa7bb51379fce3da60588d..70185615ad642d152c14243e8ada5cb263976605 100644 (file)
@@ -46,3 +46,7 @@
        margin-right: 5px;
 }
 
+.workflowengine .invalid-input {
+       border-color: #aa0000;
+}
+
index e6df4b75f70e236e579579c4580027fb38fc44e5..ce85c8c008bde1cad017da12de115dc26b1a3093 100644 (file)
 
                                        _.each(OCA.WorkflowEngine.availablePlugins, function(plugin) {
                                                if (_.isFunction(plugin.render)) {
-                                                       plugin.render(valueElement, check['class'], check['value']);
+                                                       plugin.render(valueElement, check);
                                                }
                                        });
                                }, this);
diff --git a/apps/workflowengine/js/filemimetypeplugin.js b/apps/workflowengine/js/filemimetypeplugin.js
new file mode 100644 (file)
index 0000000..33cbbd7
--- /dev/null
@@ -0,0 +1,72 @@
+/**
+ * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+(function() {
+
+       OCA.WorkflowEngine = OCA.WorkflowEngine || {};
+       OCA.WorkflowEngine.Plugins = OCA.WorkflowEngine.Plugins || {};
+
+       OCA.WorkflowEngine.Plugins.FileMimeTypePlugin = {
+               getCheck: function() {
+                       return {
+                               'class': 'OCA\\WorkflowEngine\\Check\\FileMimeType',
+                               'name': t('workflowengine', 'File mime type (upload)'),
+                               'operators': [
+                                       {'operator': 'is', 'name': t('workflowengine', 'is')},
+                                       {'operator': '!is', 'name': t('workflowengine', 'is not')},
+                                       {'operator': 'matches', 'name': t('workflowengine', 'matches')},
+                                       {'operator': '!matches', 'name': t('workflowengine', 'does not match')}
+                               ]
+                       };
+               },
+               render: function(element, check) {
+                       if (check['class'] !== 'OCA\\WorkflowEngine\\Check\\FileMimeType') {
+                               return;
+                       }
+
+                       var placeholder = t('workflowengine', 'text/plain');
+                       if (check['operator'] === 'matches' || check['operator'] === '!matches') {
+                               placeholder = t('workflowengine', '/^text\\/(plain|html)$/i');
+
+                               if (this._validateRegex(check['value'])) {
+                                       $(element).removeClass('invalid-input');
+                               } else {
+                                       $(element).addClass('invalid-input');
+                               }
+                       }
+
+                       $(element).css('width', '250px')
+                               .attr('placeholder', placeholder)
+                               .attr('title', t('workflowengine', 'Example: {placeholder}', {placeholder: placeholder}))
+                               .addClass('has-tooltip')
+                               .tooltip({
+                                       placement: 'bottom'
+                               });
+               },
+
+               _validateRegex: function(string) {
+                       var regexRegex = /^\/(.*)\/([gui]{0,3})$/,
+                               result = regexRegex.exec(string);
+                       return result !== null;
+               }
+       };
+})();
+
+OC.Plugins.register('OCA.WorkflowEngine.CheckPlugins', OCA.WorkflowEngine.Plugins.FileMimeTypePlugin);
index add2e57821ceb3322d0067b9533c9c9eafc4d681..0efa9d00edf06798be76e8b96d0e057c07475d93 100644 (file)
@@ -27,7 +27,7 @@
                getCheck: function() {
                        return {
                                'class': 'OCA\\WorkflowEngine\\Check\\FileSize',
-                               'name': t('workflowengine', 'File size'),
+                               'name': t('workflowengine', 'File size (upload)'),
                                'operators': [
                                        {'operator': 'less', 'name': t('workflowengine', 'less')},
                                        {'operator': '!greater', 'name': t('workflowengine', 'less or equals')},
                                ]
                        };
                },
-               render: function(element, classname, value) {
-                       if (classname !== 'OCA\\WorkflowEngine\\Check\\FileSize') {
+               render: function(element, check) {
+                       if (check['class'] !== 'OCA\\WorkflowEngine\\Check\\FileSize') {
                                return;
                        }
 
+                       var placeholder = '12 MB'; // Do not translate!!!
                        $(element).css('width', '250px')
-                               .attr('placeholder', t('workflowengine', '12 MB'))
-                       ;
+                               .attr('placeholder', placeholder)
+                               .attr('title', t('workflowengine', 'Example: {placeholder}', {placeholder: placeholder}))
+                               .addClass('has-tooltip')
+                               .tooltip({
+                                       placement: 'bottom'
+                               });
                }
        };
 })();
index 6f2f231c5e71fd4da175416d5a23e06166b05b69..026345571e793c8557f296de836335e336be1e48 100644 (file)
@@ -41,8 +41,8 @@
                                ]
                        };
                },
-               render: function(element, classname, value) {
-                       if (classname !== 'OCA\\WorkflowEngine\\Check\\FileSystemTags') {
+               render: function(element, check) {
+                       if (check['class'] !== 'OCA\\WorkflowEngine\\Check\\FileSystemTags') {
                                return;
                        }
 
index 528a7bd3e3d059bb25b9b75990ca7604d2d95ae9..1c09e7d5ccd18821e5d1cf1e1bf22a5be75bd865 100644 (file)
@@ -34,8 +34,8 @@
                                ]
                        };
                },
-               render: function(element, classname, value) {
-                       if (classname !== 'OCA\\WorkflowEngine\\Check\\UserGroupMembership') {
+               render: function(element, check) {
+                       if (check['class'] !== 'OCA\\WorkflowEngine\\Check\\UserGroupMembership') {
                                return;
                        }
 
index 64e03081a3427cc1d00e6d8774887e45fb130cd0..c1dc65fe8104c2822e17ad3f893631da04943847 100644 (file)
@@ -55,6 +55,7 @@ class Application extends \OCP\AppFramework\App {
                                        'admin',
 
                                        // Check plugins
+                                       'filemimetypeplugin',
                                        'filesizeplugin',
                                        'filesystemtagsplugin',
                                        'usergroupmembershipplugin',
diff --git a/apps/workflowengine/lib/Check/AbstractStringCheck.php b/apps/workflowengine/lib/Check/AbstractStringCheck.php
new file mode 100644 (file)
index 0000000..7757626
--- /dev/null
@@ -0,0 +1,110 @@
+<?php
+/**
+ * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\WorkflowEngine\Check;
+
+
+use OCP\Files\Storage\IStorage;
+use OCP\WorkflowEngine\ICheck;
+
+abstract class AbstractStringCheck implements ICheck {
+
+       /** @var array[] Nested array: [Pattern => [ActualValue => Regex Result]] */
+       protected $matches;
+
+       /**
+        * @param IStorage $storage
+        * @param string $path
+        */
+       public function setFileInfo(IStorage $storage, $path) {
+               // Nothing changes here with a different path
+       }
+
+       /**
+        * @return string
+        */
+       abstract protected function getActualValue();
+
+       /**
+        * @param string $operator
+        * @param string $value
+        * @return bool
+        */
+       public function executeCheck($operator, $value)  {
+               $actualValue = $this->getActualValue();
+               return $this->executeStringCheck($operator, $value, $actualValue);
+       }
+
+       /**
+        * @param string $operator
+        * @param string $checkValue
+        * @param string $actualValue
+        * @return bool
+        */
+       protected function executeStringCheck($operator, $checkValue, $actualValue) {
+               if ($operator === 'is') {
+                       return $checkValue === $actualValue;
+               } else if ($operator === '!is') {
+                       return $checkValue !== $actualValue;
+               } else {
+                       $match = $this->match($checkValue, $actualValue);
+                       if ($operator === 'matches') {
+                               return $match === 1;
+                       } else {
+                               return $match === 0;
+                       }
+               }
+       }
+
+       /**
+        * @param string $operator
+        * @param string $value
+        * @throws \UnexpectedValueException
+        */
+       public function validateCheck($operator, $value) {
+               if (!in_array($operator, ['is', '!is', 'matches', '!matches'])) {
+                       throw new \UnexpectedValueException('Invalid operator', 1);
+               }
+
+               if (in_array($operator, ['matches', '!matches']) &&
+                         @preg_match($value, null) === false) {
+                       throw new \UnexpectedValueException('Invalid regex', 2);
+               }
+       }
+
+       /**
+        * @param string $pattern
+        * @param string $subject
+        * @return int|bool
+        */
+       protected function match($pattern, $subject) {
+               $patternHash = md5($pattern);
+               $subjectHash = md5($subject);
+               if (isset($this->matches[$patternHash][$subjectHash])) {
+                       return $this->matches[$patternHash][$subjectHash];
+               }
+               if (!isset($this->matches[$patternHash])) {
+                       $this->matches[$patternHash] = [];
+               }
+               $this->matches[$patternHash][$subjectHash] = preg_match($pattern, $subject);
+               return $this->matches[$patternHash][$subjectHash];
+       }
+}
diff --git a/apps/workflowengine/lib/Check/FileMimeType.php b/apps/workflowengine/lib/Check/FileMimeType.php
new file mode 100644 (file)
index 0000000..c774d30
--- /dev/null
@@ -0,0 +1,82 @@
+<?php
+/**
+ * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\WorkflowEngine\Check;
+
+
+use OCP\Files\IMimeTypeDetector;
+use OCP\IRequest;
+
+class FileMimeType extends AbstractStringCheck {
+
+       /** @var string */
+       protected $mimeType;
+
+       /** @var IRequest */
+       protected $request;
+
+       /** @var IMimeTypeDetector */
+       protected $mimeTypeDetector;
+
+       /**
+        * @param IRequest $request
+        * @param IMimeTypeDetector $mimeTypeDetector
+        */
+       public function __construct(IRequest $request, IMimeTypeDetector $mimeTypeDetector) {
+               $this->request = $request;
+               $this->mimeTypeDetector = $mimeTypeDetector;
+       }
+
+       /**
+        * @return string
+        */
+       protected function getActualValue() {
+               if ($this->mimeType !== null) {
+                       return $this->mimeType;
+               }
+
+               $this->mimeType = '';
+               if ($this->isWebDAVRequest()) {
+                       if ($this->request->getMethod() === 'PUT') {
+                               $path = $this->request->getPathInfo();
+                               $this->mimeType = $this->mimeTypeDetector->detectPath($path);
+                       }
+               } else if (in_array($this->request->getMethod(), ['POST', 'PUT'])) {
+                       $files = $this->request->getUploadedFile('files');
+                       if (isset($files['type'][0])) {
+                               $this->mimeType = $files['type'][0];
+                       }
+               }
+               return $this->mimeType;
+       }
+
+       /**
+        * @return bool
+        */
+       protected function isWebDAVRequest() {
+               return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && (
+                       $this->request->getPathInfo() === '/webdav' ||
+                       strpos($this->request->getPathInfo(), '/webdav/') === 0 ||
+                       $this->request->getPathInfo() === '/dav/files' ||
+                       strpos($this->request->getPathInfo(), '/dav/files/') === 0
+               );
+       }
+}
diff --git a/apps/workflowengine/tests/Check/AbstractStringCheckTest.php b/apps/workflowengine/tests/Check/AbstractStringCheckTest.php
new file mode 100644 (file)
index 0000000..43818ab
--- /dev/null
@@ -0,0 +1,149 @@
+<?php
+/**
+ * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\WorkflowEngine\Tests\Check;
+
+
+class AbstractStringCheckTest extends \Test\TestCase {
+
+       public function dataExecuteStringCheck() {
+               return [
+                       ['is', 'same', 'same', true],
+                       ['is', 'different', 'not the same', false],
+                       ['!is', 'same', 'same', false],
+                       ['!is', 'different', 'not the same', true],
+
+                       ['matches', '/match/', 'match', true],
+                       ['matches', '/different/', 'not the same', false],
+                       ['!matches', '/match/', 'match', false],
+                       ['!matches', '/different/', 'not the same', true],
+               ];
+       }
+
+       /**
+        * @dataProvider dataExecuteStringCheck
+        * @param string $operation
+        * @param string $checkValue
+        * @param string $actualValue
+        * @param bool $expected
+        */
+       public function testExecuteStringCheck($operation, $checkValue, $actualValue, $expected) {
+               $check = $this->getMockBuilder('OCA\WorkflowEngine\Check\AbstractStringCheck')
+                       ->setMethods([
+                               'setPath',
+                               'executeCheck',
+                               'getActualValue',
+                       ])
+                       ->getMock();
+
+               /** @var \OCA\WorkflowEngine\Check\AbstractStringCheck $check */
+               $this->assertEquals($expected, $this->invokePrivate($check, 'executeStringCheck', [$operation, $checkValue, $actualValue]));
+       }
+
+       public function dataValidateCheck() {
+               return [
+                       ['is', '/Invalid(Regex/'],
+                       ['!is', '/Invalid(Regex/'],
+                       ['matches', '/Valid(Regex)/'],
+                       ['!matches', '/Valid(Regex)/'],
+               ];
+       }
+
+       /**
+        * @dataProvider dataValidateCheck
+        * @param string $operator
+        * @param string $value
+        */
+       public function testValidateCheck($operator, $value) {
+               $check = $this->getMockBuilder('OCA\WorkflowEngine\Check\AbstractStringCheck')
+                       ->setMethods([
+                               'setPath',
+                               'executeCheck',
+                               'getActualValue',
+                       ])
+                       ->getMock();
+
+               /** @var \OCA\WorkflowEngine\Check\AbstractStringCheck $check */
+               $check->validateCheck($operator, $value);
+       }
+
+       public function dataValidateCheckInvalid() {
+               return [
+                       ['!!is', '', 1, 'Invalid operator'],
+                       ['less', '', 1, 'Invalid operator'],
+                       ['matches', '/Invalid(Regex/', 2, 'Invalid regex'],
+                       ['!matches', '/Invalid(Regex/', 2, 'Invalid regex'],
+               ];
+       }
+
+       /**
+        * @dataProvider dataValidateCheckInvalid
+        * @param $operator
+        * @param $value
+        * @param $exceptionCode
+        * @param $exceptionMessage
+        */
+       public function testValidateCheckInvalid($operator, $value, $exceptionCode, $exceptionMessage) {
+               $check = $this->getMockBuilder('OCA\WorkflowEngine\Check\AbstractStringCheck')
+                       ->setMethods([
+                               'setPath',
+                               'executeCheck',
+                               'getActualValue',
+                       ])
+                       ->getMock();
+
+               try {
+                       /** @var \OCA\WorkflowEngine\Check\AbstractStringCheck $check */
+                       $check->validateCheck($operator, $value);
+               } catch (\UnexpectedValueException $e) {
+                       $this->assertEquals($exceptionCode, $e->getCode());
+                       $this->assertEquals($exceptionMessage, $e->getMessage());
+               }
+       }
+
+       public function dataMatch() {
+               return [
+                       ['/valid/', 'valid', [], true],
+                       ['/valid/', 'valid', [md5('/valid/') => [md5('valid') => false]], false], // Cache hit
+               ];
+       }
+
+       /**
+        * @dataProvider dataMatch
+        * @param string $pattern
+        * @param string $subject
+        * @param array[] $matches
+        * @param bool $expected
+        */
+       public function testMatch($pattern, $subject, $matches, $expected) {
+               $check = $this->getMockBuilder('OCA\WorkflowEngine\Check\AbstractStringCheck')
+                       ->setMethods([
+                               'setPath',
+                               'executeCheck',
+                               'getActualValue',
+                       ])
+                       ->getMock();
+
+               $this->invokePrivate($check, 'matches', [$matches]);
+
+               $this->assertEquals($expected, $this->invokePrivate($check, 'match', [$pattern, $subject]));
+       }
+}