Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload pasted image on editor #495

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions assets/js/post-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

this.initDropzones()
this.initFormEvents()
this.initImagePasting()
this.addToolbarButton()
}

Expand Down Expand Up @@ -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;
mhanoglu marked this conversation as resolved.
Show resolved Hide resolved
}

var clipboardData = event.clipboardData;

if (clipboardData.types.indexOf('Files') != -1) {
mhanoglu marked this conversation as resolved.
Show resolved Hide resolved
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)
mhanoglu marked this conversation as resolved.
Show resolved Hide resolved
formData.append('_session_key', self.sessionKey)

jQuery.ajax({
mhanoglu marked this conversation as resolved.
Show resolved Hide resolved
url: self.formAction,
type: "POST",
mhanoglu marked this conversation as resolved.
Show resolved Hide resolved
cache: false,
contentType: false,
processData: false,
data: formData
})
.done(function(response) {
mhanoglu marked this conversation as resolved.
Show resolved Hide resolved
self.resumeUpdates();
self.codeEditor.insertSnippet('![' + response.file + '](' + response.path + ')')
})
.fail(function(response) {
mhanoglu marked this conversation as resolved.
Show resolved Hide resolved
self.resumeUpdates();
});
};
reader.readAsDataURL(file);
}
});
}

PostForm.prototype.pauseUpdates = function() {
this.$markdownEditor.markdownEditor('pauseUpdates')
}
Expand Down
51 changes: 35 additions & 16 deletions formwidgets/BlogMarkdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,50 @@ protected function checkUploadPostback()
$uploadedFileName = null;

try {
$uploadedFile = Input::file('file');

if ($uploadedFile)
$uploadedFileName = $uploadedFile->getClientOriginalName();
if (Input::has('file')) {

$validationRules = ['max:'.File::getMaxFilesize()];
$validationRules[] = 'mimes:jpg,jpeg,bmp,png,gif';
$uploadedFile = Input::file('file');

$validation = Validator::make(
['file_data' => $uploadedFile],
['file_data' => $validationRules]
);
if ($uploadedFile)
$uploadedFileName = $uploadedFile->getClientOriginalName();
mhanoglu marked this conversation as resolved.
Show resolved Hide resolved

if ($validation->fails()) {
throw new ValidationException($validation);
}
$validationRules = ['max:'.File::getMaxFilesize()];
$validationRules[] = 'mimes:jpg,jpeg,bmp,png,gif';

$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;

} else if (Input::has('_image')) {

mhanoglu marked this conversation as resolved.
Show resolved Hide resolved
$content = Input::get('_image');

preg_match('/^(data:\s*image\/(\w+);base64,)/', $content, $result);

$file_content = base64_decode( str_replace( $result[1], '', $content ) );
mhanoglu marked this conversation as resolved.
Show resolved Hide resolved
$file_ext = $result[2];
mhanoglu marked this conversation as resolved.
Show resolved Hide resolved
$file_name = md5( $file_content ) . "." . $file_ext;
mhanoglu marked this conversation as resolved.
Show resolved Hide resolved
$uploadedFileName = "image" . "." . $file_ext;
mhanoglu marked this conversation as resolved.
Show resolved Hide resolved

if (!$uploadedFile->isValid()) {
throw new SystemException(Lang::get('cms::lang.asset.file_not_valid'));
$file = new File();
$file->fromData($file_content, $file_name);
mhanoglu marked this conversation as resolved.
Show resolved Hide resolved
}

$fileRelation = $this->model->content_images();

$file = new File();
$file->data = $uploadedFile;
$file->is_public = true;
$file->save();

Expand Down