|
|
<?php
|
|
|
|
|
|
namespace app\base\controller\oss\admin;
|
|
|
|
|
|
use app\BaseController;
|
|
|
use think\App;
|
|
|
|
|
|
class Oss extends BaseController
|
|
|
{
|
|
|
/**
|
|
|
* 格式话上传文件名
|
|
|
* @param $name
|
|
|
* @return mixed
|
|
|
*/
|
|
|
private function uploadNameHandel($name)
|
|
|
{
|
|
|
//+过滤
|
|
|
$name = str_replace("+", "", $name);
|
|
|
//空格过滤
|
|
|
$name = str_replace(' ', '', $name);
|
|
|
return $name;
|
|
|
}
|
|
|
|
|
|
private function gmtIso8601($time)
|
|
|
{
|
|
|
$dtStr = date("c", $time);
|
|
|
$pos = strpos($dtStr, '+');
|
|
|
$expiration = substr($dtStr, 0, $pos);
|
|
|
return $expiration . ".000Z";
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 上传图片
|
|
|
* @date 2020-09-17
|
|
|
*/
|
|
|
public function getOssParam()
|
|
|
{
|
|
|
$is_original_name = input('post.is_original_name/d', 0);
|
|
|
$file_path = input('post.file_path/s', '');
|
|
|
$file_path = $this->uploadNameHandel($file_path);
|
|
|
|
|
|
$oss_param = get_ali_oss_config();
|
|
|
$oss_config = $oss_param;
|
|
|
|
|
|
$protocol = getProtocol();
|
|
|
$accessKeyId = $oss_config['access_key_id'];
|
|
|
$accessKeySecret = $oss_config['access_key_secret'];
|
|
|
$bucketDefaultDomain = $oss_config['bucket_default_url'];
|
|
|
$showUrlDomain = $oss_config['show_url'] . '/';
|
|
|
$projectName = config('database.connections.mysql.database');
|
|
|
|
|
|
|
|
|
if (empty($is_original_name)) {
|
|
|
$file_name = date('Ymd') . '/' . md5(microtime(true));
|
|
|
} else {
|
|
|
$file_name = '';
|
|
|
}
|
|
|
|
|
|
$callback_param = array(
|
|
|
'callbackUrl' => 'https://ym2.jucheng01.net/' . 'member/Oss/fileCallBack',
|
|
|
'callbackBody' => 'bucket=${bucket}&object=${object}&show_url_domain=${show_url_domain}',
|
|
|
'callbackBodyType' => "application/x-www-form-urlencoded"
|
|
|
);
|
|
|
//var_dump($callback_param);die;
|
|
|
$callback_string = json_encode($callback_param);
|
|
|
$base64_callback_body = base64_encode($callback_string);
|
|
|
|
|
|
$now = time();
|
|
|
$expire = 30; //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问。
|
|
|
$end = $now + $expire;
|
|
|
$expiration = $this->gmtIso8601($end);
|
|
|
|
|
|
$conditions = [
|
|
|
// 上传Object的最小和最大允许大小
|
|
|
array(0 => 'content-length-range', 1 => 0, 2 => 1048576000),//最大文件大小.用户可以自己设置
|
|
|
array(0 => 'starts-with', 1 => '$key', 2 => $projectName)// 表示用户上传的数据,必须是以$dir开始,不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录。
|
|
|
];
|
|
|
$policy = array('expiration' => $expiration, 'conditions' => $conditions);
|
|
|
$policy = json_encode($policy);
|
|
|
$base64_policy = base64_encode($policy);
|
|
|
|
|
|
$string_to_sign = $base64_policy;
|
|
|
$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $accessKeySecret, true));
|
|
|
|
|
|
$response = array();
|
|
|
$response['accessid'] = $accessKeyId;
|
|
|
$response['host'] = $bucketDefaultDomain;
|
|
|
$response['showUrlDomain'] = $showUrlDomain;
|
|
|
$response['policy'] = $base64_policy;
|
|
|
$response['signature'] = $signature;
|
|
|
$response['expire'] = $end;
|
|
|
$response['callback'] = $base64_callback_body;
|
|
|
$response['dir'] = empty($file_path) ? $projectName . '/uid' . 2 . '/member/' . $file_name : $file_path . date('YmdHis'); // 这个参数是设置用户上传文件时指定的前缀。
|
|
|
|
|
|
|
|
|
return sendSuccessMessage($response);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 本地图片转oss
|
|
|
* @param $param ['url'] 本地路径
|
|
|
* @date 2022-12-05
|
|
|
*/
|
|
|
public function ossload()
|
|
|
{
|
|
|
$param = input('post.');
|
|
|
$oss_param = get_ali_oss_config(2);
|
|
|
|
|
|
$oss_class = new \ali\oss\Oss($oss_param);
|
|
|
|
|
|
$oss_file_dirname = 'uid' . 2 . '/ueditor/' . date("Ymd");
|
|
|
|
|
|
$oss = $oss_class->uploadFile('.' . $param['url'], $oss_file_dirname);
|
|
|
$url = $oss['url'];
|
|
|
|
|
|
return sendSuccessMessage(['url' => $url]);
|
|
|
}
|
|
|
} |