From ca8e907adf9dc927711c7a1dae854a1872da736c Mon Sep 17 00:00:00 2001 From: trendschau Date: Wed, 1 May 2024 22:25:37 +0200 Subject: [PATCH] V2.4.3 update file component in forms and shortcode component --- media/files/filerestrictions.yaml | 2 + .../Controllers/ControllerApiFile.php | 9 +- system/typemill/Models/Storage.php | 23 +- system/typemill/Models/Validation.php | 29 ++ .../typemill/author/js/vue-blox-components.js | 59 +++- system/typemill/author/js/vue-forms.js | 293 ++++++++++-------- 6 files changed, 273 insertions(+), 142 deletions(-) diff --git a/media/files/filerestrictions.yaml b/media/files/filerestrictions.yaml index 08e86cd..edf2774 100644 --- a/media/files/filerestrictions.yaml +++ b/media/files/filerestrictions.yaml @@ -1,2 +1,4 @@ media/files/typemill-v2-navigation.gif: member media/files/markdown.png: member +media/files/wordpress-test.txt: contributor +media/files/publii-test.txt: editor diff --git a/system/typemill/Controllers/ControllerApiFile.php b/system/typemill/Controllers/ControllerApiFile.php index 7a64121..2640992 100644 --- a/system/typemill/Controllers/ControllerApiFile.php +++ b/system/typemill/Controllers/ControllerApiFile.php @@ -134,10 +134,15 @@ class ControllerApiFile extends Controller $restrictions[$filename] = $role; } - $storage->updateYaml('fileFolder', '', 'filerestrictions.yaml', $restrictions); + $result = $storage->updateYaml('fileFolder', '', 'filerestrictions.yaml', $restrictions); + if(!$result) + { + $result = $storage->getError(); + } $response->getBody()->write(json_encode([ - 'restrictions' => $restrictions + 'restrictions' => $restrictions, + 'storage' => $result ])); return $response->withHeader('Content-Type', 'application/json'); diff --git a/system/typemill/Models/Storage.php b/system/typemill/Models/Storage.php index c0c1a03..ddccea0 100644 --- a/system/typemill/Models/Storage.php +++ b/system/typemill/Models/Storage.php @@ -673,8 +673,6 @@ class Storage return $imagelist; } - - # get details from existing image for media library public function getImageDetails($name) { @@ -795,6 +793,27 @@ class Storage ################## ## FILES ## ################## + + public function checkFileExists($filepath) + { + $pathinfo = pathinfo($filepath); + if(!$pathinfo) + { + $this->error = Translations::translate('Could not read pathinfo'); + + return false; + } + + $filename = $pathinfo['filename'] . '.' . $pathinfo['extension']; + $newpath = false; + + if($this->checkFile('fileFolder', '', $filename)) + { + $newpath = 'media/files/' . $filename; + } + + return $newpath; + } public function publishFile($name) { diff --git a/system/typemill/Models/Validation.php b/system/typemill/Models/Validation.php index e51da6d..d064a14 100644 --- a/system/typemill/Models/Validation.php +++ b/system/typemill/Models/Validation.php @@ -804,6 +804,10 @@ class Validation $v->rule('lengthMax', $fieldName, 1000); $v->rule('image_types', $fieldName); break; + case "file": + $v->rule('noHTML', $fieldName); + $v->rule('lengthMax', $fieldName, 1000); + break; case "number": $v->rule('integer', $fieldName); break; @@ -916,6 +920,31 @@ class Validation } } + if($fielddefinitions['type'] == 'file') + { + # then check if file is there already: check for name and maybe correct image extension (if quality has been changed) + $storage = new StorageWrapper('\Typemill\Models\Storage'); + $existingFilePath = $storage->checkFileExists($fieldvalue); + + if($existingFilePath) + { + $fieldvalue = $existingFilePath; + } + else + { + # there is no published file with that name, so check if there is an unpublished image in tmp folder and publish it + $newFilePath = $storage->publishFile($fieldvalue); + if($newFilePath) + { + $fieldvalue = $newFilePath; + } + else + { + $fieldvalue = ''; + } + } + } + $output[$fieldname] = $fieldvalue; } else diff --git a/system/typemill/author/js/vue-blox-components.js b/system/typemill/author/js/vue-blox-components.js index da9d58a..b110a89 100644 --- a/system/typemill/author/js/vue-blox-components.js +++ b/system/typemill/author/js/vue-blox-components.js @@ -2418,7 +2418,6 @@ bloxeditor.component('video-component', { }, }) - bloxeditor.component('shortcode-component', { props: ['markdown', 'disabled', 'index'], data: function(){ @@ -2437,14 +2436,43 @@ bloxeditor.component('shortcode-component', {
- +
-
- - - - - +
+ +
+ + + + +
+
@@ -2501,25 +2529,22 @@ bloxeditor.component('shortcode-component', { { for(var i=0;i - -
-
-
- -
- - -
-
-
- -

{{ $filters.translate('upload') }}

-
-
- -
-
-
-
- - - -
-
- - -
- - -
-
- -
`, + props: ['id', 'description', 'maxlength', 'hidden', 'readonly', 'required', 'disabled', 'placeholder', 'label', 'name', 'type', 'value', 'css', 'errors'], + components: { + medialib: medialib + }, data: function(){ return { maxsize: 20, // megabyte showmedialib: false, - fileid: '', - load: false, +// fileid: '', +// load: false, + error: false, userroles: ['all'], selectedrole: '', } }, + template: `
+ + + +
+ +
+ +
+ + +
+ + +
+
+ +
+ + +
+ +
+ +
+ +
+

+ + + + {{ $filters.translate('upload a file') }} +

+ +
+
+ +
+
+
+

{{ error }}

+

{{ $filters.translate(description) }}

+ + +
+ + +
+
+
`, + mounted: function(){ this.getrestriction(); }, methods: { - addFromMedialibFunction(value) + addFromMedialibFunction(file) { - this.imgfile = value; - this.imgpreview = data.urlinfo.baseurl + '/' + value; + this.error = false; this.showmedialib = false; - this.saveimage = false; - this.createmarkdown(); + this.update(file.url); + this.getrestriction(file.url); }, - update: function(value) + update: function(filepath) { - FormBus.$emit('forminput', {'name': this.name, 'value': value}); - }, - updatemarkdown: function(markdown, url) - { - /* is called from child component medialib if file has been selected */ - this.update(url); - this.getrestriction(url); - }, - createmarkdown: function(url) - { - /* is called from child component medialib */ - this.update(url); + eventBus.$emit('forminput', {'name': this.name, 'value': filepath}); }, deleteFile: function() { + this.error = false; + this.update(''); this.selectedrole = 'all'; }, - getrestriction: function(url) + getrestriction(newfilepath) { - var filename = this.value; - if(url) + this.error = false; + + var filepath = this.value; + if(newfilepath) { - filename = url; + filepath = newfilepath; } var myself = this; - myaxios.get('/api/v1/filerestrictions',{ - params: { - 'url': document.getElementById("path").value, - 'filename': filename, - } + tmaxios.get('/api/v1/filerestrictions',{ + params: { + 'url': data.urlinfo.route, + 'filename': filepath, + } }) - .then(function (response) { - myself.userroles = ['all']; - myself.userroles = myself.userroles.concat(response.data.userroles); - myself.selectedrole = response.data.restriction; - }) - .catch(function (error) - { - if(error.response) - { - } - }); - }, - updaterestriction: function() - { - myaxios.post('/api/v1/filerestrictions',{ - 'url': document.getElementById("path").value, - 'filename': this.value, - 'role': this.selectedrole, + .then(function (response) { + myself.userroles = ['all']; + myself.userroles = myself.userroles.concat(response.data.userroles); + myself.selectedrole = response.data.restriction; }) - .then(function (response) { - - }) - .catch(function (error) - { - if(error.response) - { - } - }); + .catch(function (error) + { + if(error.response.data.message) + { + myself.error = myself.$filters.translate(error.response.data.message); + myself.error +// eventBus.$emit('publishermessage', message); + } + }); }, - onFileChange: function( e ) + updaterestriction() { + this.error = false; + + var filepath = this.value; + if(!filepath) + { + this.error = 'File is missing for a restriction.'; + } + + var resself = this; + + tmaxios.post('/api/v1/filerestrictions',{ + 'url': data.urlinfo.route, + 'filename': filepath, + 'role': this.selectedrole, + }) + .then(function (response) { + + }) + .catch(function (error){ + resself.error = 'some error update file restrictions'; + }); + }, + onFileChange( e ) + { + this.error = false; + if(e.target.files.length > 0) { let uploadedFile = e.target.files[0]; @@ -1074,34 +1122,37 @@ app.component('component-file', { if (size > this.maxsize) { - publishController.errors.message = "The maximal size of a file is " + this.maxsize + " MB"; + let message = "The maximal size of a file is " + this.maxsize + " MB"; + + // show error in component + eventBus.$emit('publishermessage', message); } else { - sharedself = this; - - sharedself.load = true; + self.load = true; + + self = this; let reader = new FileReader(); reader.readAsDataURL(uploadedFile); - reader.onload = function(e) { - myaxios.post('/api/v1/file',{ - 'url': document.getElementById("path").value, + reader.onload = function(e) + { + tmaxios.post('/api/v1/file',{ 'file': e.target.result, - 'name': uploadedFile.name, - 'publish': true, + 'name': uploadedFile.name }) .then(function (response) { - sharedself.load = false; - sharedself.selectedrole = 'all'; - sharedself.update(response.data.info.url); + self.filetitle = response.data.fileinfo.title; + self.selectedrole = 'all'; + self.update(response.data.filepath); }) .catch(function (error) { - sharedself.load = false; if(error.response) { - publishController.errors.message = error.response.data.errors; + self.error = 'error update file'; + console.info(error.response); + /* publishController.errors.message = error.response.data.errors; */ } }); }