/** * 腾讯云Cos上传文件 * 上传分片 * 合并分片 * @param File $file 文件流 */ public function uploadCos() { if (!Auth::isModuleAllow()) { $this->error("请登录后再进行操作"); } $config = get_addon_config('cos'); $this->cosConfig = array( 'region' => $config['region'], 'schema' => 'https', //协议头部,默认为http 'credentials' => array( 'secretId' => $config['secretId'], 'secretKey' => $config['secretKey'] ) ); $oss = new Client($this->cosConfig); //检测删除文件或附件 $checkDeleteFile = function ($attachment, $upload, $force = false) use ($config) { //如果设定为不备份则删除文件和记录 或 强制删除 if ((isset($config['serverbackup']) && !$config['serverbackup']) || $force) { if ($attachment && !empty($attachment['id'])) { $attachment->delete(); } if ($upload) { //文件绝对路径 $filePath = $upload->getFile()->getRealPath() ?: $upload->getFile()->getPathname(); @unlink($filePath); } } }; $chunkid = $this->request->post("chunkid"); if ($chunkid) { $action = $this->request->post("action"); $chunkindex = $this->request->post("chunkindex/d"); $chunkcount = $this->request->post("chunkcount/d"); $filesize = $this->request->post("filesize"); $filename = $this->request->post("filename"); $method = $this->request->method(true); $key = $this->request->post("key"); $uploadId = $this->request->post("uploadId"); if ($action == 'merge') { $attachment = null; $upload = null; //合并分片 if ($config['uploadmode'] == 'server') { //合并分片文件 try { $upload = new Upload(); $attachment = $upload->merge($chunkid, $chunkcount, $filename); } catch (UploadException $e) { $this->error($e->getMessage()); } } $etags = $this->request->post("etags/a", []); if (count($etags) != $chunkcount) { $checkDeleteFile($attachment, $upload, true); $this->error("分片数据错误"); } $listParts = []; for ($i = 0; $i < $chunkcount; $i++) { $listParts[] = array("PartNumber" => $i + 1, "ETag" => $etags[$i]); } try { $result = $oss->completeMultipartUpload(array( 'Bucket' => $config['bucket'], 'Key' => $key, 'UploadId' => $uploadId, 'Parts' => $listParts ) ); } catch (\Exception $e) { $checkDeleteFile($attachment, $upload, true); $this->error($e->getMessage()); } if (!isset($result['Key'])) { $checkDeleteFile($attachment, $upload, true); $this->error("上传失败"); } else { $checkDeleteFile($attachment, $upload); $this->success("上传成功", '', ['url' => "/" . $key, 'fullurl' => cdnurl("/" . $key, true)]); } } else { //默认普通上传文件 $file = $this->request->file('file'); try { $upload = new Upload($file); $file = $upload->chunk($chunkid, $chunkindex, $chunkcount); } catch (UploadException $e) { $this->error($e->getMessage()); } try { $params = array( 'Bucket' => $config['bucket'], 'Key' => $key, 'UploadId' => $uploadId, 'PartNumber' => $chunkindex + 1, 'Body' => $file->fread($file->getSize()) ); $ret = $oss->uploadPart($params); $etag = $ret['ETag']; } catch (\Exception $e) { $this->error($e->getMessage()); } $this->success("上传成功", "", [], 3, ['ETag' => $etag]); } } else { $attachment = null; //默认普通上传文件 $file = $this->request->file('file'); try { $upload = new Upload($file); $attachment = $upload->upload(); } catch (UploadException $e) { $this->error($e->getMessage()); } //文件绝对路径 $filePath = $upload->getFile()->getRealPath() ?: $upload->getFile()->getPathname(); $url = $attachment->url; try { $ret = $oss->upload($config['bucket'], ltrim($attachment->url, "/"), $upload->getFile()); //成功不做任何操作 } catch (\Exception $e) { $checkDeleteFile($attachment, $upload, true); $this->error("上传失败" . $e->getMessage()); } $checkDeleteFile($attachment, $upload); // 记录云存储记录 $data = $attachment->toArray(); unset($data['id']); $data['storage'] = 'cos'; Attachment::create($data, true); $this->success("上传成功", ['url' => $url, 'fullurl' => cdnurl($url, true)]); } return; }
标签:fastadmin,request,upload,api,attachment,file,post,config From: https://www.cnblogs.com/web928943/p/17984350