| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234 |
- <?php
- /**
- * Created by PhpStorm.
- * User: weifeng
- * Date: 2020-02-04
- * Time: 10:10
- *
- * .--, .--,
- * ( ( \.---./ ) )
- * '.__/o o\__.'
- * {= ^ =}
- * > - <
- * / \
- * // \\
- * //| . |\\
- * "'\ /'"_.-~^`'-.
- * \ _ /--' `
- * ___)( )(___
- * (((__) (__))) 梦之所想,心之所向.
- */
- namespace app\backend\modules\upload\controllers;
- use app\backend\modules\upload\models\CoreAttach;
- use app\common\components\BaseController;
- use app\common\services\ImageZip;
- use app\common\services\upload\UploadService;
- use app\platform\modules\system\models\SystemSetting;
- use getID3;
- class UploadV2Controller extends BaseController
- {
- protected $isPublic = true;
- protected $uniacid;
- protected $common;
- public function __construct()
- {
- $this->uniacid = \YunShop::app()->uniacid ?: 0;
- $this->common = $this->common();
- }
- public function upload()
- {
- $file = request()->file('file');
- if (!$file) {
- return $this->errorJson('文件上传失败.');
- }
- if (!$file->isValid()) {
- return $this->errorJson('文件上传失败.');
- }
- $type = request()->upload_type;
- // 获取文件相关信息
- $originalName = $file->getClientOriginalName(); // 文件原名
- $realPath = $file->getRealPath(); //临时文件的绝对路径
- $ext = $file->getClientOriginalExtension(); //文件后缀
- $harmtype = array('asp', 'php', 'jsp', 'js', 'css', 'php3', 'php4', 'php5', 'ashx', 'aspx', 'exe', 'cgi');
- if (in_array($ext, $harmtype)) {
- return $this->errorJson('文件格式禁止上传');
- }
- if ($type == 'image') {
- if ($file->getClientSize() > 30 * 1024 * 1024) {
- return $this->errorJson('图片过大.');
- }
- $defaultImgType = [
- 'jpg', 'bmp', 'eps', 'gif', 'mif', 'miff', 'png', 'tif',
- 'tiff', 'svg', 'wmf', 'jpe', 'jpeg', 'dib', 'ico', 'tga', 'cut', 'pic','mp4'
- ];
- if (!in_array($ext, $defaultImgType)) {
- return $this->errorJson('非规定类型的文件格式');
- }
- if (!$ext) {
- $ext = 'jpg';
- }
- $newOriginalName = md5($originalName . str_random(6)) . '.' . $ext;
- if (config('app.framework') == 'platform') {
- $setting = SystemSetting::settingLoad('global', 'system_global');
- $remote = SystemSetting::settingLoad('remote', 'system_remote');
- if (in_array($ext, $defaultImgType)) {
- if ($setting['image_extentions'] && !in_array($ext, array_filter($setting['image_extentions']))) {
- return $this->errorJson('非规定类型的文件格式');
- }
- $defaultImgSize = $setting['img_size'] ? $setting['img_size'] * 1024 : 1024 * 1024 * 5; //默认大小为5M
- if ($file->getClientSize() > $defaultImgSize) {
- return $this->errorJson('文件大小超出规定值');
- }
- }
- if ($setting['image']['zip_percentage']) {
- //执行图片压缩
- $imagezip = new ImageZip();
- $imagezip->makeThumb(
- yz_tomedia($newOriginalName),
- yz_tomedia($newOriginalName),
- $setting['image']['zip_percentage']
- );
- }
- if ($setting['thumb_width'] == 1 && $setting['thumb_width']) {
- $imagezip = new ImageZip();
- $imagezip->makeThumb(
- yz_tomedia($newOriginalName),
- yz_tomedia($newOriginalName),
- $setting['thumb_width']
- );
- }
- } else {
- //全局配置
- global $_W;
- //公众号独立配置信息 优先使用公众号独立配置
- $uni_setting = app('WqUniSetting')->get()->toArray();
- if (!empty($uni_setting['remote']) && iunserializer($uni_setting['remote'])['type'] != 0) {
- $setting['remote'] = iunserializer($uni_setting['remote']);
- $remote = $setting['remote'];
- $upload = $_W['setting']['upload'];
- } else {
- $remote = $_W['setting']['remote'];
- $upload = $_W['setting']['upload'];
- }
- if (in_array($ext, $defaultImgType)) {
- if ($upload['image']['extentions'] && !in_array($ext, $upload['image']['extentions'])) {
- return $this->errorJson('非规定类型的文件格式');
- }
- $defaultImgSize = $upload['image']['limit'] ? $upload['image']['limit'] * 1024 : 5 * 1024 * 1024;
- if ($file->getClientSize() > $defaultImgSize) {
- return $this->errorJson('文件大小超出规定值');
- }
- }
- if ($upload['image']['zip_percentage']) {
- //执行图片压缩
- $imagezip = new ImageZip();
- $imagezip->makeThumb(
- yz_tomedia($newOriginalName),
- yz_tomedia($newOriginalName),
- $upload['image']['zip_percentage']
- );
- }
- if ($upload['image']['thumb'] == 1 && $upload['image']['width']) {
- $imagezip = new ImageZip();
- $imagezip->makeThumb(
- yz_tomedia($newOriginalName),
- yz_tomedia($newOriginalName),
- $upload['image']['width']
- );
- }
- }
- if (config('app.framework') == 'platform') {
- //本地上传
- $result = \Storage::disk('newimages')->put($newOriginalName, file_get_contents($realPath));
- if (!$result) {
- return $this->successJson('上传失败');
- }
- $url = \Storage::disk('newimages')->url($newOriginalName);
- //图片审核
- if(app('plugins')->isEnabled('upload-verification')){
- if(in_array($ext,['png','jpg','jpeg','bmp','gif','webp','tiff'])){
- $uploadReuslt = do_upload_verificaton(yz_tomedia($url), 'img');
- if(0 === $uploadReuslt[0]['status']){
- return $this->errorJson($uploadReuslt[0]['msg']);
- }
- }
- }
- \app\platform\modules\application\models\CoreAttach::create([
- 'uniacid' => $this->uniacid,
- 'uid' => \Auth::guard('admin')->user()->uid,
- 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
- 'attachment' => $url,
- 'type' => 1,
- 'module_upload_dir' => '',
- 'group_id' => intval($this->uniacid),
- 'upload_type' => $remote['type']
- ]);
- //远程上传
- if ($remote['type'] != 0) {
- file_remote_upload_new($url, true, $remote);
- }
- return $this->successJson('上传成功', [
- 'name' => $originalName,
- 'ext' => $ext,
- 'filename' => $newOriginalName,
- 'attachment' => $url,
- 'url' => yz_tomedia($url),
- 'is_image' => 1,
- 'filesize' => 'null',
- 'group_id' => intval($this->uniacid),
- 'state' => 'SUCCESS'
- ]);
- } else {
- global $_W;
- //本地上传
- $result = \Storage::disk('image')->put($newOriginalName, file_get_contents($realPath));
- if (!$result) {
- return $this->successJson('上传失败');
- }
- $url = \Storage::disk('image')->url($newOriginalName);
- //图片审核
- if(app('plugins')->isEnabled('upload-verification')){
- if(in_array($ext,['png','jpg','jpeg','bmp','gif','webp','tiff'])){
- $uploadReuslt = do_upload_verificaton(yz_tomedia($url), 'img');
- if(0 === $uploadReuslt[0]['status']){
- return $this->errorJson($uploadReuslt[0]['msg']);
- }
- }
- }
- CoreAttach::create([
- 'uniacid' => $this->uniacid,
- 'uid' => $_W['uid'],
- 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
- 'attachment' => $url,
- 'type' => 1,
- 'createtime' => TIMESTAMP,
- 'module_upload_dir' => '',
- 'group_id' => 0,
- ]);
- //远程上传
- if ($remote['type'] != 0) {
- file_remote_upload_wq($url, true, $remote, true);
- }
- return $this->successJson('上传成功', [
- 'name' => $originalName,
- 'ext' => $ext,
- 'filename' => $newOriginalName,
- 'attachment' => $url,
- 'url' => yz_tomedia($url),
- 'is_image' => 1,
- 'filesize' => 'null',
- 'group_id' => intval($this->uniacid),
- 'state' => 'SUCCESS'
- ]);
- // $info = array(
- // 'name' => $originalName,
- // 'ext' => $ext,
- // 'filename' => $newOriginalName,
- // 'attachment' => $url,
- // 'url' => yz_tomedia($url),
- // 'is_image' => 1,
- // 'filesize' => 'null',
- // );
- //
- // $info['state'] = 'SUCCESS';
- //
- // die(json_encode($info));
- }
- } elseif ($type == 'video') {
- if ($file->getClientSize() > 50 * 1024 * 1024) {
- return $this->errorJson('资源过大.');
- }
- $defaultAudioType = ['avi', 'asf', 'wmv', 'avs', 'flv', 'mkv', 'mov', '3gp', 'mp4',
- 'mpg', 'mpeg', 'dat', 'ogm', 'vob', 'rm', 'rmvb', 'ts', 'tp', 'ifo', 'nsv'
- ];
- $defaultVideoType = [
- 'mp3', 'aac', 'wav', 'wma', 'cda', 'flac', 'm4a', 'mid', 'mka', 'mp2',
- 'mpa', 'mpc', 'ape', 'ofr', 'ogg', 'ra', 'wv', 'tta', 'ac3', 'dts'
- ];
- $merge_ext = array_merge($defaultAudioType, $defaultVideoType);
- if (!in_array($ext, $merge_ext)) {
- return $this->errorJson('非规定类型的文件格式');
- }
- $newOriginalName = md5($originalName . str_random(6)) . '.' . $ext;
- if (config('app.framework') == 'platform') {
- $remote = SystemSetting::settingLoad('remote', 'system_remote');
- $getID3 = new getID3();
- $ThisFileInfo = $getID3->analyze($realPath); //分析文件,$path为音频文件的地址
- $timeline=$ThisFileInfo['playtime_seconds']; //这个获得的便是音频文件的时长
- //本地上传
- $result = \Storage::disk('videos')->put($newOriginalName, file_get_contents($realPath));
- if (!$result) {
- return $this->successJson('上传失败');
- }
- $url = \Storage::disk('videos')->url($newOriginalName);
- //视频审核
- if(app('plugins')->isEnabled('upload-verification')){
- $uploadReuslt = do_upload_verificaton(yz_tomedia($url), 'videos');
- if(0 === $uploadReuslt[0]['status']){
- return $this->errorJson($uploadReuslt[0]['msg']);
- }
- }
- \app\platform\modules\application\models\CoreAttach::create([
- 'uniacid' => $this->uniacid,
- 'uid' => \Auth::guard('admin')->user()->uid,
- 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
- 'attachment' => $url,
- 'type' => 3,
- 'module_upload_dir' => '',
- 'group_id' => intval($this->uniacid),
- 'upload_type' => $remote['type'],
- 'timeline' => $timeline
- ]);
- //远程上传
- if ($remote['type'] != 0) {
- file_video_remote_upload($url, true, $remote);
- }
- return $this->successJson('上传成功', [
- 'name' => $originalName,
- 'ext' => $ext,
- 'filename' => $newOriginalName,
- 'attachment' => $url,
- 'url' => yz_tomedia($url),
- 'is_image' => 0,
- 'filesize' => 'null',
- 'group_id' => intval($this->uniacid)
- ]);
- } else {
- //全局配置
- global $_W;
- //公众号独立配置信息 优先使用公众号独立配置
- $uni_setting = app('WqUniSetting')->get()->toArray();
- if (!empty($uni_setting['remote']) && iunserializer($uni_setting['remote'])['type'] != 0) {
- $setting['remote'] = iunserializer($uni_setting['remote']);
- $remote = $setting['remote'];
- } else {
- $remote = $_W['setting']['remote'];
- }
- //本地上传
- $result = \Storage::disk('videos')->put($newOriginalName, file_get_contents($realPath));
- if (!$result) {
- return $this->successJson('上传失败');
- }
- $url = \Storage::disk('videos')->url($newOriginalName);
- //视频审核
- if(app('plugins')->isEnabled('upload-verification')){
- $uploadReuslt = do_upload_verificaton(yz_tomedia($url), 'videos');
- if(0 === $uploadReuslt[0]['status']){
- return $this->errorJson($uploadReuslt[0]['msg']);
- }
- }
- CoreAttach::create([
- 'uniacid' => $this->uniacid,
- 'uid' => $_W['uid'],
- 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
- 'attachment' => $url,
- 'type' => 3,
- 'createtime' => TIMESTAMP,
- 'module_upload_dir' => '',
- 'group_id' => 0,
- ]);
- //远程上传
- if ($remote['type'] != 0) {
- file_video_remote_upload_wq($url, true, $remote);
- }
- return $this->successJson('上传成功', [
- 'name' => $originalName,
- 'ext' => $ext,
- 'filename' => $newOriginalName,
- 'attachment' => $url,
- 'url' => yz_tomedia($url),
- 'is_image' => 0,
- 'filesize' => 'null',
- 'group_id' => intval($this->uniacid)
- ]);
- // $info = array(
- // 'name' => $originalName,
- // 'ext' => $ext,
- // 'filename' => $newOriginalName,
- // 'attachment' => $url,
- // 'url' => yz_tomedia($url),
- // 'is_image' => 0,
- // 'filesize' => 'null',
- // );
- //
- // $info['state'] = 'SUCCESS';
- // die(json_encode($info));
- }
- } elseif ($type == 'audio') {
- if ($file->getClientSize() > 50 * 1024 * 1024) {
- return $this->errorJson('资源过大.');
- }
- $defaultAudioType = ['avi', 'asf', 'wmv', 'avs', 'flv', 'mkv', 'mov', '3gp', 'mp4',
- 'mpg', 'mpeg', 'dat', 'ogm', 'vob', 'rm', 'rmvb', 'ts', 'tp', 'ifo', 'nsv'
- ];
- $defaultVideoType = [
- 'mp3', 'aac', 'wav', 'wma', 'cda', 'flac', 'm4a', 'mid', 'mka', 'mp2',
- 'mpa', 'mpc', 'ape', 'ofr', 'ogg', 'ra', 'wv', 'tta', 'ac3', 'dts'
- ];
- $merge_ext = array_merge($defaultAudioType, $defaultVideoType);
- if (!in_array($ext, $merge_ext)) {
- return $this->errorJson('非规定类型的文件格式');
- }
- $newOriginalName = md5($originalName . str_random(6)) . '.' . $ext;
- if (config('app.framework') == 'platform') {
- $remote = SystemSetting::settingLoad('remote', 'system_remote');
- $getID3 = new getID3();
- $ThisFileInfo = $getID3->analyze($realPath); //分析文件,$path为音频文件的地址
- $timeline=$ThisFileInfo['playtime_seconds']; //这个获得的便是音频文件的时长
- //本地上传
- $result = \Storage::disk('audios')->put($newOriginalName, file_get_contents($realPath));
- if (!$result) {
- return $this->successJson('上传失败');
- }
- $url = \Storage::disk('audios')->url($newOriginalName);
- \app\platform\modules\application\models\CoreAttach::create([
- 'uniacid' => $this->uniacid,
- 'uid' => \Auth::guard('admin')->user()->uid,
- 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
- 'attachment' => $url,
- 'type' => 3,
- 'module_upload_dir' => '',
- 'group_id' => intval($this->uniacid),
- 'upload_type' => $remote['type'],
- 'timeline' => $timeline,
- 'tag_id' => 0
- ]);
- //远程上传
- if ($remote['type'] != 0) {
- file_remote_upload($url, true, $remote);
- }
- return $this->successJson('上传成功', [
- 'name' => $originalName,
- 'ext' => $ext,
- 'filename' => $newOriginalName,
- 'attachment' => $url,
- 'url' => yz_tomedia($url),
- 'is_image' => 0,
- 'filesize' => 'null',
- 'group_id' => intval($this->uniacid)
- ]);
- } else {
- //全局配置
- global $_W;
- //公众号独立配置信息 优先使用公众号独立配置
- $uni_setting = app('WqUniSetting')->get()->toArray();
- if (!empty($uni_setting['remote']) && iunserializer($uni_setting['remote'])['type'] != 0) {
- $setting['remote'] = iunserializer($uni_setting['remote']);
- $remote = $setting['remote'];
- } else {
- $remote = $_W['setting']['remote'];
- }
- //本地上传
- $result = \Storage::disk('audios')->put($newOriginalName, file_get_contents($realPath));
- if (!$result) {
- return $this->successJson('上传失败');
- }
- $url = \Storage::disk('audios')->url($newOriginalName);
- CoreAttach::create([
- 'uniacid' => $this->uniacid,
- 'uid' => $_W['uid'],
- 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
- 'attachment' => $url,
- 'type' => 3,
- 'createtime' => TIMESTAMP,
- 'module_upload_dir' => '',
- 'group_id' => 0,
- 'tag_id' => 0
- ]);
- //远程上传
- if ($remote['type'] != 0) {
- file_remote_upload($url, true, $remote);
- }
- return $this->successJson('上传成功', [
- 'name' => $originalName,
- 'ext' => $ext,
- 'filename' => $newOriginalName,
- 'attachment' => $url,
- 'url' => yz_tomedia($url),
- 'is_image' => 0,
- 'filesize' => 'null',
- 'group_id' => intval($this->uniacid)
- ]);
- // $info = array(
- // 'name' => $originalName,
- // 'ext' => $ext,
- // 'filename' => $newOriginalName,
- // 'attachment' => $url,
- // 'url' => yz_tomedia($url),
- // 'is_image' => 0,
- // 'filesize' => 'null',
- // );
- //
- // $info['state'] = 'SUCCESS';
- // die(json_encode($info));
- }
- } elseif ($type == 'file') {
- if ($file->getClientSize() > 30 * 1024 * 1024) {
- return $this->errorJson('图片过大.');
- }
- //允许上传的文件,新添mp4格式
- $defaultImgType = [
- 'jpg', 'bmp', 'eps', 'gif', 'mif', 'miff', 'png', 'tif',
- 'tiff', 'svg', 'wmf', 'jpe', 'jpeg', 'dib', 'ico', 'tga', 'cut', 'pic','pdf','mp4'
- ];
- if (!in_array($ext, $defaultImgType)) {
- return $this->errorJson('非规定类型的文件格式');
- }
- if (!$ext) {
- return $this->errorJson('非规定类型的文件格式');
- }
- $newOriginalName = md5($originalName . str_random(6)) . '.' . $ext;
- if (config('app.framework') == 'platform') {
- $setting = SystemSetting::settingLoad('global', 'system_global');
- $remote = SystemSetting::settingLoad('remote', 'system_remote');
- $setting['image_extentions'][] = 'pdf';
- if (in_array($ext, $defaultImgType)) {
- if ($setting['image_extentions'] && !in_array($ext, array_filter($setting['image_extentions']))) {
- return $this->errorJson('非规定类型的文件格式');
- }
- $defaultImgSize = $setting['img_size'] ? $setting['img_size'] * 1024 : 1024 * 1024 * 5; //默认大小为5M
- if ($file->getClientSize() > $defaultImgSize) {
- return $this->errorJson('文件大小超出规定值');
- }
- }
- if ($setting['image']['zip_percentage'] && $ext != 'pdf') {
- //执行图片压缩
- $imagezip = new ImageZip();
- $imagezip->makeThumb(
- yz_tomedia($newOriginalName),
- yz_tomedia($newOriginalName),
- $setting['image']['zip_percentage']
- );
- }
- if ($setting['thumb_width'] == 1 && $setting['thumb_width'] && $ext != 'pdf') {
- $imagezip = new ImageZip();
- $imagezip->makeThumb(
- yz_tomedia($newOriginalName),
- yz_tomedia($newOriginalName),
- $setting['thumb_width']
- );
- }
- $result = \Storage::disk('newimages')->put($newOriginalName, file_get_contents($realPath));
- if (!$result) {
- return $this->successJson('上传失败');
- }
- $url = \Storage::disk('newimages')->url($newOriginalName);
- //图片审核
- if(app('plugins')->isEnabled('upload-verification')){
- if(in_array($ext,['png','jpg','jpeg','bmp','gif','webp','tiff'])){
- $uploadReuslt = do_upload_verificaton(yz_tomedia($url), 'img');
- if(0 === $uploadReuslt[0]['status']){
- return $this->errorJson($uploadReuslt[0]['msg']);
- }
- }
- }
- if ($ext != 'pdf')
- {
- \app\platform\modules\application\models\CoreAttach::create([
- 'uniacid' => $this->uniacid,
- 'uid' => \Auth::guard('admin')->user()->uid,
- 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
- 'attachment' => $url,
- 'type' => 1,
- 'module_upload_dir' => '',
- 'group_id' => intval($this->uniacid),
- 'upload_type' => $remote['type'],
- 'tag_id' => 0
- ]);
- }
- //远程上传
- if ($remote['type'] != 0) {
- file_remote_upload_new($url, true, $remote);
- }
- return $this->successJson('上传成功', [
- 'name' => $originalName,
- 'ext' => $ext,
- 'filename' => $newOriginalName,
- 'attachment' => $url,
- 'url' => yz_tomedia($url),
- 'is_image' => 1,
- 'filesize' => 'null',
- 'group_id' => intval($this->uniacid),
- 'state' => 'SUCCESS'
- ]);
- } else {
- //全局配置
- global $_W;
- //公众号独立配置信息 优先使用公众号独立配置
- $uni_setting = app('WqUniSetting')->get()->toArray();
- if (!empty($uni_setting['remote']) && iunserializer($uni_setting['remote'])['type'] != 0) {
- $setting['remote'] = iunserializer($uni_setting['remote']);
- $remote = $setting['remote'];
- $upload = $_W['setting']['upload'];
- } else {
- $remote = $_W['setting']['remote'];
- $upload = $_W['setting']['upload'];
- }
- if (in_array($ext, $defaultImgType)) {
- $upload['image']['extentions'][] = 'pdf';
- if ($upload['image']['extentions'] && !in_array($ext, $upload['image']['extentions'])) {
- return $this->errorJson('非规定类型的文件格式');
- }
- $defaultImgSize = $upload['image']['limit'] ? $upload['image']['limit'] * 1024 : 5 * 1024 * 1024;
- if ($file->getClientSize() > $defaultImgSize) {
- return $this->errorJson('文件大小超出规定值');
- }
- }
- if ($upload['image']['zip_percentage'] && $ext != 'pdf') {
- //执行图片压缩
- $imagezip = new ImageZip();
- $imagezip->makeThumb(
- yz_tomedia($newOriginalName),
- yz_tomedia($newOriginalName),
- $upload['image']['zip_percentage']
- );
- }
- if ($upload['image']['thumb'] == 1 && $upload['image']['width'] && $ext != 'pdf') {
- $imagezip = new ImageZip();
- $imagezip->makeThumb(
- yz_tomedia($newOriginalName),
- yz_tomedia($newOriginalName),
- $upload['image']['width']
- );
- }
- //本地上传
- $result = \Storage::disk('image')->put($newOriginalName, file_get_contents($realPath));
- if (!$result) {
- return $this->successJson('上传失败');
- }
- $url = \Storage::disk('image')->url($newOriginalName);
- //图片审核
- if(app('plugins')->isEnabled('upload-verification')){
- if(in_array($ext,['png','jpg','jpeg','bmp','gif','webp','tiff'])){
- $uploadReuslt = do_upload_verificaton(yz_tomedia($url), 'img');
- if(0 === $uploadReuslt[0]['status']){
- return $this->errorJson($uploadReuslt[0]['msg']);
- }
- }
- }
- if ($ext != 'pdf')
- {
- CoreAttach::create([
- 'uniacid' => $this->uniacid,
- 'uid' => $_W['uid'],
- 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
- 'attachment' => $url,
- 'type' => 1,
- 'createtime' => TIMESTAMP,
- 'module_upload_dir' => '',
- 'group_id' => 0,
- 'tag_id' => 0
- ]);
- }
- //远程上传
- if ($remote['type'] != 0) {
- file_remote_upload_wq($url, true, $remote, true);
- }
- return $this->successJson('上传成功', [
- 'name' => $originalName,
- 'ext' => $ext,
- 'filename' => $newOriginalName,
- 'attachment' => $url,
- 'url' => yz_tomedia($url),
- 'is_image' => 1,
- 'filesize' => 'null',
- 'group_id' => intval($this->uniacid),
- 'state' => 'SUCCESS'
- ]);
- }
- }
- }
- public function fetch()
- {
- $url = trim(request()->url);
- $resp = ihttp_get($url);
- if (!$resp) {
- return $this->errorJson('提取文件失败');
- }
- if (strexists($resp['headers']['Content-Type'], 'image')) {
- switch ($resp['headers']['Content-Type']) {
- case 'application/x-jpg':
- case 'image/jpeg':
- $ext = 'jpg';
- break;
- case 'image/png':
- $ext = 'png';
- break;
- case 'image/gif':
- $ext = 'gif';
- break;
- default:
- return $this->errorJson('提取资源失败, 资源文件类型错误.');
- break;
- }
- } else {
- return $this->errorJson('提取资源失败, 仅支持图片提取.');
- }
- $originName = pathinfo($url, PATHINFO_BASENAME);
- $newOriginalName = md5($originName . str_random(6)) . '.' . $ext;
- if (config('app.framework') == 'platform') {
- $setting = SystemSetting::settingLoad('global', 'system_global');
- $remote = SystemSetting::settingLoad('remote', 'system_remote');
- if ($setting['image']['zip_percentage']) {
- //执行图片压缩
- $imagezip = new ImageZip();
- $imagezip->makeThumb(
- yz_tomedia($originName),
- yz_tomedia($originName),
- $setting['image']['zip_percentage']
- );
- }
- if ($setting['thumb_width'] == 1 && $setting['thumb_width']) {
- $imagezip = new ImageZip();
- $imagezip->makeThumb(
- yz_tomedia($originName),
- yz_tomedia($originName),
- $setting['thumb_width']
- );
- }
- //本地上传
- $result = \Storage::disk('newimages')->put($newOriginalName, $resp['content']);
- if (!$result) {
- return $this->successJson('上传失败');
- }
- \app\platform\modules\application\models\CoreAttach::create([
- 'uniacid' => $this->uniacid,
- 'uid' => \Auth::guard('admin')->user()->uid,
- 'filename' => $newOriginalName,
- 'attachment' => $url,
- 'type' => 1,
- 'module_upload_dir' => '',
- 'group_id' => intval($this->uniacid),
- 'upload_type' => $remote['type'],
- 'tag_id' => 0
- ]);
- //远程上传
- if ($remote['type'] != 0) {
- file_remote_upload_new($newOriginalName, true, $remote);
- }
- $url = \Storage::disk('newimages')->url($newOriginalName);
- return $this->successJson('上传成功', [
- 'img' => $url,
- 'img_url' => yz_tomedia($url),
- ]);
- } else {
- //全局配置
- global $_W;
- //公众号独立配置信息 优先使用公众号独立配置
- $uni_setting = app('WqUniSetting')->get()->toArray();
- if (!empty($uni_setting['remote']) && iunserializer($uni_setting['remote'])['type'] != 0) {
- $setting['remote'] = iunserializer($uni_setting['remote']);
- $remote = $setting['remote'];
- $upload = $_W['setting']['upload'];
- } else {
- $remote = $_W['setting']['remote'];
- $upload = $_W['setting']['upload'];
- }
- if ($upload['image']['zip_percentage']) {
- //执行图片压缩
- $imagezip = new ImageZip();
- $imagezip->makeThumb(
- yz_tomedia($originName),
- yz_tomedia($originName),
- $upload['image']['zip_percentage']
- );
- }
- if ($upload['image']['thumb'] == 1 && $upload['image']['width']) {
- $imagezip = new ImageZip();
- $imagezip->makeThumb(
- yz_tomedia($originName),
- yz_tomedia($originName),
- $upload['image']['width']
- );
- }
- //本地上传
- $result = \Storage::disk('image')->put($newOriginalName, $resp['content']);
- if (!$result) {
- return $this->successJson('上传失败');
- }
- CoreAttach::create([
- 'uniacid' => $this->uniacid,
- 'uid' => $_W['uid'],
- 'filename' => $newOriginalName,
- 'attachment' => $url,
- 'type' => 1,
- 'createtime' => TIMESTAMP,
- 'module_upload_dir' => '',
- 'group_id' => 0,
- 'tag_id' => 0
- ]);
- $url = \Storage::disk('image')->url($newOriginalName);
- //远程上传
- if ($remote['type'] != 0) {
- file_remote_upload_wq($url, true, $remote, true);
- }
- return $this->successJson('上传成功', [
- 'img' => $url,
- 'img_url' => yz_tomedia($url),
- ]);
- }
- }
- public function getImage()
- {
- if (config('app.framework') == 'platform') {
- $result = $this->getNewImage();
- } else {
- $result = $this->getWqImageV2();
- }
- return $this->successJson('ok', $result);
- }
- public function getWqImageV2()
- {
- $year = request()->year;
- $month = intval(request()->month);
- // $page = max(1, intval(request()->page));
- $groupid = intval(request()->group_id);
- $is_local_image = $this->common['islocal'] == 'local' ? true : false;
- $core_attach = new CoreAttach;
- $core_attach = $core_attach->where('uniacid', $this->uniacid)->where('module_upload_dir', $this->common['module_upload_dir']);
- if (!$this->uniacid) {
- $core_attach = $core_attach->where('uid', \Auth::guard('admin')->user()->uid);
- }
- if ($groupid > 0) {
- $core_attach = $core_attach->where('group_id', $groupid);
- }
- if ($groupid == 0) {
- $core_attach = $core_attach->where('group_id', -1);
- }
- if ($year || $month) {
- $start_time = $month ? strtotime("{$year}-{$month}-01") : strtotime("{$year}-1-01");
- $end_time = $month ? strtotime('+1 month', $start_time) : strtotime('+12 month', $start_time);
- $core_attach = $core_attach->where('createtime', '>=', $start_time)->where('createtime', '<=', $end_time);
- }
- $core_attach = $core_attach->select('id','attachment')->where('type', 1);
- $core_attach = $core_attach->orderby('createtime', 'desc');
- // $count = $core_attach->count();
- $core_attach = $core_attach->paginate(15)->toArray();//->offset($offset)->limit($page_size)->get();
- foreach ($core_attach['data'] as &$meterial) {
- if ($this->common['islocal']) {
- $meterial['url'] = yz_tomedia($meterial['attachment']);
- unset($meterial['uid']);
- } else {
- $meterial['attach'] = yz_tomedia($meterial['attachment'], true);
- $meterial['url'] = $meterial['attach'];
- }
- }
- return $core_attach;
- // $pager = pagination($count, $page, $page_size,'',$context = array('before' => 5, 'after' => 4, 'isajax' => '1'));
- // $result = array('items' => $core_attach, 'pager' => $pager);
- //
- // iajax(0, $result);
- }
- public function getWqImage()
- {
- $year = request()->year;
- $month = intval(request()->month);
- $page = max(1, intval(request()->page));
- $groupid = intval(request()->group_id);
- $page_size = 33;
- $is_local_image = $this->common['islocal'] == 'local' ? true : false;
- if ($page<=1) {
- $page = 0;
- $offset = ($page)*$page_size;
- } else {
- $offset = ($page-1)*$page_size;
- }
- $core_attach = new CoreAttach;
- $core_attach = $core_attach->where('uniacid', $this->uniacid)->where('module_upload_dir', $this->common['module_upload_dir']);
- if (!$this->uniacid) {
- $core_attach = $core_attach->where('uid', \Auth::guard('admin')->user()->uid);
- }
- if ($groupid > 0) {
- $core_attach = $core_attach->where('group_id', $groupid);
- }
- if ($groupid == 0) {
- $core_attach = $core_attach->where('group_id', -1);
- }
- if ($year || $month) {
- $start_time = $month ? strtotime("{$year}-{$month}-01") : strtotime("{$year}-1-01");
- $end_time = $month ? strtotime('+1 month', $start_time) : strtotime('+12 month', $start_time);
- $core_attach = $core_attach->where('createtime', '>=', $start_time)->where('createtime', '<=', $end_time);
- }
- $core_attach = $core_attach->where('type', 1);
- $core_attach = $core_attach->orderby('createtime', 'desc');
- $count = $core_attach->count();
- $core_attach = $core_attach->offset($offset)->limit($page_size)->get();
- foreach ($core_attach as &$meterial) {
- if ($this->common['islocal']) {
- $meterial['url'] = yz_tomedia($meterial['attachment']);
- unset($meterial['uid']);
- } else {
- $meterial['attach'] = yz_tomedia($meterial['attachment'], true);
- $meterial['url'] = $meterial['attach'];
- }
- }
- $pager = pagination($count, $page, $page_size,'',$context = array('before' => 5, 'after' => 4, 'isajax' => '1'));
- $result = array('items' => $core_attach, 'pager' => $pager);
- iajax(0, $result);
- }
- public function getNewImage()
- {
- $core_attach = new \app\platform\modules\application\models\CoreAttach();
- if (request()->year != '不限') {
- $search['year'] = request()->year;
- }
- if(request()->month != '不限') {
- $search['month'] = request()->month;
- }
- $core_attach = $core_attach->search($search);
- $core_attach = $core_attach->where('uniacid', $this->uniacid)->where('module_upload_dir', $this->common['module_upload_dir']);
- if (!$this->uniacid) {
- $core_attach = $core_attach->where('uid', \Auth::guard('admin')->user()->uid);
- }
- //type = 1 图片
- $core_attach = $core_attach->select('id','attachment')->where('type', 1);
- $core_attach = $core_attach->orderby('created_at', 'desc')->paginate(15);
- foreach ($core_attach as &$meterial) {
- $meterial['url'] = yz_tomedia($meterial['attachment']);
- unset($meterial['uid']);
- }
- return $core_attach->toArray();
- }
- public function getVideo()
- {
- if (config('app.framework') == 'platform') {
- $core_attach = new \app\platform\modules\application\models\CoreAttach();
- if (request()->year != '不限') {
- $search['year'] = request()->year;
- }
- if(request()->month != '不限') {
- $search['month'] = request()->month;
- }
- $core_attach = $core_attach->search($search);
- $core_attach = $core_attach->where('uniacid', $this->uniacid)->where('module_upload_dir', $this->common['module_upload_dir']);
- if (!$this->uniacid) {
- $core_attach = $core_attach->where('uid', \Auth::guard('admin')->user()->uid);
- }
- //type = 3 视频
- $core_attach = $core_attach->where('type', 3);
- $core_attach = $core_attach->orderby('created_at', 'desc')->paginate(33);
- foreach ($core_attach as &$meterial) {
- $meterial['url'] = yz_tomedia($meterial['attachment']);
- unset($meterial['uid']);
- }
- return $this->successJson('ok', $core_attach);
- } else {
- $core_attach = new CoreAttach();
- $page_index = max(1, request()->page);
- $page_size = 5;
- if ($page_index<=1) {
- $page_index = 0;
- $offset = ($page_index)*$page_size;
- } else {
- $offset = ($page_index-1)*$page_size;
- }
- if (!$this->uniacid) {
- $core_attach = $core_attach->where('uid', \Auth::guard('admin')->user()->uid);
- }
- $total = $core_attach->count();
- $core_attach = $core_attach
- ->where('type', 3)
- ->where('uniacid', $this->uniacid)
- ->where('module_upload_dir', $this->common['module_upload_dir'])
- ->orderby('createtime', 'desc')
- ->offset($offset)
- ->limit(24)
- ->get();
- foreach ($core_attach as &$meterial) {
- $meterial['url'] = yz_tomedia($meterial['attachment']);
- unset($meterial['uid']);
- }
- $pager = pagination($total, 1, 24, '', $context = array('before' => 5, 'after' => 4, 'isajax' => '1'));
- $result = array('items' => $core_attach, 'pager' => $pager);
- iajax(0, $result);
- }
- }
- public function delete()
- {
- $uid = \Auth::guard('admin')->user()->uid;
- $id = request()->id;
- if (!is_array($id)) {
- $id = array(intval($id));
- }
- $id = safe_gpc_array($id);
- if (config('app.framework') == 'platform') {
- $setting = SystemSetting::settingLoad('global', 'system_global');
- $remote = SystemSetting::settingLoad('remote', 'system_remote');
- $core_attach = \app\platform\modules\application\models\CoreAttach::where('id', $id);
- if (!$this->uniacid) {
- $core_attach = $core_attach->where('uid', $uid);
- } else {
- $core_attach = $core_attach->where('uniacid', $this->uniacid);
- }
- $core_attach = $core_attach->first();
- if ($core_attach['upload_type']) {
- $status = file_remote_delete($core_attach['attachment'], $core_attach['upload_type'], $remote);
- } else {
- $status = file_delete($core_attach['attachment']);
- }
- if (is_error($status)) {
- return $this->errorJson($status['message']);
- }
- if ($core_attach->delete()) {
- return $this->successJson('删除成功');
- } else {
- return $this->errorJson('删除数据表数据失败');
- }
- } else {
- $core_attach = CoreAttach::where('id', $id);
- if (!$this->uniacid) {
- $core_attach = $core_attach->where('uid', $uid);
- } else {
- $core_attach = $core_attach->where('uniacid', $this->uniacid);
- }
- $core_attach = $core_attach->first();
- if ($core_attach['upload_type']) {
- $status = file_remote_delete($core_attach['attachment']);
- } else {
- $status = file_delete($core_attach['attachment']);
- }
- if (is_error($status)) {
- return $this->errorJson($status['message']);
- }
- if ($core_attach->delete()) {
- return $this->successJson('删除成功');
- } else {
- return $this->errorJson('删除数据表数据失败');
- }
- }
- }
- public function common()
- {
- $dest_dir = request()->dest_dir;
- $type = in_array(request()->upload_type, array('image','audio','video')) ? request()->upload_type : 'image';
- $option = array_elements(array('uploadtype', 'global', 'dest_dir'), $_POST);
- $option['width'] = intval($option['width']);
- $option['global'] = request()->global;
- if (preg_match('/^[a-zA-Z0-9_\/]{0,50}$/', $dest_dir, $out)) {
- $dest_dir = trim($dest_dir, '/');
- $pieces = explode('/', $dest_dir);
- if(count($pieces) > 3){
- $dest_dir = '';
- }
- } else {
- $dest_dir = '';
- }
- $module_upload_dir = '';
- if($dest_dir != '') {
- $module_upload_dir = sha1($dest_dir);
- }
- if ($option['global']) {
- $folder = "{$type}s/global/";
- if ($dest_dir) {
- $folder .= '' . $dest_dir . '/';
- }
- } else {
- $folder = "{$type}s/{$this->uniacid}";
- if (!$dest_dir) {
- $folder .= '/' . date('Y/m/');
- } else {
- $folder .= '/' . $dest_dir . '/';
- }
- }
- return [
- 'dest_dir' => $dest_dir,
- 'module_upload_dir' => $module_upload_dir,
- 'type' => $type,
- 'options' => $option,
- 'folder' => $folder,
- ];
- }
- public function uploadVue()
- {
- $type = request()->upload_type;
- $file = request()->file('file');
- if (!$file) {
- return $this->errorJson('请传入正确参数.');
- }
- if (!$file->isValid()) {
- return $this->errorJson('上传失败.');
- }
- switch ($type) {
- case 'video' :
- $dir = 'videos';
- break;
- case 'audio' :
- $dir = 'audio';
- break;
- default :
- $dir = 'image';
- break;
- }
- $uploadService = new UploadService();
- $upload_res = $uploadService->upload($file, $type, $dir);
- return json_encode(['location' => $upload_res['absolute_path']]);
- }
- }
|