isCompress = $system['compress'] ?? true; // } $this->validate = new VUpload(); $this->uploadPath = Config::get('filesystem.disks.local.url'); $this->videoUploadPath = Config::get('filesystem.disks.video.url'); if (!is_writable(app()->getRootPath().'public'.$this->uploadPath)) { mkdir(app()->getRootPath().'public'.$this->uploadPath, 0777, true); } $this->uploadPathIsWritable = 1; if (!is_writable(app()->getRootPath().'public'.$this->videoUploadPath)) { mkdir(app()->getRootPath().'public'.$this->videoUploadPath, 0777, true); } $this->videoUploadPathIsWritable = 1; } /** * 通用文件上传 * @return Json */ public function file() { $file = request()->file('file'); if (empty($file)) { return $this->json(4001, '请上传的文件'); } if ($this->validate->checkFile($file)) { try { if (!$this->uploadPathIsWritable) { throw new \Exception('上传文件夹需要写入权限'); } $src = Filesystem::putFile('files/'.date('Ym'), $file, 'uniqid'); $src = $this->uploadPath.'/'.$src; $return['src'] = $src; $return['name'] = $file->getOriginalName(); //加入上传文件表 File::add($file, $src, $file->md5()); } catch (\Exception $e) { return $this->json(4003, $e->getMessage()); } return $this->json(0, 'success', $return); } else { $errorMsg = Lang::get($this->validate->getError()); return $this->json(4002, $errorMsg); } } /** * 通用图片上传 * @return Json */ public function image() { $image = request()->file('image'); if (empty($image)) { return $this->json(4001, '请上传图片文件'); } if (!in_array($image->getMime(), ['image/png', 'image/jpeg', 'image/bmp'])) { return $this->json(4001, '请上传png|jpeg|jpg|bmp格式图片'); } if ($image->getSize() > 1.5 * 1024 * 1024) { return $this->json(4001, '图片超出尺寸要求'); } try { if (!$this->uploadPathIsWritable) { throw new \Exception('上传文件夹需要写入权限'); } $src = Filesystem::putFile('images/'.date('Ym'), $image, 'uniqid'); $src = $this->uploadPath.'/'.$src; $return['src'] = $src; } catch (\Exception $e) { return $this->json(4003, $e->getMessage()); } return $this->json(0, 'success', $return); } /** * 通用视频上传 * @return Json */ public function video() { try { $file = request()->file('video'); $md5 = $file->md5();//文件md5 if ($this->validate->checkVideo($file)) { if (!$this->videoUploadPathIsWritable) { throw new \Exception('上传文件夹需要写入权限'); } $src = Filesystem::putFile('videos/'.date('Ym'), $file, 'uniqid'); $src = $this->uploadPath.'/'.$src; $return['src'] = $src; $return['full_src'] = resourceJoin($src); //加入上传文件表 File::add($file, $src, $md5, 'video'); return $this->json(0, 'success', $return); } else { $errorMsg = Lang::get($this->validate->getError()); return $this->json(4002, $errorMsg); } } catch (\Exception $e) { return $this->json(4000, $e->getMessage()); } } }