zzwy2/app/controller/manager/Upload.php

211 lines
7.6 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\controller\manager;
use app\service\Image;
use app\model\{System, File};
use app\validate\Upload as VUpload;
use think\facade\{Filesystem, Config, Lang};
use think\Image as TImage;
use app\controller\BaseController;
use think\image\Exception as ImageException;
class Upload extends BaseController
{
private $isCompress = true;
private $validate;
private $uploadPath;
private $uploadPathIsWritable = 0;
public function __construct()
{
$system = System::getSystem();
if (!empty($system)) {
$this->isCompress = $system['compress']??true;
}
$this->validate = new VUpload();
$this->uploadPath = Config::get('filesystem.disks.local.url');
if(is_writable(app()->getRootPath() . 'public' . $this->uploadPath)){
$this->uploadPathIsWritable = 1;
}
}
//视频上传
public function video()
{
if(!$this->uploadPathIsWritable){
return $this->json(1, '上传文件夹需要写入权限');
}
$video = request()->file('video');
if($this->validate->checkVideo($video)){
$src = Filesystem::disk('video')->putFile(date('Ymd'), $video, 'uniqid');
$src = $this->uploadPath . '/' . $src;
$return['src'] = $src;
//加入上传文件表
$newFile = File::add($video, $src, 'video');
$return['fid'] = $newFile->id;
return $this->json(0, 'ok', $return);
}else{
$errorMsg = Lang::get($this->validate->getError());
return $this->json(1, $errorMsg);
}
}
//音频上传
public function audio()
{
if(!$this->uploadPathIsWritable){
return $this->json(1, '上传文件夹需要写入权限');
}
$audio = request()->file('audio');
if($this->validate->checkFile($audio)){
$src = Filesystem::disk('audio')->putFile(date('Ymd'), $audio, 'uniqid');
$src = $this->uploadPath . '/' . $src;
$return['src'] = $src;
File::add($audio, $src, 'audio'); //加入上传文件表
return $this->json(0, 'ok', $return);
}else{
$errorMsg = Lang::get($this->validate->getError());
return $this->json(1, $errorMsg);
}
}
//文件上传(通用)
public function file()
{
$file = request()->file('file');
if($this->validate->checkFile($file)){
try{
if(!$this->uploadPathIsWritable){
throw new \Exception('上传文件夹需要写入权限');
}
$src = Filesystem::putFile(date('Ymd'), $file, 'uniqid');
$src = $this->uploadPath . '/' . $src;
$return['src'] = $src;
$return['name'] = $file->getOriginalName();
File::add($file, $src, 'file'); //加入上传文件表
} catch (\Exception $e) {
return $this->json(1, $e->getMessage());
}
return $this->json(0,'ok',$return);
}else{
$errorMsg = Lang::get($this->validate->getError());
return $this->json(1, $errorMsg);
}
}
//图片上传(通用)
public function image()
{
$image = request()->file('image');
// $md5 = $image->md5();//文件md5
if($this->validate->checkImage($image)){
// if ($fileItem = File::where('md5', $md5)->find()) {
// $return['src'] = $fileItem['src'];
// $fileItem['updated_at'] = date('Y-m-d H:i:s');
// return $this->json(0, '该文件已存在 路径为:'.$fileItem['src'], $return);
// }
try{
if(!$this->uploadPathIsWritable){
throw new \Exception('上传文件夹需要写入权限');
}
$src = Filesystem::putFile(date('Ymd'), $image, 'uniqid');
$src = $this->uploadPath . '/' . $src;
// $suffix = strtolower($image->getOriginalExtension());
// if($suffix == 'gif'){
// $return['thumb_src'] = $src; //TODO获取GIF缩略图
// }else{
// $return['thumb_src'] = Image::getThumb($src, 300, 300, TImage::THUMB_SCALING); //上传返回缩略图宽度为300
// }
$return['src'] = $src;
// if($this->isCompress){
// Image::resize($src);
// }
File::add($image, $src); //加入上传文件表
} catch (\Exception|ImageException $e) {
return $this->json(1, $e->getMessage());
}
return $this->json(0, 'ok', $return);
}else{
$errorMsg = Lang::get($this->validate->getError());
return $this->json(1, $errorMsg);
}
}
//富文本编辑器商城图片
public function wangImage()
{
$imageArr = request()->file('wang_img'); // 该方式前端js上传方法中字段名称必须以数组形式传参 如 wang_img[] = 值
$errno = 0;
$data = [];
if(!$this->uploadPathIsWritable){
$errno = 1;
$data[] = '上传文件夹需要写入权限';
}else{
foreach ($imageArr as $image) {
if($this->validate->checkImage($image)){
$src = Filesystem::putFile(date('Ymd'), $image, 'uniqid');
$src = $this->uploadPath . '/' . $src;
$data[] = $src;
if($this->isCompress){
Image::resize($src);
}
File::add($image, $src); //加入上传文件表
}else{
$errno = 1;
$data = [];
$data[] = Lang::get($this->validate->getError());
break;
}
}
}
$return['errno'] = $errno;
$return['data'] = $data;
return json($return);
}
//富文本编辑器上传图片
public function tinyImage()
{
$image = request()->file('file');
if (!$image) {
// 字段名不对
header("HTTP/1.1 404 config field error");
exit;
}
if (!$this->uploadPathIsWritable) {
header("HTTP/1.1 403 Insufficient folder permissions");
exit;
} else {
if (!in_array(strtolower($image->getMime()), ['image/png','image/jpeg','image/jpg','image/gif'])) {
header("HTTP/1.1 400 MIME TYPE ERROR");
exit;
}
checkPathExistWithMake(public_path().$this->uploadPath.'/images/'.date('Ym'));
// tinymce富文本对图片进行操作后xuan上传的是blob文件。
// 针对blob文件未读取到后缀名 自动生成后缀 默认用mimetype后缀 如image/jpeg =》jpeg
$newFileName = $image->hashName('uniqid');
if (isset(explode('.',$newFileName)[1]) && empty(explode('.',$newFileName)[1])) {
$ext = explode('/', $image->getOriginalMime());
$newFileName .= $ext[1];
}
$src = Filesystem::putFileAs('images/'.date('Ym'), $image, $newFileName);
$src = $this->uploadPath.'/'.$src;
if ($this->isCompress) {
// 剪切
Image::resize($src);
}
File::add($image, $src); //加入上传文件表
$res['location'] = $src;
return json($res);
}
}
}