123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- <?php
- namespace app\oss\controller;
- use app\common\service\HelperService;
- use app\oss\service\OssServiceV2;
- use Exception;
- use think\Config;
- use think\Controller;
- use think\Validate;
- class File extends Controller {
- private static $fileType = array(
- 'image' => 1,
- 'audio' => 2,
- 'video' => 3,
- 'text' => 4,
- 'file' => 5,
- 'pdf' => 6
- );
- public function _initialize() {
- parent::_initialize();
- }
- private function setConfig($bucket){
- switch($bucket){
- case 'viva-private':
- Config::set('OSS_ACCESS_KEY_ID','LTAI4G9Zz1yWLASYkBmXRFF6');
- Config::set('ACCESS_KEY_SECRET','pnMsp9wlTEo4lP0JQgVukMGURlcC7y');
- Config::set('OSS_ENDPOINT','oss-cn-hangzhou.aliyuncs.com');
- break;
- default:
- throw new \RuntimeException('无此bucket');
- }
- }
- public function newDiffParam($rule, $params) {
- $validate = new Validate($rule);
- if (!$validate->check($params)) {
- HelperService::returnJson(['code'=>400,'msg'=>$validate->getError()]);
- }
- }
- /**
- * 图片上传
- */
- public function upload() {
- header('Access-Control-Allow-Origin:*');
- header("Access-Control-Allow-Methods", "*");
- header("Access-Control-Allow-Headers", "Content-Type,XFILENAME,XFILECATEGORY,XFILESIZE");
- $params = $this->request->param();
- $rule = [
- 'fileName' => 'require',
- 'upType' => 'require',
- 'isRoot' => 'require',
- 'fileType' => 'require|number',
- 'bucket' => 'require',
- 'isPrivate' => 'boolean'
- ];
- if (isset($params['upType']) && $params['upType'] === 'base64') {
- $rule['content'] = 'require';
- }
- $this->newDiffParam($rule, $params);
- $fileName = (string)$params['fileName'];
- $upType = (string)$params['upType'];
- $isRoot = (string)$params['isRoot'];
- $fileType = (string)$params['fileType'];
- $isPrivate = isset($params['isPrivate']) ?$params['isPrivate']: true;
- $bucket = $params['bucket'];
- $this->setConfig($bucket);
- Config::set('ALIYUN_OSS_BUCKET', $bucket);
- $ossService = new OssServiceV2($bucket);
- $ext = !empty($fileName) ? (string)pathinfo($fileName, PATHINFO_EXTENSION) : '';
- list($fileName, $realContent) = $ossService->getFileNameAndRealContent($upType, $params, $fileName, $ext);
- //向阿里推送图片
- $type = (array)array_flip(self::$fileType);
- if(empty($type[$fileType])){
- HelperService::returnJson(['code'=>400,'msg'=>'文件类型不正确']);
- }
- $fileSavePath = $isRoot ? ltrim($fileName, '/') : $type[$fileType] . '/' . date('Ym') . '/' . $fileName;
- //重试3次
- $times = 3;
- $message = '';
- while ($times--) {
- try {
- if (true === $ossService->fileExist($fileSavePath)) {
- $path = (string)pathinfo($fileSavePath, PATHINFO_DIRNAME);
- $path = ($path === '.' || $path === '..') ? '' : $path;
- $fileName = pathinfo($fileSavePath, PATHINFO_BASENAME);
- $ext = pathinfo($fileSavePath, PATHINFO_EXTENSION);
- $fileName = substr($fileName, 0, strpos($fileName, '.' . $ext));
- $newFileName = $fileName . '_' . date('YmdHis') . mt_rand(10000, 99999) . '.' . $ext;
- $fileSavePath = ltrim($path . '/' . $newFileName, '/');
- }
- $result = $ossService->fileUpload($fileSavePath, $realContent);
- if ($isPrivate === true) {
- $ossService->putObjectAcl($result, 'private');
- }
- if ($result) {
- $aLiYunUrl = 'http://' . $bucket . '.' . Config::get('OSS_ENDPOINT') . '/' . trim($result, '/');
- HelperService::returnJson(['data'=>[
- 'url' => $result,
- 'bucket' => $bucket,
- 'aliyunUrl' => $aLiYunUrl,
- 'signUrl' => $ossService->getSignUrl([
- 'object' => $result,
- 'bucket' => $bucket
- ])
- ],'msg'=>'success','code'=>200]);
- } else {
- HelperService::returnJson(['data'=>['times' => $times, 'bucket' => $bucket], 'msg'=>'fail','code'=>400]);
- }
- } catch (Exception $ex) {
- $message = $ex->getMessage();
- continue;
- }
- }
- HelperService::returnJson(['data'=>['times' => $times, 'bucket' => $bucket], 'msg'=>'上传失败: ' . $message,'code'=>400]);
- }
- protected function input() {
- $contents = file_get_contents('php://input');
- $param = json_decode($contents, true);
- if (empty($param))
- return $this->output([], 'json error', '0004');
- return $param;
- }
- public function output($data=[],$msg='success',$code=200){
- HelperService::returnJson([
- 'data'=>$data,
- 'msg'=>$msg,
- 'code'=>$code
- ]);
- }
- /**
- * 下载文件
- */
- public function downFile() {
- $params = $this->input();
- $this->newDiffParam([
- 'filePath' => 'require',
- 'bucket' => 'require'
- ], $params);
- $this->setConfig($params['bucket']);
- Config::set('ALIYUN_OSS_BUCKET', (string)($params['bucket']));
- $res = (new OssServiceV2((string)($params['bucket'])))->downFile((string)($params['filePath']));
- die($res);
- }
- /**
- * 文件删除
- */
- public function fileDelete() {
- $params = $this->input();
- $this->newDiffParam([
- 'filePath' => 'require',
- 'bucket' => 'require'
- ], $params);
- $filePath = (string)($params['filePath']);
- $this->setConfig($params['bucket']);
- Config::set('ALIYUN_OSS_BUCKET', (string)($params['bucket']));
- $ossService = new OssServiceV2((string)($params['bucket']));
- //重试3次
- $times = 3;
- $message = '';
- while ($times--) {
- try {
- $exist = $ossService->fileExist($filePath);
- if (true !== $exist) {
- $this->output([], 'filePath is not exist');
- }
- $result = $ossService->fileDelete($filePath);
- if ($result) {
- $this->output([], 'del success');
- } else {
- $this->output(['times' => $times], 'del fail');
- }
- } catch (Exception $ex) {
- $message = $ex->getMessage();
- continue;
- }
- }
- $this->output(['times' => $times], 'del fail: ' . $message);
- }
- /**
- * 图片信息
- */
- public function imageInfo() {
- $params = $this->input();
- $this->newDiffParam([
- 'imagePath' => 'require',
- 'ossDomain' => 'require',
- 'bucket' => 'require',
- ], $params);
- $imagePath = (string)($params['imagePath']);
- Config::set('ALIYUN_OSS_BUCKET', (string)($params['bucket']));
- $this->setConfig($params['bucket']);
- $ossService = new OssServiceV2((string)($params['bucket']));
- //重试3次
- $times = 3;
- $message = '';
- while ($times--) {
- try {
- $exist = (bool) $ossService->fileExist($imagePath);
- if (true !== $exist) {
- $this->output([], 'image is not exist');
- }
- $result = $ossService->imageInfo($imagePath, (string)($params['ossDomain']));
- if ($result) {
- $this->output($result, 'success');
- } else {
- $this->output(['times' => $times], 'fail');
- }
- } catch (Exception $ex) {
- $message = $ex->getMessage();
- continue;
- }
- }
- $this->output(['times' => $times], 'fail: ' . $message);
- }
- /**
- * 文件是否存在
- */
- public function fileExist() {
- $params = $this->input();
- $this->newDiffParam([
- 'filePath' => 'require',
- 'bucket' => 'require'
- ], $params);
- $filePath = (string)($params['filePath']);
- Config::set('ALIYUN_OSS_BUCKET', (string)($params['bucket']));
- $this->setConfig($params['bucket']);
- $ossService = new OssServiceV2((string)($params['bucket']));
- //重试3次
- $times = 3;
- $message = '';
- while ($times--) {
- try {
- $result = $ossService->fileExist($filePath);
- if ($result) {
- $this->output([], 'success');
- } else {
- $this->output(['times' => $times], 'fail');
- }
- } catch (Exception $ex) {
- $message = $ex->getMessage();
- continue;
- }
- }
- $this->output(['times' => $times], 'fail: ' . $message);
- }
- /**
- * bucket列表
- */
- public function bucketList() {
- $ossService = new OssServiceV2();
- //重试3次
- $times = 3;
- $message = '';
- while ($times--) {
- try {
- $bucketList = $ossService->bucketList();
- $this->output($bucketList, 'success');
- } catch (Exception $ex) {
- $message = $ex->getMessage();
- continue;
- }
- }
- $this->output(['times' => $times], 'fail: ' . $message);
- }
- /**
- * 获取带有签名的路径
- */
- public function getSignUrl() {
- $params = $this->input();
- $this->newDiffParam([
- 'object' => 'require',
- 'bucket' => 'require',
- 'isIntranet' => 'boolean' //是否是内网
- ], $params);
- $this->setConfig($params['bucket']);
- $url = (new OssServiceV2($params['bucket']))->getSignUrl($params);
- $this->output(['url' => $url]);
- }
- /**
- * 批量获取带有签名的路径
- */
- public function getMultiSignUrl() {
- $params = $this->input();
- $this->newDiffParam(['data' => 'require|array'], $params);
- $data = (new OssServiceV2())->getMultiSignUrl($params['data']);
- $this->output(['urls' => $data]);
- }
- /**
- * 获取阿里云处理后的图片
- */
- public function getProcessImageUrl() {
- $params = $this->input();
- $this->newDiffParam([
- 'process' => 'require',
- 'object' => 'require',
- 'bucket' => 'require',
- 'endpoint' => 'require',
- ], $params);
- $this->setConfig($params['bucket']);
- $objectInfo = parse_url($params['object']);
- if (empty($objectInfo['path'])) {
- $this->output([], '传入的地址信息有误');
- }
- $params['path'] = $objectInfo['path'];
- $data = (new OssServiceV2($params['bucket']))->getProcessImageUrl($params);
- $this->output(['url' => $data]);
- }
- }
|