diff --git a/assets/js/post-form.js b/assets/js/post-form.js index bdfe8732..c9910337 100644 --- a/assets/js/post-form.js +++ b/assets/js/post-form.js @@ -14,6 +14,7 @@ this.initDropzones() this.initFormEvents() + this.initImagePasting() this.addToolbarButton() } @@ -104,6 +105,48 @@ }) } + PostForm.prototype.initImagePasting = function() { + var self = this + + self.$markdownEditor.bind('paste', function (event) { + if (event.type.indexOf('copy') === 0 || event.type.indexOf('paste') === 0) { + event.clipboardData = event.originalEvent.clipboardData + } + + var clipboardData = event.clipboardData + + if (clipboardData.types.indexOf('Files') != -1) { + var file = clipboardData.items[0].getAsFile() + var reader = new FileReader() + reader.onload = function (evt) { + self.pauseUpdates() + + var formData = new FormData() + formData.append('X_BLOG_IMAGE_UPLOAD', 1) + formData.append('_image', evt.target.result) + formData.append('_session_key', self.sessionKey) + + $.ajax({ + url: self.formAction, + type: 'POST', + cache: false, + contentType: false, + processData: false, + data: formData + }) + .done(function(response) { + self.resumeUpdates() + self.codeEditor.insertSnippet('!['+response.file+']('+response.path+')') + }) + .fail(function(response) { + self.resumeUpdates() + }); + }; + reader.readAsDataURL(file) + } + }); + } + PostForm.prototype.pauseUpdates = function() { this.$markdownEditor.markdownEditor('pauseUpdates') } @@ -181,4 +224,4 @@ $.oc.blogPostForm = form }) -}(window.jQuery); \ No newline at end of file +}(window.jQuery); diff --git a/formwidgets/BlogMarkdown.php b/formwidgets/BlogMarkdown.php index 9907c0df..e611c991 100644 --- a/formwidgets/BlogMarkdown.php +++ b/formwidgets/BlogMarkdown.php @@ -55,31 +55,48 @@ protected function checkUploadPostback() $uploadedFileName = null; try { - $uploadedFile = Input::file('file'); - if ($uploadedFile) - $uploadedFileName = $uploadedFile->getClientOriginalName(); + if (Input::has('file')) { + $uploadedFile = Input::file('file'); - $validationRules = ['max:'.File::getMaxFilesize()]; - $validationRules[] = 'mimes:jpg,jpeg,bmp,png,gif'; + if ($uploadedFile) { + $uploadedFileName = $uploadedFile->getClientOriginalName(); + } - $validation = Validator::make( - ['file_data' => $uploadedFile], - ['file_data' => $validationRules] - ); + $validationRules = ['max:'.File::getMaxFilesize()]; + $validationRules[] = 'mimes:jpg,jpeg,bmp,png,gif'; - if ($validation->fails()) { - throw new ValidationException($validation); - } + $validation = Validator::make( + ['file_data' => $uploadedFile], + ['file_data' => $validationRules] + ); + + if ($validation->fails()) { + throw new ValidationException($validation); + } + + if (!$uploadedFile->isValid()) { + throw new SystemException(Lang::get('cms::lang.asset.file_not_valid')); + } + + $file = new File(); + $file->data = $uploadedFile; + } elseif (Input::has('_image')) { + $content = Input::get('_image'); + + preg_match('/^(data:\s*image\/(\w+);base64,)/', $content, $result); + + $fileContent = base64_decode(str_replace($result[1], '', $content)); + $fileExt = $result[2]; + $fileName = md5($file_content) . '.' . $fileExt; + $uploadedFileName = 'image' . '.' . $fileExt; - if (!$uploadedFile->isValid()) { - throw new SystemException(Lang::get('cms::lang.asset.file_not_valid')); + $file = new File(); + $file->fromData($fileContent, $fileName); } $fileRelation = $this->model->content_images(); - $file = new File(); - $file->data = $uploadedFile; $file->is_public = true; $file->save();