luck-draw/app/controller/manager/Upload.php

239 lines
9.9 KiB
PHP
Raw Normal View History

2022-02-22 09:27:27 +00:00
<?php
namespace app\controller\manager;
use app\service\Image;
use Intervention\Image\ImageManager;
use Intervention\Image\ImageManagerStatic;
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;
class Upload extends BaseController
{
protected $noNeedLogin = ['video', 'file', 'image', 'wangImage'];
private $isCompress = true;
private $validate;
private $uploadPath;
private $videoUploadPath;
private $uploadPathIsWritable = 0;
private $videoUploadPathIsWritable = 0;
private $DIRECTORY_SEPARATOR = "/";
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');
$this->videoUploadPath = Config::get('filesystem.disks.video.url');
$uploadDiskPath = public_path().$this->uploadPath;
$videoUploadDiskPath = public_path().$this->videoUploadPath;
if (!is_dir($uploadDiskPath)) {
@mkdir($uploadDiskPath, 0777, true);
}
if (!is_dir($videoUploadDiskPath)) {
@mkdir($videoUploadDiskPath, 0777, true);
}
if (is_writable($uploadDiskPath)) {
$this->uploadPathIsWritable = 1;
}
if (is_writable($videoUploadDiskPath)) {
$this->videoUploadPathIsWritable = 1;
}
$this->DIRECTORY_SEPARATOR = DIRECTORY_SEPARATOR == "\\" ? "/" : DIRECTORY_SEPARATOR;
ini_set('max_execution_time', '0');
ini_set("memory_limit", '-1');
set_time_limit(0);
}
//视频上传
public function video()
{
$video = request()->file('video_video');
if (!$this->videoUploadPathIsWritable) {
return $this->json(1, '上传文件夹需要写入权限');
}
if ($this->validate->checkVideo($video)) {
$md5 = $video->md5();//文件md5
if ($fileItem = File::where('md5', $md5)->find()) {
$return['src'] = $fileItem['src'];
$fileItem['updated_at'] = date('Y-m-d H:i:s');
$fileItem->save();
return $this->json(200, '该文件已存在 路径为:'.$fileItem['path'], $return);
}
$path = request()->param('path/s', '');//指定路径 基于public下 若为空则默认
$hasPath = !empty($path);
// 去除以/storage/开头的部分 如/storage/20210808/test => 20210808/test
$path = ltrim(trim($path, $this->DIRECTORY_SEPARATOR), \app\model\Attachment::ROOT_NAME.$this->DIRECTORY_SEPARATOR);
$datePath = $hasPath ? $path : 'videos'.$this->DIRECTORY_SEPARATOR.date('Ym');//自定义目录
checkPathExistWithMake(public_path().$this->uploadPath.'/'.$datePath);
$src = Filesystem::putFile($datePath, $video, 'uniqid');
$src = $this->uploadPath.'/'.$src;
$return['src'] = $src;
File::add($video, $src, $md5, 'video'); //加入上传文件表
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_file');
$md5 = $file->md5();//文件md5
$fileName = $file->getOriginalName();//原始文件名
if ($fileItem = File::where('md5', $md5)->find()) {
$return['src'] = $fileItem['src'];
$return['name'] = $fileName;
$fileItem['updated_at'] = date('Y-m-d H:i:s');
return $this->json(200, '该文件已存在 路径为:'.$fileItem['path'], $return);
}
if ($this->validate->checkFile($file)) {
try {
if (!$this->uploadPathIsWritable) {
throw new \Exception('上传文件夹需要写入权限');
}
$path = request()->param('path/s', '');//指定路径 基于public下 若为空则默认
$hasPath = !empty($path);
$datePath = $hasPath ? $path : 'files'.$this->DIRECTORY_SEPARATOR.date('Ym');//自定义目录
checkPathExistWithMake(public_path().$this->uploadPath.'/'.$datePath);
$src = Filesystem::putFile('files'.$this->DIRECTORY_SEPARATOR.date('Ym'), $file, 'uniqid');
$src = $this->uploadPath.$this->DIRECTORY_SEPARATOR.$src;
$return['src'] = $src;
$return['name'] = $fileName;
File::add($file, $src, $md5, '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-image避免冲突 layui组件自动生成的隐藏file input框中name容易重名冲突
$image = request()->file('image_image');
// $img = ImageManagerStatic::cache(function($imageObj) use ($image) {
// $imageObj->make($image)->resize(300, 200)->greyscale();
// }, 10, true);
// $img = (string)ImageManagerStatic::make($image)->fit(350, 610)->encode('png');
// echo $img;
// exit;
$md5 = $image->md5();//文件md5
$path = request()->param('path/s', '');//指定路径 基于public下 若为空则默认
$hasPath = !empty($path);
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(200, '该文件已存在 路径为:'.$fileItem['path'], $return);
}
try {
if (!$this->uploadPathIsWritable) {
throw new \Exception('上传文件夹需要写入权限');
}
// 去除以/storage/开头的部分 如/storage/20210808/test => 20210808/test
if (strpos(trim($path, $this->DIRECTORY_SEPARATOR), \app\model\Attachment::ROOT_NAME.$this->DIRECTORY_SEPARATOR) === 0) {
$path = substr(trim($path, $this->DIRECTORY_SEPARATOR), strlen(\app\model\Attachment::ROOT_NAME.$this->DIRECTORY_SEPARATOR));
}
$datePath = $hasPath ? $path : 'images'.$this->DIRECTORY_SEPARATOR.date('Ym');//自定义目录
$datePath = ($datePath == $this->DIRECTORY_SEPARATOR."storage") ? $this->DIRECTORY_SEPARATOR : $datePath;
checkPathExistWithMake(public_path().$this->uploadPath.'/'.$datePath);
$src = Filesystem::putFile($datePath, $image, 'uniqid');
$src = $this->uploadPath.$this->DIRECTORY_SEPARATOR.$src;
$suffix = strtolower($image->getOriginalExtension());
$cutJson= input('cut');
$cutList = json_decode($cutJson, true);
// 裁剪尺寸
if (!empty($cutList)) {
$srcArr = explode('.', $src);
foreach ($cutList as $cut) {
ImageManagerStatic::make($image)->fit($cut['width'], $cut['height'])->save(public_path().$srcArr[0].'_'.$cut['name'].'.'.$suffix);
}
}
$return['src'] = $src;
File::add($image, $src, $md5); //加入上传文件表
} 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 wangImage()
{
$imageArr = request()->file('wang_img'); // 该方式前端js上传方法中字段名称必须以数组形式传参 如 wang_img[] = 值
$errno = 0;
$data = [];
if (!$this->uploadPathIsWritable) {
$errno = 1;
$data[] = '上传文件夹需要写入权限';
} else {
foreach ($imageArr as $image) {
$md5 = $image->md5();//文件md5
if ($fileItem = File::where('md5', $md5)->find()) {
$return['src'] = $fileItem['src'];
$fileItem['updated_at'] = date('Y-m-d H:i:s');
$data[] = $fileItem['src'];
$fileItem->save();
continue;
}
if ($this->validate->checkImage($image)) {
checkPathExistWithMake(public_path().$this->uploadPath.'/images/'.date('Ym'));
$src = Filesystem::putFile('images/'.date('Ym'), $image, 'uniqid');
$src = $this->uploadPath.$this->DIRECTORY_SEPARATOR.$src;
$data[] = $src;
if ($this->isCompress) {
Image::resize($src);
}
File::add($image, $src, $md5); //加入上传文件表
} else {
$errno = 1;
$data = [];
$data[] = Lang::get($this->validate->getError());
break;
}
}
}
$return['errno'] = $errno;
$return['data'] = $data;
return json($return);
}
}