-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
skywalker512
committed
Jul 10, 2018
1 parent
fd43ef6
commit ad5cedd
Showing
7 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Upyun upload plugin | ||
|
||
|
||
## migrate db from http images to https(protocal-relative url) | ||
|
||
```sql | ||
UPDATE `et_post` | ||
SET `content` = REPLACE(`content`, "http://<bucket>.b0.upaiyun.com", "//<bucket>.b0.upaiyun.com") | ||
WHERE `content` LIKE "%http://<bucket>.b0.upaiyun.com%" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
if (!defined('IN_ESOTALK')) exit; | ||
|
||
class UpyunController extends ETController { | ||
|
||
public function action_index() | ||
{ | ||
$this->render404(T('message.pageNotFound')); | ||
return false; | ||
} | ||
|
||
public function action_signature() | ||
{ | ||
if ( !ET::$session->user ) | ||
{ | ||
$this->render404(T('message.pageNotFound')); | ||
return false; | ||
} | ||
|
||
$bucket = C('plugin.upyun.bucket'); | ||
$secret = C('plugin.upyun.secret'); | ||
$expiration = 86400; // 24h | ||
|
||
// TODO more configs | ||
$policy = array( | ||
'bucket' => $bucket, | ||
'expiration' => time() + $expiration, | ||
'save-key' => '/{filemd5}{.suffix}', | ||
'content-length-range' => '1024,12582912', // 1K - 12M | ||
'x-gmkerl-rotate' => 'auto', | ||
); | ||
|
||
|
||
$policyBase64 = base64_encode(json_encode($policy)); | ||
$signature = md5("$policyBase64&$secret"); | ||
|
||
header('Cache-Control: max-age=' . $expiration - 600); | ||
header('Content-Type: application/json; charset=utf-8'); | ||
echo json_encode(array( | ||
'bucket' => $bucket, | ||
'policy' => $policyBase64, | ||
'signature' => $signature, | ||
)); | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
if (!defined('IN_ESOTALK')) exit; | ||
|
||
ET::$pluginInfo['Upyun'] = array( | ||
'name' => 'Upyun', | ||
'description' => '允许用户上传附件到又拍云', | ||
'version' => '0.1.0', | ||
'author' => 'rhyzx', | ||
'authorEmail' => '[email protected]', | ||
'authorURL' => 'https://3dgundam.org', | ||
'license' => 'MIT' | ||
); | ||
|
||
class ETPlugin_Upyun extends ETPlugin { | ||
|
||
public function __construct($rootDirectory) | ||
{ | ||
parent::__construct($rootDirectory); | ||
ETFactory::registerController('upyun', 'UpyunController', dirname(__FILE__).'/UpyunController.class.php'); | ||
} | ||
|
||
public function handler_conversationController_renderBefore($sender) | ||
{ | ||
$sender->addJSFile($this->resource('upyun.js')); | ||
$sender->addCssFile($this->resource('upyun.css')); | ||
} | ||
public function handler_conversationController_getEditControls($sender, &$controls, $id) | ||
{ | ||
addToArrayString($controls, "imageup", "<a href='javascript:UPyun.imageup(\"$id\");void(0)' title='".T("文件上传")."' class='control-fixed'><i class='icon-paper-clip'></i></a>", 0); | ||
|
||
} | ||
|
||
|
||
public function settings($sender) | ||
{ | ||
$form = ETFactory::make('form'); | ||
$form->action = URL('admin/plugins/settings/Upyun'); | ||
|
||
$form->setValue('bucket', C('plugin.upyun.bucket')); | ||
$form->setValue('secret', C('plugin.upyun.secret')); | ||
// $form->setValue('expiration', C('plugin.upyun.expiration')); | ||
|
||
if ($form->validPostBack('submit')) { | ||
$config = array(); | ||
$config['plugin.upyun.bucket'] = $form->getValue('bucket'); | ||
$config['plugin.upyun.secret'] = $form->getValue('secret'); | ||
|
||
if (!$form->errorCount()) { | ||
ET::writeConfig($config); | ||
$sender->message(T('message.changesSaved'), 'success autoDismiss'); | ||
$sender->redirect(URL('admin/plugins')); | ||
} | ||
} | ||
$sender->data('form', $form); | ||
return $this->view('settings'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.upyun-drag-highlight { | ||
box-shadow: inset 0 0 0 1px #edcd1c; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
var UPyun = { | ||
imageup: function(id) {ETConversation.wrapText($("#"+id+" textarea"), "[img]", "[/img]", "", "http://example.com/image.jpg");}, | ||
}; | ||
void function () { | ||
|
||
//if ( typeof BBCode === 'undefined' || typeof FormData === 'undefined' ) return | ||
|
||
function getSign() { | ||
return $.getJSON( '/upyun/signature' ) | ||
.fail(function () { | ||
ETMessages.showMessage('无法进行上传验证', 'warning') | ||
}) | ||
} | ||
|
||
|
||
// click upload | ||
var $file = $('<input type="file" multiple accept="image/png, image/gif, image/jpg, image/jpeg">') | ||
var $target = $() | ||
$file.on('change', function(evt) { | ||
$.each(this.files, function(i, file) { | ||
upload(file, $target) | ||
}); | ||
this.value = '' | ||
}) | ||
UPyun.imageup = function (id) { | ||
$target = $('#' + id + ' textarea') | ||
$file.trigger('click') | ||
} | ||
|
||
// drag upload | ||
$(document) | ||
.on('drop', '#reply', function(evt) { | ||
evt.preventDefault() | ||
evt.stopPropagation() | ||
|
||
$(this).trigger('change') // active textarea | ||
$('> .postContent', this).removeClass('upyun-drag-highlight') | ||
var $target = $('textarea', this) | ||
$.each(evt.originalEvent.dataTransfer.files, function(i, file) { | ||
return upload(file, $target) | ||
}) | ||
}) | ||
.on('dragover', function(evt) { | ||
evt.preventDefault() | ||
evt.originalEvent.dataTransfer.dropEffect = 'none' | ||
}) | ||
.on('enter dragover', '#reply', function(evt) { | ||
evt.preventDefault() | ||
evt.stopPropagation() | ||
evt.originalEvent.dataTransfer.dropEffect = 'copy' | ||
$('> .postContent', this).addClass('upyun-drag-highlight') | ||
}) | ||
.on('dragleave', '#reply', function(evt) { | ||
$('> .postContent', this).removeClass('upyun-drag-highlight') | ||
}) | ||
|
||
// clipboard upload | ||
$(document).on('paste', '#reply', function (evt) { | ||
var data = evt.originalEvent.clipboardData | ||
if (typeof data !== 'object') return | ||
|
||
var $target = $('textarea', this) | ||
$.each(data.items, function(i, item) { | ||
if (item.kind === 'file') { | ||
var file = item.getAsFile() | ||
file.name = file.name || 'screenshot.png' | ||
return upload(file, $target) | ||
} | ||
}) | ||
}) | ||
|
||
|
||
function upload(file, $target) { | ||
// TODO queue upload | ||
var deferred = getSign() | ||
.then(function (sign) { | ||
var data = new FormData | ||
data.append('file', file, file.name) | ||
data.append('policy', sign.policy) | ||
data.append('signature', sign.signature) | ||
|
||
return $.ajax('//v0.api.upyun.com/' + sign.bucket, { | ||
data: data, | ||
type: 'POST', | ||
processData: false, | ||
contentType: false, | ||
dataType: 'json', | ||
timeout: 1000 * 60 * 15 // 15 minutes | ||
}) | ||
.fail(function ($xhr, status, error) { | ||
if (status === 'timeout') { | ||
// TODO i18n | ||
ETMessages.showMessage('上传超时', 'warning') | ||
} else { | ||
ETMessages.showMessage('上传失败', 'warning') | ||
} | ||
}) | ||
.then(function (res) { | ||
return $.Deferred().resolve(res, sign.bucket) | ||
}) | ||
}) | ||
|
||
|
||
// ==== insert content | ||
|
||
var placeholder = '[img]上传中 ' + file.name + '…[/img]' | ||
|
||
var pos = $target.getSelection() | ||
var result = '' | ||
ETConversation.wrapText($target, placeholder, '', '', '') | ||
deferred | ||
.done(function (res, bucket) { | ||
result = '[url=//' + bucket + '.b0.upaiyun.com' + res.url + ']' | ||
// TODO thumb name setting | ||
// TODO https? | ||
+ '[img]//' + bucket + '.b0.upaiyun.com' + res.url + '!s[/img]' | ||
+ '[i]' + res['image-width'] + 'x' + res['image-height'] + '[/i]' | ||
+ '[/url]' | ||
}) | ||
.always(function () { | ||
var cpos = $target.getSelection() | ||
$target.val($target.val().replace(placeholder, result)) | ||
var x = cpos.end | ||
if ( cpos.end >= (pos.end + placeholder.length) ) { | ||
x = cpos.end + result.length - placeholder.length | ||
} else if (cpos.end > pos.end) { | ||
x = pos.end | ||
} | ||
// restore caret position | ||
$target.selectRange(x, x) | ||
}) | ||
} | ||
|
||
}(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
if (!defined("IN_ESOTALK")) exit; | ||
$form = $data["form"]; | ||
?> | ||
|
||
<?php echo $form->open(); ?> | ||
<div class='section'> | ||
<ul class='form'> | ||
<li> | ||
<label>Bucket</label> | ||
<?php echo $form->input("bucket", "text"); ?> | ||
</li> | ||
<li> | ||
<label>Secret</label> | ||
<?php echo $form->input("secret", "text"); ?> | ||
</li> | ||
</ul> | ||
</div> | ||
<div class='buttons'> | ||
<?php echo $form->saveButton("submit"); ?> | ||
</div> | ||
<?php echo $form->close(); ?> | ||
|