input('scene', 'image'); $file = $request->file('file'); if ($file && $file->isValid()) { $pathUrl = '/uploads/'.$scene.'/'.date('Ymd').'/'.md5($file->getUploadName().time()).'.'.$file->getUploadExtension(); $fullPathUrl = public_path().$pathUrl; $file->move($fullPathUrl); $data = [ 'domain' => getenv('MAIN_DOMAIN'), 'file_url' => $pathUrl, 'full_url' => getenv('MAIN_DOMAIN').$pathUrl, 'scene' => $scene, ]; return $this->successResponse($data, 'upload success'); } return $this->errorResponse('file not found'); } catch (\Throwable $e) { return $this->errorResponse($e->getMessage()); } } // 上传单文件图片 public function uploadImage(Request $request) { try { $file = $request->file('image'); if (!$file || !$file->isValid()) { return $this->errorResponse('上传失败'); } $result = Storage::adapter('oss') ->path('vehicle_album/'.date('Ymd')) ->size(1024*1024*5) ->extYes(['image/jpeg','image/png']) ->extNo(['image/gif']) ->upload($file); $ossservice = new OssService(); $data = [ 'image_name' => $result->file_name, 'image_url' => $ossservice->generatePrivateUrl($result->file_name) ]; return $this->successResponse($data); }catch (\Throwable $e){ var_dump($e->getFile().'-'.$e->getLine().'-'.$e->getMessage()); return $this->errorResponse($e->getMessage()); } } public function proxy(Request $request) { $filePath = $request->get('file'); // 原OSS文件路径 try { // 生成签名URL $signedUrl = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($filePath); // 获取图片数据并返回 $imageData = file_get_contents($signedUrl); // 根据文件类型设置Content-Type $mime = mime_content_type('data://application/octet-stream;base64,'.base64_encode($imageData)); return response($imageData, 200, [ 'Content-Type' => $mime, 'Cache-Control' => 'public, max-age=600' // 客户端缓存10分钟 ]); } catch (\Exception $e) { return response('Image not found '.$e->getMessage(), 404); } } }