kindeditor joins the example explanation uploaded by Qiniuyun

  • 2021-08-12 02:22:39
  • OfStack

There are two main types of Niuyun uploads:

Server-side upload

The front end uploads, and the front end is divided into two return modes:

1). Redirect back, which can solve the cross-domain problem of ajax

2) Callback, 7 Niu Cloud first returns data to the server, and then 7 Niu Cloud returns to the front end to solve the request mode that does not support redirection, such as uploading small programs

This time, 7 Niuyun php sdk; is used;


composer require qiniu/php-sdk

Adding config. php under Kindeditor/php is mainly a configuration parameter


<?php
error_reporting(0);
 
defined('ROOT_PATH') || define('ROOT_PATH', dirname(__DIR__).'/');
defined('QINIU_ACCESS_KEY') || define('QINIU_ACCESS_KEY', '');
defined('QINIU_SECRET_KEY') || define('QINIU_SECRET_KEY', '');
defined('QINIU_TEST_BUCKET') || define('QINIU_TEST_BUCKET', '7 Niu cloud hosting name ');
defined('QINIU_BUCKET_DOMAIN') || define('QINIU_BUCKET_DOMAIN', '7 Niu cloud hosting website ');
 
defined('CALLBACK_URL') || define('CALLBACK_URL', ' Domain name /kindeditor/php/callBack.php');
defined('RETURN_URL') || define('RETURN_URL', ' Domain name /kindeditor/php/returnBack.php');
 
require_once ROOT_PATH."vendor/autoload.php";

Adding qiniu_token. php under Kindeditor/php is mainly to generate token for upload


<?php
use Qiniu\Auth;
 
require_once __DIR__."/config.php";
 
//  Constructing Authentication Object 
$auth = new Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY);
 
$data = [
  'returnUrl' => RETURN_URL,
];
if (isset($_REQUEST['is_call'])) {
  $data = [
   'callbackUrl' => CALLBACK_URL,
   'callbackBody' => 'key=$(key)&hash=$(etag)&w=$(imageInfo.width)&h=$(imageInfo.height)'
  ];
}
//  Generate upload  Token
$token = $auth->uploadToken(QINIU_TEST_BUCKET, null, 3600, $data);
 
echo json_encode([
  'error' => 0,
  'token' => $token
]);

Adding callBack under Kindeditor/php. php is mainly called back


<?php
use Qiniu\Auth;
 
require_once __DIR__."/config.php";
$_body = file_get_contents('php://input');
$auth = new Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY);
// Callback contentType
$contentType = 'application/x-www-form-urlencoded';
// The signature information of the callback, which can verify whether the callback comes from 7 Cattle 
$authorization = $_SERVER['HTTP_AUTHORIZATION'];
$isQiniuCallback = $auth->verifyCallback($contentType, $authorization, CALLBACK_URL, $_body);
if (!$isQiniuCallback) {
  echo json_encode([
    'error' => 2,
    'message' => ' Validation failed '
  ]);
  die();
}
 
$body = $_POST;
$qiniu_url = QINIU_BUCKET_DOMAIN;
if (!empty($body['key'])) {
  echo json_encode([
    'error' => 0,
    'url' => $qiniu_url.$body['key']
  ]);
  die();
}
echo json_encode([
  'error' => 1,
  'message' => ' Outgoing error on video '
]);

Adding returnBack under Kindeditor/php. php is mainly to redirect the receiving address


<?php
use Qiniu\Auth;
 
require_once __DIR__."/config.php";
$upload_ret = base64_decode($_GET['upload_ret']);
$upload_ret = json_decode($upload_ret, true);
$qiniu_url = QINIU_BUCKET_DOMAIN;
if (!empty($upload_ret['key'])) {
  echo json_encode([
    'error' => 0,
    'url' => $qiniu_url.$upload_ret['key']
  ]);
  die();
}
echo json_encode([
  'error' => 1,
  'message' => ' Outgoing error on video '
]);

Next is the front-end change. When I change it, the video is uploaded

Kindeditor/plugins/media/media.js


KindEditor.plugin('media', function(K) {
  var self = this, name = 'media', lang = self.lang(name + '.'),
    allowMediaUpload = K.undef(self.allowMediaUpload, true),
    allowFileManager = K.undef(self.allowFileManager, false),
    formatUploadUrl = K.undef(self.formatUploadUrl, true),
    extraParams = K.undef(self.extraFileUploadParams, {
      'token': ''// Add token
    }),
    filePostName = K.undef(self.filePostName, 'file'), // Change file upload name 
    uploadJson = K.undef(self.uploadJson, 'https://up.qbox.me'); // Change the upload address, when I use it, the space in East China is used https
 
     
    ....
 
      function getQToken() {
        $.getJSON('/includes/kindeditor/php/qiniu_token.php', function (data) {
          K('[name="token"]', div).val(data.token);
        });
      }
            //  Get settings upload token
      getQToken();
 
      if (allowMediaUpload) {
        var uploadbutton = K.uploadbutton({
          button : K('.ke-upload-button', div)[0],
          fieldName : filePostName,
          extraParams : extraParams,
          url : uploadJson,// Remove Added Parameters 
          afterUpload : function(data) {
      ...
});

This will enable you to upload videos to 7 Niuyun.


Related articles: