Commit 3994af3e authored by gxx's avatar gxx

Merge branch 'dev-master' of 120.27.221.179:gxx123/workbiu-admin into dev-master

parents e4cb6b33 5d678702
......@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Contracts\FileServiceContract;
use Illuminate\Http\Request;
class FileController extends Controller
{
......@@ -28,4 +29,13 @@ class FileController extends Controller
return view(route('500'));
}
}
public function deleteFormOss(Request $request)
{
$file_id = $request;
$result = $this->mFile->delete($file_id);
if($result){
return view(route('500'));
}
}
}
......@@ -12,10 +12,17 @@ namespace App\Services;
use App\Models\File;
use App\Contracts\FileServiceContract;
use App\Util\OssClient;
use App\Util\OssEngine;
class FileService implements FileServiceContract
{
public function __construct()
{
$this->ossClient = OssEngine::getClient();
}
public function getAll($group_id)
{
$files = File::where('group_id',$group_id)->get();
......@@ -35,6 +42,21 @@ class FileService implements FileServiceContract
{
$file =File::where(["id"=>file_id])->get();
$result = $file->delete();
// $pointer = $file->pointer;
// $count =File::where(["pointer"=>$pointer])->count();
// if($result && $count == 0){
// $ossClient = $this->ossClient;
// $bucket = 'workbiu-files'; //从数据库中获取
// $object = $pointer;
// if($this->doesObjectExist($bucket,$object)) {
// //从数据库中删除,再从oss删除
// if(true) {
// $ossClient->deleteObject($bucket, $object);
// }
// }else{
// return Message::makeError('文件不存在');
// }
// }
return $result;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: mirzat
* Date: 2017/10/24
* Time: 14:30
*/
namespace App\Util;
use Carbon\Carbon;
use DateTime;
use OSS\OssClient;
class OssEngine
{
private $id, $key, $host, $callBackUrl,$endPoint;
private $dir,$base64Policy;
public function __construct ()
{
$this->checkEnv();
}
public function checkEnv(){
$this->id = env('OSS_ID');
$this->key = env('OSS_KEY');
$this->host = env('OSS_HOST');
$this->endPoint = env('OSS_END_POINT');
}
public static function getClient($id=null,$key=null,$endKey=null){
return new OssClient(env('OSS_ID'),env('OSS_KEY'),env('OSS_END_POINT'));
}
public static function makeSignature($callBackUrl,$dir,$UserToken){
$engine = new OssEngine();
$engine->setCallBackUrl($callBackUrl);
$engine->setDir($dir);
$engine->setUserToken($UserToken);
return $engine->getFinalSignature();
}
public function getFinalSignature(){
$end = null;//这个变量经过getBase64Policy的应用后,会变成最新的时间。开始必须是NUll
$base64_policy = $this->getBase64Policy($end);
$base64_callback_body = $this->getBase64CallBackBody();
$signature = $this->getSignature();
$response = array();
$response['accessid'] = $this->id;
$response['host'] = $this->host;
$response['policy'] = $base64_policy;
$response['signature'] = $signature;
$response['expire'] = 0;
$response['callback'] = $base64_callback_body;
//这个参数是设置用户上传指定的前缀
$response['dir'] = $this->dir;
$result = json_encode($response);
return $result;
}
public function getBase64Policy(&$end){
$now = time();
$expire = 30; //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问
$end = $now + $expire;
$expiration = $this->gmt_iso8601($end);
$key = $this->key;
$start = array(0=>'starts-with', 1=>'$key', 2=>$this->dir);
$conditions[] = $start;
$arr = array('expiration'=>$expiration,'conditions'=>$conditions);
$policy = json_encode($arr);
$base64_policy = base64_encode($policy);
$this->base64Policy = $base64_policy;
return $base64_policy;
}
public function getSignature(){
$key = $this->key;
$policy = $this->base64Policy;
$signature = base64_encode(hash_hmac('sha1',$policy, $key, true));
return $signature;
}
private function getBase64CallBackBody(){
$UserToken = $this->getUserToken();
$callbackUrl = $this->callBackUrl;
$randomToken = bcrypt("gxx");
$callback_param = array('callbackUrl'=>$callbackUrl,
'callbackBody'=>'filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}&userToken='.$UserToken.'&randomToken=${'.$randomToken.'}',
'callbackBodyType'=>"application/x-www-form-urlencoded");
$callback_string = json_encode($callback_param);
$base64_callback_body = base64_encode($callback_string);
return $base64_callback_body;
}
public function getId ()
{
return $this->id;
}
public function setId ($id)
{
$this->id = $id;
}
public function getCallBackUrl ()
{
return $this->callBackUrl;
}
public function setCallBackUrl ($callBackUrl)
{
$this->callBackUrl = $callBackUrl;
}
public function getDir ()
{
return $this->dir;
}
public function setDir ($dir)
{
$this->dir = $dir;
}
private function gmt_iso8601 ($time)
{
$dtStr = date("c", $time);
$mydatetime = new DateTime($dtStr);
$expiration = $mydatetime->format(DateTime::ISO8601);
$pos = strpos($expiration, '+');
$expiration = substr($expiration, 0, $pos);
return $expiration . "Z";
}
/**
* @param mixed $base64Policy
*/
public function setBase64Policy ($base64Policy)
{
$this->base64Policy = $base64Policy;
}
/**
* @return mixed
*/
public function getEndPoint ()
{
return $this->endPoint;
}
/**
* @param mixed $endPoint
*/
public function setEndPoint ($endPoint)
{
$this->endPoint = $endPoint;
}
public function setUserToken($UserToken)
{
$this->UserToken = $UserToken;
}
public function getUserToken()
{
return $this->UserToken;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment