more image upload api
This commit is contained in:
parent
89656306f4
commit
53bdfb9c3e
|
@ -7,12 +7,13 @@
|
|||
* @package Sakura
|
||||
*/
|
||||
|
||||
define( 'SAKURA_VERSION', '3.2.8' );
|
||||
define( 'SAKURA_VERSION', '3.3.0' );
|
||||
define( 'BUILD_VERSION', '3' );
|
||||
define( 'JSDELIVR_VERSION', '3.6.7' );
|
||||
|
||||
//ini_set('display_errors', true);
|
||||
//error_reporting(E_ALL);
|
||||
error_reporting(E_ALL ^ E_NOTICE);
|
||||
|
||||
if ( !function_exists( 'akina_setup' ) ) :
|
||||
/**
|
||||
|
|
BIN
inc/QQWry.Dat
BIN
inc/QQWry.Dat
Binary file not shown.
138
inc/api.php
138
inc/api.php
|
@ -28,24 +28,72 @@ function upload_image(WP_REST_Request $req)
|
|||
* -F "cmt_img_file=@screenshot.jpg" \
|
||||
* https://dev.2heng.xin/wp-json/sakura/v1/image/upload
|
||||
*/
|
||||
$file = $req->get_file_params();
|
||||
$image = file_get_contents($_FILES["cmt_img_file"]["tmp_name"]);
|
||||
|
||||
// $file = $req->get_file_params();
|
||||
|
||||
switch (akina_option("img_upload_api")) {
|
||||
case 'imgur':
|
||||
$API_Request = Imgur_API($image);
|
||||
break;
|
||||
case 'smms':
|
||||
$API_Request = SMMS_API($image);
|
||||
break;
|
||||
case 'imgur':
|
||||
$image = file_get_contents($_FILES["cmt_img_file"]["tmp_name"]);
|
||||
$API_Request = Imgur_API($image);
|
||||
break;
|
||||
case 'smms':
|
||||
$image = $_FILES;
|
||||
$API_Request = SMMS_API($image);
|
||||
break;
|
||||
case 'chevereto':
|
||||
$image = file_get_contents($_FILES["cmt_img_file"]["tmp_name"]);
|
||||
$API_Request = Chevereto_API($image);
|
||||
break;
|
||||
}
|
||||
|
||||
$result = new WP_REST_Response($API_Request, 200);
|
||||
$result->set_headers(array('Content-Type' => 'application/json',
|
||||
'Cache-Control' => 'max-age=3600')); // json 缓存控制
|
||||
$result = new WP_REST_Response($API_Request, $API_Request->status);
|
||||
$result->set_headers(array('Content-Type' => 'application/json'));
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Chevereto upload interface
|
||||
*/
|
||||
function Chevereto_API($image)
|
||||
{
|
||||
$fields = array(
|
||||
'source' => base64_encode($image),
|
||||
'key' => akina_option('chevereto_api_key')
|
||||
);
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, akina_option('cheverto_url').'/api/1/upload');
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
|
||||
|
||||
$reply = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
||||
$reply = json_decode($reply);
|
||||
|
||||
if ($reply->status_txt == 'OK' && $reply->status_code == 200) {
|
||||
$status = 200;
|
||||
$success = true;
|
||||
$message = "success";
|
||||
$link = $reply->image->image->url;
|
||||
$proxy = akina_option('cmt_image_proxy') . $link;
|
||||
} else {
|
||||
$status = $reply->status_code;
|
||||
$success = false;
|
||||
$message = $reply->error->message;
|
||||
$link = 'https://view.moezx.cc/images/2019/10/28/default_d_h_large.gif';
|
||||
$proxy = akina_option('cmt_image_proxy') . $link;
|
||||
}
|
||||
$output = array(
|
||||
'status' => $status,
|
||||
'success' => $success,
|
||||
'message' => $message,
|
||||
'link' => $link,
|
||||
'proxy' => $proxy,
|
||||
);
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Imgur upload interface
|
||||
*/
|
||||
|
@ -67,17 +115,20 @@ function Imgur_API($image)
|
|||
|
||||
if ($reply->success && $reply->status == 200) {
|
||||
$status = 200;
|
||||
$success = true;
|
||||
$message = "success";
|
||||
$link = $reply->data->link;
|
||||
$proxy = akina_option('cmt_image_proxy') . $link;
|
||||
} else {
|
||||
$status = $reply->status;
|
||||
$success = false;
|
||||
$message = $reply->data->error;
|
||||
$link = 'https://view.moezx.cc/images/2019/10/28/default_d_h_large.gif';
|
||||
$proxy = akina_option('cmt_image_proxy') . $link;
|
||||
}
|
||||
$output = array(
|
||||
'status' => $status,
|
||||
'success' => $success,
|
||||
'message' => $message,
|
||||
'link' => $link,
|
||||
'proxy' => $proxy,
|
||||
|
@ -85,17 +136,36 @@ function Imgur_API($image)
|
|||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* smms upload interface
|
||||
*/
|
||||
function SMMS_API($image)
|
||||
{
|
||||
$client_id = akina_option('smms_client_id');
|
||||
|
||||
$filename = $image['cmt_img_file']['name'];
|
||||
$filedata = $image['cmt_img_file']['tmp_name'];
|
||||
$filesize = $image['cmt_img_file']['size'];
|
||||
|
||||
$url = "https://sm.ms/api/v2/upload";
|
||||
$headers = array();
|
||||
array_push($headers, "Content-Type: multipart/form-data");
|
||||
array_push($headers, "Authorization: Basic " . $client_id);
|
||||
array_push($headers, "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97");
|
||||
|
||||
$finfo = new \finfo(FILEINFO_MIME_TYPE);
|
||||
$mimetype = $finfo->file($filedata);
|
||||
|
||||
$fields = array('smfile' => curl_file_create($filedata, $mimetype, $filename));
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, 'https://sm.ms/api/v2/upload');
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . $client_id));
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type ' . 'multipart/form-data'));
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, array('smfile' => $image));
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
|
||||
|
||||
$reply = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
@ -103,20 +173,30 @@ function SMMS_API($image)
|
|||
$reply = json_decode($reply);
|
||||
|
||||
if ($reply->success && $reply->code == 'success') {
|
||||
$status = 200;
|
||||
$message = $reply->message;
|
||||
$link = $reply->data->url;
|
||||
$proxy = akina_option('cmt_image_proxy') . $link;
|
||||
$status = 200;
|
||||
$success = true;
|
||||
$message = $reply->message;
|
||||
$link = $reply->data->url;
|
||||
$proxy = akina_option('cmt_image_proxy') . $link;
|
||||
} else if (preg_match("/Image upload repeated limit/i", $reply->message, $matches)) {
|
||||
$status = 200; // sm.ms 接口不规范,建议检测到重复的情况下返回标准化的 code,并单独把 url 放进一个字段
|
||||
$success = true;
|
||||
$message = $reply->message;
|
||||
$link = str_replace('Image upload repeated limit, this image exists at: ', '', $reply->message);
|
||||
$proxy = akina_option('cmt_image_proxy') . $link;
|
||||
} else {
|
||||
$status = 0; // sm.ms 接口不规范,谁给提个意见?我要状态码!
|
||||
$message = $reply->message;
|
||||
$link = 'https://view.moezx.cc/images/2019/10/28/default_d_h_large.gif';
|
||||
$proxy = akina_option('cmt_image_proxy') . $link;
|
||||
$status = 400;
|
||||
$success = false;
|
||||
$message = $reply->message;
|
||||
$link = 'https://view.moezx.cc/images/2019/10/28/default_d_h_large.gif';
|
||||
$proxy = akina_option('cmt_image_proxy') . $link;
|
||||
}
|
||||
$output = array('status' => $status,
|
||||
'message' => $message,
|
||||
'link' => $link,
|
||||
'proxy' => $proxy,
|
||||
$output = array(
|
||||
'status' => $status,
|
||||
'success' => $success,
|
||||
'message' => $message,
|
||||
'link' => $link,
|
||||
'proxy' => $proxy,
|
||||
);
|
||||
return $output;
|
||||
}
|
||||
|
|
BIN
inc/test.jpg
BIN
inc/test.jpg
Binary file not shown.
Before Width: | Height: | Size: 32 KiB |
31
inc/test.php
31
inc/test.php
|
@ -1,31 +0,0 @@
|
|||
<?php
|
||||
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
$image = file_get_contents("test.jpg");
|
||||
|
||||
function Imgur_API($image) {
|
||||
$client_id = "98cd21cdfc58130";
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, 'https://api.mashiro.top/imgur-api/3/image');
|
||||
curl_setopt($ch, CURLOPT_POST, TRUE);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode($image)));
|
||||
|
||||
$reply = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
||||
$reply = json_decode($reply);
|
||||
var_dump($reply);
|
||||
printf('<img height="180" src="%s" >', $reply->data->link);
|
||||
$res = $reply->data->link;
|
||||
$res = 'https://images.weserv.nl/?url='.$res;
|
||||
echo $res;
|
||||
printf('<img height="180" src="%s" >', $res);
|
||||
}
|
||||
|
||||
Imgur_API($image);
|
|
@ -196,11 +196,15 @@ function attach_image() {
|
|||
setTimeout(function () {
|
||||
cached.html('<i class="fa fa-picture-o" aria-hidden="true"></i>');
|
||||
}, 1000);
|
||||
var get_the_url = res.proxy;
|
||||
$('#upload-img-show').append('<img class="lazyload upload-image-preview" src="https://cdn.jsdelivr.net/gh/moezx/cdn@3.0.2/img/svg/loader/trans.ajax-spinner-preloader.svg" data-src="' + get_the_url + '" onclick="window.open(\'' + get_the_url + '\')" onerror="imgError(this)" />');
|
||||
lazyload();
|
||||
addComment.createButterbar("图片上传成功~<br>Uploaded successfully~");
|
||||
grin(get_the_url, type = 'Img');
|
||||
if (res.status == 200) {
|
||||
var get_the_url = res.proxy;
|
||||
$('#upload-img-show').append('<img class="lazyload upload-image-preview" src="https://cdn.jsdelivr.net/gh/moezx/cdn@3.0.2/img/svg/loader/trans.ajax-spinner-preloader.svg" data-src="' + get_the_url + '" onclick="window.open(\'' + get_the_url + '\')" onerror="imgError(this)" />');
|
||||
lazyload();
|
||||
addComment.createButterbar("图片上传成功~<br>Uploaded successfully~");
|
||||
grin(get_the_url, type = 'Img');
|
||||
} else {
|
||||
addComment.createButterbar("上传失败!<br>Uploaded failed!<br> 文件名/Filename: "+f.name+"<br>code: "+res.status+"<br>"+res.message, 3000);
|
||||
}
|
||||
},
|
||||
error: function (jqXHR, textStatus, errorThrown) {
|
||||
cached.html('<i class="fa fa-times" aria-hidden="true" style="color:red"></i>');
|
||||
|
|
17
options.php
17
options.php
|
@ -1012,7 +1012,8 @@ function optionsframework_options() {
|
|||
'type' => "radio",
|
||||
'options' => array(
|
||||
'imgur' => __('Imgur (https://imgur.com)', 'sakura'),
|
||||
'smms' => __('SM.MS (https://sm.ms)', 'sakura')
|
||||
'smms' => __('SM.MS (https://sm.ms)', 'sakura'),
|
||||
'chevereto' => __('Chevereto (https://chevereto.com)', 'sakura')
|
||||
));
|
||||
|
||||
$options[] = array(
|
||||
|
@ -1029,6 +1030,20 @@ function optionsframework_options() {
|
|||
'std' => '',
|
||||
'type' => 'text');
|
||||
|
||||
$options[] = array(
|
||||
'name' => __('Chevereto API v1 key', 'sakura'),
|
||||
'desc' => __('Get your API key here '.akina_option('cheverto_url').'/dashboard/settings/api', 'sakura'),
|
||||
'id' => 'chevereto_api_key',
|
||||
'std' => '',
|
||||
'type' => 'text');
|
||||
|
||||
$options[] = array(
|
||||
'name' => __('Chevereto URL', 'sakura'),
|
||||
'desc' => __('Your Chevereto homepage url, no slash in the end, eg. https://your.cherverto.com', 'sakura'),
|
||||
'id' => 'cheverto_url',
|
||||
'std' => 'https://your.cherverto.com',
|
||||
'type' => 'text');
|
||||
|
||||
$options[] = array(
|
||||
'name' => __('Comment images proxy', 'sakura'),
|
||||
'desc' => __('A front-ed proxy for the uploaded images. Leave it blank if you do not need.', 'sakura'),
|
||||
|
|
|
@ -5,7 +5,7 @@ Theme URI: https://2heng.xin/theme-sakura/
|
|||
Author: Mashiro, Louie, Fuzzz
|
||||
Author URI: http://2heng.xin
|
||||
Description: A branch of theme Akina
|
||||
Version: 3.2.8
|
||||
Version: 3.3.0
|
||||
License: GNU General Public License v2 or later
|
||||
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
Text Domain: sakura
|
||||
|
|
Loading…
Reference in New Issue