UploadV2Controller.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: weifeng
  5. * Date: 2020-02-04
  6. * Time: 10:10
  7. *
  8. * .--, .--,
  9. * ( ( \.---./ ) )
  10. * '.__/o o\__.'
  11. * {= ^ =}
  12. * > - <
  13. * / \
  14. * // \\
  15. * //| . |\\
  16. * "'\ /'"_.-~^`'-.
  17. * \ _ /--' `
  18. * ___)( )(___
  19. * (((__) (__))) 梦之所想,心之所向.
  20. */
  21. namespace app\backend\modules\upload\controllers;
  22. use app\backend\modules\upload\models\CoreAttach;
  23. use app\common\components\BaseController;
  24. use app\common\services\ImageZip;
  25. use app\common\services\upload\UploadService;
  26. use app\platform\modules\system\models\SystemSetting;
  27. use getID3;
  28. class UploadV2Controller extends BaseController
  29. {
  30. protected $isPublic = true;
  31. protected $uniacid;
  32. protected $common;
  33. public function __construct()
  34. {
  35. $this->uniacid = \YunShop::app()->uniacid ?: 0;
  36. $this->common = $this->common();
  37. }
  38. public function upload()
  39. {
  40. $file = request()->file('file');
  41. if (!$file) {
  42. return $this->errorJson('文件上传失败.');
  43. }
  44. if (!$file->isValid()) {
  45. return $this->errorJson('文件上传失败.');
  46. }
  47. $type = request()->upload_type;
  48. // 获取文件相关信息
  49. $originalName = $file->getClientOriginalName(); // 文件原名
  50. $realPath = $file->getRealPath(); //临时文件的绝对路径
  51. $ext = $file->getClientOriginalExtension(); //文件后缀
  52. $harmtype = array('asp', 'php', 'jsp', 'js', 'css', 'php3', 'php4', 'php5', 'ashx', 'aspx', 'exe', 'cgi');
  53. if (in_array($ext, $harmtype)) {
  54. return $this->errorJson('文件格式禁止上传');
  55. }
  56. if ($type == 'image') {
  57. if ($file->getClientSize() > 30 * 1024 * 1024) {
  58. return $this->errorJson('图片过大.');
  59. }
  60. $defaultImgType = [
  61. 'jpg', 'bmp', 'eps', 'gif', 'mif', 'miff', 'png', 'tif',
  62. 'tiff', 'svg', 'wmf', 'jpe', 'jpeg', 'dib', 'ico', 'tga', 'cut', 'pic','mp4'
  63. ];
  64. if (!in_array($ext, $defaultImgType)) {
  65. return $this->errorJson('非规定类型的文件格式');
  66. }
  67. if (!$ext) {
  68. $ext = 'jpg';
  69. }
  70. $newOriginalName = md5($originalName . str_random(6)) . '.' . $ext;
  71. if (config('app.framework') == 'platform') {
  72. $setting = SystemSetting::settingLoad('global', 'system_global');
  73. $remote = SystemSetting::settingLoad('remote', 'system_remote');
  74. if (in_array($ext, $defaultImgType)) {
  75. if ($setting['image_extentions'] && !in_array($ext, array_filter($setting['image_extentions']))) {
  76. return $this->errorJson('非规定类型的文件格式');
  77. }
  78. $defaultImgSize = $setting['img_size'] ? $setting['img_size'] * 1024 : 1024 * 1024 * 5; //默认大小为5M
  79. if ($file->getClientSize() > $defaultImgSize) {
  80. return $this->errorJson('文件大小超出规定值');
  81. }
  82. }
  83. if ($setting['image']['zip_percentage']) {
  84. //执行图片压缩
  85. $imagezip = new ImageZip();
  86. $imagezip->makeThumb(
  87. yz_tomedia($newOriginalName),
  88. yz_tomedia($newOriginalName),
  89. $setting['image']['zip_percentage']
  90. );
  91. }
  92. if ($setting['thumb_width'] == 1 && $setting['thumb_width']) {
  93. $imagezip = new ImageZip();
  94. $imagezip->makeThumb(
  95. yz_tomedia($newOriginalName),
  96. yz_tomedia($newOriginalName),
  97. $setting['thumb_width']
  98. );
  99. }
  100. } else {
  101. //全局配置
  102. global $_W;
  103. //公众号独立配置信息 优先使用公众号独立配置
  104. $uni_setting = app('WqUniSetting')->get()->toArray();
  105. if (!empty($uni_setting['remote']) && iunserializer($uni_setting['remote'])['type'] != 0) {
  106. $setting['remote'] = iunserializer($uni_setting['remote']);
  107. $remote = $setting['remote'];
  108. $upload = $_W['setting']['upload'];
  109. } else {
  110. $remote = $_W['setting']['remote'];
  111. $upload = $_W['setting']['upload'];
  112. }
  113. if (in_array($ext, $defaultImgType)) {
  114. if ($upload['image']['extentions'] && !in_array($ext, $upload['image']['extentions'])) {
  115. return $this->errorJson('非规定类型的文件格式');
  116. }
  117. $defaultImgSize = $upload['image']['limit'] ? $upload['image']['limit'] * 1024 : 5 * 1024 * 1024;
  118. if ($file->getClientSize() > $defaultImgSize) {
  119. return $this->errorJson('文件大小超出规定值');
  120. }
  121. }
  122. if ($upload['image']['zip_percentage']) {
  123. //执行图片压缩
  124. $imagezip = new ImageZip();
  125. $imagezip->makeThumb(
  126. yz_tomedia($newOriginalName),
  127. yz_tomedia($newOriginalName),
  128. $upload['image']['zip_percentage']
  129. );
  130. }
  131. if ($upload['image']['thumb'] == 1 && $upload['image']['width']) {
  132. $imagezip = new ImageZip();
  133. $imagezip->makeThumb(
  134. yz_tomedia($newOriginalName),
  135. yz_tomedia($newOriginalName),
  136. $upload['image']['width']
  137. );
  138. }
  139. }
  140. if (config('app.framework') == 'platform') {
  141. //本地上传
  142. $result = \Storage::disk('newimages')->put($newOriginalName, file_get_contents($realPath));
  143. if (!$result) {
  144. return $this->successJson('上传失败');
  145. }
  146. $url = \Storage::disk('newimages')->url($newOriginalName);
  147. //图片审核
  148. if(app('plugins')->isEnabled('upload-verification')){
  149. if(in_array($ext,['png','jpg','jpeg','bmp','gif','webp','tiff'])){
  150. $uploadReuslt = do_upload_verificaton(yz_tomedia($url), 'img');
  151. if(0 === $uploadReuslt[0]['status']){
  152. return $this->errorJson($uploadReuslt[0]['msg']);
  153. }
  154. }
  155. }
  156. \app\platform\modules\application\models\CoreAttach::create([
  157. 'uniacid' => $this->uniacid,
  158. 'uid' => \Auth::guard('admin')->user()->uid,
  159. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  160. 'attachment' => $url,
  161. 'type' => 1,
  162. 'module_upload_dir' => '',
  163. 'group_id' => intval($this->uniacid),
  164. 'upload_type' => $remote['type']
  165. ]);
  166. //远程上传
  167. if ($remote['type'] != 0) {
  168. file_remote_upload_new($url, true, $remote);
  169. }
  170. return $this->successJson('上传成功', [
  171. 'name' => $originalName,
  172. 'ext' => $ext,
  173. 'filename' => $newOriginalName,
  174. 'attachment' => $url,
  175. 'url' => yz_tomedia($url),
  176. 'is_image' => 1,
  177. 'filesize' => 'null',
  178. 'group_id' => intval($this->uniacid),
  179. 'state' => 'SUCCESS'
  180. ]);
  181. } else {
  182. global $_W;
  183. //本地上传
  184. $result = \Storage::disk('image')->put($newOriginalName, file_get_contents($realPath));
  185. if (!$result) {
  186. return $this->successJson('上传失败');
  187. }
  188. $url = \Storage::disk('image')->url($newOriginalName);
  189. //图片审核
  190. if(app('plugins')->isEnabled('upload-verification')){
  191. if(in_array($ext,['png','jpg','jpeg','bmp','gif','webp','tiff'])){
  192. $uploadReuslt = do_upload_verificaton(yz_tomedia($url), 'img');
  193. if(0 === $uploadReuslt[0]['status']){
  194. return $this->errorJson($uploadReuslt[0]['msg']);
  195. }
  196. }
  197. }
  198. CoreAttach::create([
  199. 'uniacid' => $this->uniacid,
  200. 'uid' => $_W['uid'],
  201. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  202. 'attachment' => $url,
  203. 'type' => 1,
  204. 'createtime' => TIMESTAMP,
  205. 'module_upload_dir' => '',
  206. 'group_id' => 0,
  207. ]);
  208. //远程上传
  209. if ($remote['type'] != 0) {
  210. file_remote_upload_wq($url, true, $remote, true);
  211. }
  212. return $this->successJson('上传成功', [
  213. 'name' => $originalName,
  214. 'ext' => $ext,
  215. 'filename' => $newOriginalName,
  216. 'attachment' => $url,
  217. 'url' => yz_tomedia($url),
  218. 'is_image' => 1,
  219. 'filesize' => 'null',
  220. 'group_id' => intval($this->uniacid),
  221. 'state' => 'SUCCESS'
  222. ]);
  223. // $info = array(
  224. // 'name' => $originalName,
  225. // 'ext' => $ext,
  226. // 'filename' => $newOriginalName,
  227. // 'attachment' => $url,
  228. // 'url' => yz_tomedia($url),
  229. // 'is_image' => 1,
  230. // 'filesize' => 'null',
  231. // );
  232. //
  233. // $info['state'] = 'SUCCESS';
  234. //
  235. // die(json_encode($info));
  236. }
  237. } elseif ($type == 'video') {
  238. if ($file->getClientSize() > 50 * 1024 * 1024) {
  239. return $this->errorJson('资源过大.');
  240. }
  241. $defaultAudioType = ['avi', 'asf', 'wmv', 'avs', 'flv', 'mkv', 'mov', '3gp', 'mp4',
  242. 'mpg', 'mpeg', 'dat', 'ogm', 'vob', 'rm', 'rmvb', 'ts', 'tp', 'ifo', 'nsv'
  243. ];
  244. $defaultVideoType = [
  245. 'mp3', 'aac', 'wav', 'wma', 'cda', 'flac', 'm4a', 'mid', 'mka', 'mp2',
  246. 'mpa', 'mpc', 'ape', 'ofr', 'ogg', 'ra', 'wv', 'tta', 'ac3', 'dts'
  247. ];
  248. $merge_ext = array_merge($defaultAudioType, $defaultVideoType);
  249. if (!in_array($ext, $merge_ext)) {
  250. return $this->errorJson('非规定类型的文件格式');
  251. }
  252. $newOriginalName = md5($originalName . str_random(6)) . '.' . $ext;
  253. if (config('app.framework') == 'platform') {
  254. $remote = SystemSetting::settingLoad('remote', 'system_remote');
  255. $getID3 = new getID3();
  256. $ThisFileInfo = $getID3->analyze($realPath); //分析文件,$path为音频文件的地址
  257. $timeline=$ThisFileInfo['playtime_seconds']; //这个获得的便是音频文件的时长
  258. //本地上传
  259. $result = \Storage::disk('videos')->put($newOriginalName, file_get_contents($realPath));
  260. if (!$result) {
  261. return $this->successJson('上传失败');
  262. }
  263. $url = \Storage::disk('videos')->url($newOriginalName);
  264. //视频审核
  265. if(app('plugins')->isEnabled('upload-verification')){
  266. $uploadReuslt = do_upload_verificaton(yz_tomedia($url), 'videos');
  267. if(0 === $uploadReuslt[0]['status']){
  268. return $this->errorJson($uploadReuslt[0]['msg']);
  269. }
  270. }
  271. \app\platform\modules\application\models\CoreAttach::create([
  272. 'uniacid' => $this->uniacid,
  273. 'uid' => \Auth::guard('admin')->user()->uid,
  274. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  275. 'attachment' => $url,
  276. 'type' => 3,
  277. 'module_upload_dir' => '',
  278. 'group_id' => intval($this->uniacid),
  279. 'upload_type' => $remote['type'],
  280. 'timeline' => $timeline
  281. ]);
  282. //远程上传
  283. if ($remote['type'] != 0) {
  284. file_video_remote_upload($url, true, $remote);
  285. }
  286. return $this->successJson('上传成功', [
  287. 'name' => $originalName,
  288. 'ext' => $ext,
  289. 'filename' => $newOriginalName,
  290. 'attachment' => $url,
  291. 'url' => yz_tomedia($url),
  292. 'is_image' => 0,
  293. 'filesize' => 'null',
  294. 'group_id' => intval($this->uniacid)
  295. ]);
  296. } else {
  297. //全局配置
  298. global $_W;
  299. //公众号独立配置信息 优先使用公众号独立配置
  300. $uni_setting = app('WqUniSetting')->get()->toArray();
  301. if (!empty($uni_setting['remote']) && iunserializer($uni_setting['remote'])['type'] != 0) {
  302. $setting['remote'] = iunserializer($uni_setting['remote']);
  303. $remote = $setting['remote'];
  304. } else {
  305. $remote = $_W['setting']['remote'];
  306. }
  307. //本地上传
  308. $result = \Storage::disk('videos')->put($newOriginalName, file_get_contents($realPath));
  309. if (!$result) {
  310. return $this->successJson('上传失败');
  311. }
  312. $url = \Storage::disk('videos')->url($newOriginalName);
  313. //视频审核
  314. if(app('plugins')->isEnabled('upload-verification')){
  315. $uploadReuslt = do_upload_verificaton(yz_tomedia($url), 'videos');
  316. if(0 === $uploadReuslt[0]['status']){
  317. return $this->errorJson($uploadReuslt[0]['msg']);
  318. }
  319. }
  320. CoreAttach::create([
  321. 'uniacid' => $this->uniacid,
  322. 'uid' => $_W['uid'],
  323. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  324. 'attachment' => $url,
  325. 'type' => 3,
  326. 'createtime' => TIMESTAMP,
  327. 'module_upload_dir' => '',
  328. 'group_id' => 0,
  329. ]);
  330. //远程上传
  331. if ($remote['type'] != 0) {
  332. file_video_remote_upload_wq($url, true, $remote);
  333. }
  334. return $this->successJson('上传成功', [
  335. 'name' => $originalName,
  336. 'ext' => $ext,
  337. 'filename' => $newOriginalName,
  338. 'attachment' => $url,
  339. 'url' => yz_tomedia($url),
  340. 'is_image' => 0,
  341. 'filesize' => 'null',
  342. 'group_id' => intval($this->uniacid)
  343. ]);
  344. // $info = array(
  345. // 'name' => $originalName,
  346. // 'ext' => $ext,
  347. // 'filename' => $newOriginalName,
  348. // 'attachment' => $url,
  349. // 'url' => yz_tomedia($url),
  350. // 'is_image' => 0,
  351. // 'filesize' => 'null',
  352. // );
  353. //
  354. // $info['state'] = 'SUCCESS';
  355. // die(json_encode($info));
  356. }
  357. } elseif ($type == 'audio') {
  358. if ($file->getClientSize() > 50 * 1024 * 1024) {
  359. return $this->errorJson('资源过大.');
  360. }
  361. $defaultAudioType = ['avi', 'asf', 'wmv', 'avs', 'flv', 'mkv', 'mov', '3gp', 'mp4',
  362. 'mpg', 'mpeg', 'dat', 'ogm', 'vob', 'rm', 'rmvb', 'ts', 'tp', 'ifo', 'nsv'
  363. ];
  364. $defaultVideoType = [
  365. 'mp3', 'aac', 'wav', 'wma', 'cda', 'flac', 'm4a', 'mid', 'mka', 'mp2',
  366. 'mpa', 'mpc', 'ape', 'ofr', 'ogg', 'ra', 'wv', 'tta', 'ac3', 'dts'
  367. ];
  368. $merge_ext = array_merge($defaultAudioType, $defaultVideoType);
  369. if (!in_array($ext, $merge_ext)) {
  370. return $this->errorJson('非规定类型的文件格式');
  371. }
  372. $newOriginalName = md5($originalName . str_random(6)) . '.' . $ext;
  373. if (config('app.framework') == 'platform') {
  374. $remote = SystemSetting::settingLoad('remote', 'system_remote');
  375. $getID3 = new getID3();
  376. $ThisFileInfo = $getID3->analyze($realPath); //分析文件,$path为音频文件的地址
  377. $timeline=$ThisFileInfo['playtime_seconds']; //这个获得的便是音频文件的时长
  378. //本地上传
  379. $result = \Storage::disk('audios')->put($newOriginalName, file_get_contents($realPath));
  380. if (!$result) {
  381. return $this->successJson('上传失败');
  382. }
  383. $url = \Storage::disk('audios')->url($newOriginalName);
  384. \app\platform\modules\application\models\CoreAttach::create([
  385. 'uniacid' => $this->uniacid,
  386. 'uid' => \Auth::guard('admin')->user()->uid,
  387. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  388. 'attachment' => $url,
  389. 'type' => 3,
  390. 'module_upload_dir' => '',
  391. 'group_id' => intval($this->uniacid),
  392. 'upload_type' => $remote['type'],
  393. 'timeline' => $timeline,
  394. 'tag_id' => 0
  395. ]);
  396. //远程上传
  397. if ($remote['type'] != 0) {
  398. file_remote_upload($url, true, $remote);
  399. }
  400. return $this->successJson('上传成功', [
  401. 'name' => $originalName,
  402. 'ext' => $ext,
  403. 'filename' => $newOriginalName,
  404. 'attachment' => $url,
  405. 'url' => yz_tomedia($url),
  406. 'is_image' => 0,
  407. 'filesize' => 'null',
  408. 'group_id' => intval($this->uniacid)
  409. ]);
  410. } else {
  411. //全局配置
  412. global $_W;
  413. //公众号独立配置信息 优先使用公众号独立配置
  414. $uni_setting = app('WqUniSetting')->get()->toArray();
  415. if (!empty($uni_setting['remote']) && iunserializer($uni_setting['remote'])['type'] != 0) {
  416. $setting['remote'] = iunserializer($uni_setting['remote']);
  417. $remote = $setting['remote'];
  418. } else {
  419. $remote = $_W['setting']['remote'];
  420. }
  421. //本地上传
  422. $result = \Storage::disk('audios')->put($newOriginalName, file_get_contents($realPath));
  423. if (!$result) {
  424. return $this->successJson('上传失败');
  425. }
  426. $url = \Storage::disk('audios')->url($newOriginalName);
  427. CoreAttach::create([
  428. 'uniacid' => $this->uniacid,
  429. 'uid' => $_W['uid'],
  430. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  431. 'attachment' => $url,
  432. 'type' => 3,
  433. 'createtime' => TIMESTAMP,
  434. 'module_upload_dir' => '',
  435. 'group_id' => 0,
  436. 'tag_id' => 0
  437. ]);
  438. //远程上传
  439. if ($remote['type'] != 0) {
  440. file_remote_upload($url, true, $remote);
  441. }
  442. return $this->successJson('上传成功', [
  443. 'name' => $originalName,
  444. 'ext' => $ext,
  445. 'filename' => $newOriginalName,
  446. 'attachment' => $url,
  447. 'url' => yz_tomedia($url),
  448. 'is_image' => 0,
  449. 'filesize' => 'null',
  450. 'group_id' => intval($this->uniacid)
  451. ]);
  452. // $info = array(
  453. // 'name' => $originalName,
  454. // 'ext' => $ext,
  455. // 'filename' => $newOriginalName,
  456. // 'attachment' => $url,
  457. // 'url' => yz_tomedia($url),
  458. // 'is_image' => 0,
  459. // 'filesize' => 'null',
  460. // );
  461. //
  462. // $info['state'] = 'SUCCESS';
  463. // die(json_encode($info));
  464. }
  465. } elseif ($type == 'file') {
  466. if ($file->getClientSize() > 30 * 1024 * 1024) {
  467. return $this->errorJson('图片过大.');
  468. }
  469. //允许上传的文件,新添mp4格式
  470. $defaultImgType = [
  471. 'jpg', 'bmp', 'eps', 'gif', 'mif', 'miff', 'png', 'tif',
  472. 'tiff', 'svg', 'wmf', 'jpe', 'jpeg', 'dib', 'ico', 'tga', 'cut', 'pic','pdf','mp4'
  473. ];
  474. if (!in_array($ext, $defaultImgType)) {
  475. return $this->errorJson('非规定类型的文件格式');
  476. }
  477. if (!$ext) {
  478. return $this->errorJson('非规定类型的文件格式');
  479. }
  480. $newOriginalName = md5($originalName . str_random(6)) . '.' . $ext;
  481. if (config('app.framework') == 'platform') {
  482. $setting = SystemSetting::settingLoad('global', 'system_global');
  483. $remote = SystemSetting::settingLoad('remote', 'system_remote');
  484. $setting['image_extentions'][] = 'pdf';
  485. if (in_array($ext, $defaultImgType)) {
  486. if ($setting['image_extentions'] && !in_array($ext, array_filter($setting['image_extentions']))) {
  487. return $this->errorJson('非规定类型的文件格式');
  488. }
  489. $defaultImgSize = $setting['img_size'] ? $setting['img_size'] * 1024 : 1024 * 1024 * 5; //默认大小为5M
  490. if ($file->getClientSize() > $defaultImgSize) {
  491. return $this->errorJson('文件大小超出规定值');
  492. }
  493. }
  494. if ($setting['image']['zip_percentage'] && $ext != 'pdf') {
  495. //执行图片压缩
  496. $imagezip = new ImageZip();
  497. $imagezip->makeThumb(
  498. yz_tomedia($newOriginalName),
  499. yz_tomedia($newOriginalName),
  500. $setting['image']['zip_percentage']
  501. );
  502. }
  503. if ($setting['thumb_width'] == 1 && $setting['thumb_width'] && $ext != 'pdf') {
  504. $imagezip = new ImageZip();
  505. $imagezip->makeThumb(
  506. yz_tomedia($newOriginalName),
  507. yz_tomedia($newOriginalName),
  508. $setting['thumb_width']
  509. );
  510. }
  511. $result = \Storage::disk('newimages')->put($newOriginalName, file_get_contents($realPath));
  512. if (!$result) {
  513. return $this->successJson('上传失败');
  514. }
  515. $url = \Storage::disk('newimages')->url($newOriginalName);
  516. //图片审核
  517. if(app('plugins')->isEnabled('upload-verification')){
  518. if(in_array($ext,['png','jpg','jpeg','bmp','gif','webp','tiff'])){
  519. $uploadReuslt = do_upload_verificaton(yz_tomedia($url), 'img');
  520. if(0 === $uploadReuslt[0]['status']){
  521. return $this->errorJson($uploadReuslt[0]['msg']);
  522. }
  523. }
  524. }
  525. if ($ext != 'pdf')
  526. {
  527. \app\platform\modules\application\models\CoreAttach::create([
  528. 'uniacid' => $this->uniacid,
  529. 'uid' => \Auth::guard('admin')->user()->uid,
  530. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  531. 'attachment' => $url,
  532. 'type' => 1,
  533. 'module_upload_dir' => '',
  534. 'group_id' => intval($this->uniacid),
  535. 'upload_type' => $remote['type'],
  536. 'tag_id' => 0
  537. ]);
  538. }
  539. //远程上传
  540. if ($remote['type'] != 0) {
  541. file_remote_upload_new($url, true, $remote);
  542. }
  543. return $this->successJson('上传成功', [
  544. 'name' => $originalName,
  545. 'ext' => $ext,
  546. 'filename' => $newOriginalName,
  547. 'attachment' => $url,
  548. 'url' => yz_tomedia($url),
  549. 'is_image' => 1,
  550. 'filesize' => 'null',
  551. 'group_id' => intval($this->uniacid),
  552. 'state' => 'SUCCESS'
  553. ]);
  554. } else {
  555. //全局配置
  556. global $_W;
  557. //公众号独立配置信息 优先使用公众号独立配置
  558. $uni_setting = app('WqUniSetting')->get()->toArray();
  559. if (!empty($uni_setting['remote']) && iunserializer($uni_setting['remote'])['type'] != 0) {
  560. $setting['remote'] = iunserializer($uni_setting['remote']);
  561. $remote = $setting['remote'];
  562. $upload = $_W['setting']['upload'];
  563. } else {
  564. $remote = $_W['setting']['remote'];
  565. $upload = $_W['setting']['upload'];
  566. }
  567. if (in_array($ext, $defaultImgType)) {
  568. $upload['image']['extentions'][] = 'pdf';
  569. if ($upload['image']['extentions'] && !in_array($ext, $upload['image']['extentions'])) {
  570. return $this->errorJson('非规定类型的文件格式');
  571. }
  572. $defaultImgSize = $upload['image']['limit'] ? $upload['image']['limit'] * 1024 : 5 * 1024 * 1024;
  573. if ($file->getClientSize() > $defaultImgSize) {
  574. return $this->errorJson('文件大小超出规定值');
  575. }
  576. }
  577. if ($upload['image']['zip_percentage'] && $ext != 'pdf') {
  578. //执行图片压缩
  579. $imagezip = new ImageZip();
  580. $imagezip->makeThumb(
  581. yz_tomedia($newOriginalName),
  582. yz_tomedia($newOriginalName),
  583. $upload['image']['zip_percentage']
  584. );
  585. }
  586. if ($upload['image']['thumb'] == 1 && $upload['image']['width'] && $ext != 'pdf') {
  587. $imagezip = new ImageZip();
  588. $imagezip->makeThumb(
  589. yz_tomedia($newOriginalName),
  590. yz_tomedia($newOriginalName),
  591. $upload['image']['width']
  592. );
  593. }
  594. //本地上传
  595. $result = \Storage::disk('image')->put($newOriginalName, file_get_contents($realPath));
  596. if (!$result) {
  597. return $this->successJson('上传失败');
  598. }
  599. $url = \Storage::disk('image')->url($newOriginalName);
  600. //图片审核
  601. if(app('plugins')->isEnabled('upload-verification')){
  602. if(in_array($ext,['png','jpg','jpeg','bmp','gif','webp','tiff'])){
  603. $uploadReuslt = do_upload_verificaton(yz_tomedia($url), 'img');
  604. if(0 === $uploadReuslt[0]['status']){
  605. return $this->errorJson($uploadReuslt[0]['msg']);
  606. }
  607. }
  608. }
  609. if ($ext != 'pdf')
  610. {
  611. CoreAttach::create([
  612. 'uniacid' => $this->uniacid,
  613. 'uid' => $_W['uid'],
  614. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  615. 'attachment' => $url,
  616. 'type' => 1,
  617. 'createtime' => TIMESTAMP,
  618. 'module_upload_dir' => '',
  619. 'group_id' => 0,
  620. 'tag_id' => 0
  621. ]);
  622. }
  623. //远程上传
  624. if ($remote['type'] != 0) {
  625. file_remote_upload_wq($url, true, $remote, true);
  626. }
  627. return $this->successJson('上传成功', [
  628. 'name' => $originalName,
  629. 'ext' => $ext,
  630. 'filename' => $newOriginalName,
  631. 'attachment' => $url,
  632. 'url' => yz_tomedia($url),
  633. 'is_image' => 1,
  634. 'filesize' => 'null',
  635. 'group_id' => intval($this->uniacid),
  636. 'state' => 'SUCCESS'
  637. ]);
  638. }
  639. }
  640. }
  641. public function fetch()
  642. {
  643. $url = trim(request()->url);
  644. $resp = ihttp_get($url);
  645. if (!$resp) {
  646. return $this->errorJson('提取文件失败');
  647. }
  648. if (strexists($resp['headers']['Content-Type'], 'image')) {
  649. switch ($resp['headers']['Content-Type']) {
  650. case 'application/x-jpg':
  651. case 'image/jpeg':
  652. $ext = 'jpg';
  653. break;
  654. case 'image/png':
  655. $ext = 'png';
  656. break;
  657. case 'image/gif':
  658. $ext = 'gif';
  659. break;
  660. default:
  661. return $this->errorJson('提取资源失败, 资源文件类型错误.');
  662. break;
  663. }
  664. } else {
  665. return $this->errorJson('提取资源失败, 仅支持图片提取.');
  666. }
  667. $originName = pathinfo($url, PATHINFO_BASENAME);
  668. $newOriginalName = md5($originName . str_random(6)) . '.' . $ext;
  669. if (config('app.framework') == 'platform') {
  670. $setting = SystemSetting::settingLoad('global', 'system_global');
  671. $remote = SystemSetting::settingLoad('remote', 'system_remote');
  672. if ($setting['image']['zip_percentage']) {
  673. //执行图片压缩
  674. $imagezip = new ImageZip();
  675. $imagezip->makeThumb(
  676. yz_tomedia($originName),
  677. yz_tomedia($originName),
  678. $setting['image']['zip_percentage']
  679. );
  680. }
  681. if ($setting['thumb_width'] == 1 && $setting['thumb_width']) {
  682. $imagezip = new ImageZip();
  683. $imagezip->makeThumb(
  684. yz_tomedia($originName),
  685. yz_tomedia($originName),
  686. $setting['thumb_width']
  687. );
  688. }
  689. //本地上传
  690. $result = \Storage::disk('newimages')->put($newOriginalName, $resp['content']);
  691. if (!$result) {
  692. return $this->successJson('上传失败');
  693. }
  694. \app\platform\modules\application\models\CoreAttach::create([
  695. 'uniacid' => $this->uniacid,
  696. 'uid' => \Auth::guard('admin')->user()->uid,
  697. 'filename' => $newOriginalName,
  698. 'attachment' => $url,
  699. 'type' => 1,
  700. 'module_upload_dir' => '',
  701. 'group_id' => intval($this->uniacid),
  702. 'upload_type' => $remote['type'],
  703. 'tag_id' => 0
  704. ]);
  705. //远程上传
  706. if ($remote['type'] != 0) {
  707. file_remote_upload_new($newOriginalName, true, $remote);
  708. }
  709. $url = \Storage::disk('newimages')->url($newOriginalName);
  710. return $this->successJson('上传成功', [
  711. 'img' => $url,
  712. 'img_url' => yz_tomedia($url),
  713. ]);
  714. } else {
  715. //全局配置
  716. global $_W;
  717. //公众号独立配置信息 优先使用公众号独立配置
  718. $uni_setting = app('WqUniSetting')->get()->toArray();
  719. if (!empty($uni_setting['remote']) && iunserializer($uni_setting['remote'])['type'] != 0) {
  720. $setting['remote'] = iunserializer($uni_setting['remote']);
  721. $remote = $setting['remote'];
  722. $upload = $_W['setting']['upload'];
  723. } else {
  724. $remote = $_W['setting']['remote'];
  725. $upload = $_W['setting']['upload'];
  726. }
  727. if ($upload['image']['zip_percentage']) {
  728. //执行图片压缩
  729. $imagezip = new ImageZip();
  730. $imagezip->makeThumb(
  731. yz_tomedia($originName),
  732. yz_tomedia($originName),
  733. $upload['image']['zip_percentage']
  734. );
  735. }
  736. if ($upload['image']['thumb'] == 1 && $upload['image']['width']) {
  737. $imagezip = new ImageZip();
  738. $imagezip->makeThumb(
  739. yz_tomedia($originName),
  740. yz_tomedia($originName),
  741. $upload['image']['width']
  742. );
  743. }
  744. //本地上传
  745. $result = \Storage::disk('image')->put($newOriginalName, $resp['content']);
  746. if (!$result) {
  747. return $this->successJson('上传失败');
  748. }
  749. CoreAttach::create([
  750. 'uniacid' => $this->uniacid,
  751. 'uid' => $_W['uid'],
  752. 'filename' => $newOriginalName,
  753. 'attachment' => $url,
  754. 'type' => 1,
  755. 'createtime' => TIMESTAMP,
  756. 'module_upload_dir' => '',
  757. 'group_id' => 0,
  758. 'tag_id' => 0
  759. ]);
  760. $url = \Storage::disk('image')->url($newOriginalName);
  761. //远程上传
  762. if ($remote['type'] != 0) {
  763. file_remote_upload_wq($url, true, $remote, true);
  764. }
  765. return $this->successJson('上传成功', [
  766. 'img' => $url,
  767. 'img_url' => yz_tomedia($url),
  768. ]);
  769. }
  770. }
  771. public function getImage()
  772. {
  773. if (config('app.framework') == 'platform') {
  774. $result = $this->getNewImage();
  775. } else {
  776. $result = $this->getWqImageV2();
  777. }
  778. return $this->successJson('ok', $result);
  779. }
  780. public function getWqImageV2()
  781. {
  782. $year = request()->year;
  783. $month = intval(request()->month);
  784. // $page = max(1, intval(request()->page));
  785. $groupid = intval(request()->group_id);
  786. $is_local_image = $this->common['islocal'] == 'local' ? true : false;
  787. $core_attach = new CoreAttach;
  788. $core_attach = $core_attach->where('uniacid', $this->uniacid)->where('module_upload_dir', $this->common['module_upload_dir']);
  789. if (!$this->uniacid) {
  790. $core_attach = $core_attach->where('uid', \Auth::guard('admin')->user()->uid);
  791. }
  792. if ($groupid > 0) {
  793. $core_attach = $core_attach->where('group_id', $groupid);
  794. }
  795. if ($groupid == 0) {
  796. $core_attach = $core_attach->where('group_id', -1);
  797. }
  798. if ($year || $month) {
  799. $start_time = $month ? strtotime("{$year}-{$month}-01") : strtotime("{$year}-1-01");
  800. $end_time = $month ? strtotime('+1 month', $start_time) : strtotime('+12 month', $start_time);
  801. $core_attach = $core_attach->where('createtime', '>=', $start_time)->where('createtime', '<=', $end_time);
  802. }
  803. $core_attach = $core_attach->select('id','attachment')->where('type', 1);
  804. $core_attach = $core_attach->orderby('createtime', 'desc');
  805. // $count = $core_attach->count();
  806. $core_attach = $core_attach->paginate(15)->toArray();//->offset($offset)->limit($page_size)->get();
  807. foreach ($core_attach['data'] as &$meterial) {
  808. if ($this->common['islocal']) {
  809. $meterial['url'] = yz_tomedia($meterial['attachment']);
  810. unset($meterial['uid']);
  811. } else {
  812. $meterial['attach'] = yz_tomedia($meterial['attachment'], true);
  813. $meterial['url'] = $meterial['attach'];
  814. }
  815. }
  816. return $core_attach;
  817. // $pager = pagination($count, $page, $page_size,'',$context = array('before' => 5, 'after' => 4, 'isajax' => '1'));
  818. // $result = array('items' => $core_attach, 'pager' => $pager);
  819. //
  820. // iajax(0, $result);
  821. }
  822. public function getWqImage()
  823. {
  824. $year = request()->year;
  825. $month = intval(request()->month);
  826. $page = max(1, intval(request()->page));
  827. $groupid = intval(request()->group_id);
  828. $page_size = 33;
  829. $is_local_image = $this->common['islocal'] == 'local' ? true : false;
  830. if ($page<=1) {
  831. $page = 0;
  832. $offset = ($page)*$page_size;
  833. } else {
  834. $offset = ($page-1)*$page_size;
  835. }
  836. $core_attach = new CoreAttach;
  837. $core_attach = $core_attach->where('uniacid', $this->uniacid)->where('module_upload_dir', $this->common['module_upload_dir']);
  838. if (!$this->uniacid) {
  839. $core_attach = $core_attach->where('uid', \Auth::guard('admin')->user()->uid);
  840. }
  841. if ($groupid > 0) {
  842. $core_attach = $core_attach->where('group_id', $groupid);
  843. }
  844. if ($groupid == 0) {
  845. $core_attach = $core_attach->where('group_id', -1);
  846. }
  847. if ($year || $month) {
  848. $start_time = $month ? strtotime("{$year}-{$month}-01") : strtotime("{$year}-1-01");
  849. $end_time = $month ? strtotime('+1 month', $start_time) : strtotime('+12 month', $start_time);
  850. $core_attach = $core_attach->where('createtime', '>=', $start_time)->where('createtime', '<=', $end_time);
  851. }
  852. $core_attach = $core_attach->where('type', 1);
  853. $core_attach = $core_attach->orderby('createtime', 'desc');
  854. $count = $core_attach->count();
  855. $core_attach = $core_attach->offset($offset)->limit($page_size)->get();
  856. foreach ($core_attach as &$meterial) {
  857. if ($this->common['islocal']) {
  858. $meterial['url'] = yz_tomedia($meterial['attachment']);
  859. unset($meterial['uid']);
  860. } else {
  861. $meterial['attach'] = yz_tomedia($meterial['attachment'], true);
  862. $meterial['url'] = $meterial['attach'];
  863. }
  864. }
  865. $pager = pagination($count, $page, $page_size,'',$context = array('before' => 5, 'after' => 4, 'isajax' => '1'));
  866. $result = array('items' => $core_attach, 'pager' => $pager);
  867. iajax(0, $result);
  868. }
  869. public function getNewImage()
  870. {
  871. $core_attach = new \app\platform\modules\application\models\CoreAttach();
  872. if (request()->year != '不限') {
  873. $search['year'] = request()->year;
  874. }
  875. if(request()->month != '不限') {
  876. $search['month'] = request()->month;
  877. }
  878. $core_attach = $core_attach->search($search);
  879. $core_attach = $core_attach->where('uniacid', $this->uniacid)->where('module_upload_dir', $this->common['module_upload_dir']);
  880. if (!$this->uniacid) {
  881. $core_attach = $core_attach->where('uid', \Auth::guard('admin')->user()->uid);
  882. }
  883. //type = 1 图片
  884. $core_attach = $core_attach->select('id','attachment')->where('type', 1);
  885. $core_attach = $core_attach->orderby('created_at', 'desc')->paginate(15);
  886. foreach ($core_attach as &$meterial) {
  887. $meterial['url'] = yz_tomedia($meterial['attachment']);
  888. unset($meterial['uid']);
  889. }
  890. return $core_attach->toArray();
  891. }
  892. public function getVideo()
  893. {
  894. if (config('app.framework') == 'platform') {
  895. $core_attach = new \app\platform\modules\application\models\CoreAttach();
  896. if (request()->year != '不限') {
  897. $search['year'] = request()->year;
  898. }
  899. if(request()->month != '不限') {
  900. $search['month'] = request()->month;
  901. }
  902. $core_attach = $core_attach->search($search);
  903. $core_attach = $core_attach->where('uniacid', $this->uniacid)->where('module_upload_dir', $this->common['module_upload_dir']);
  904. if (!$this->uniacid) {
  905. $core_attach = $core_attach->where('uid', \Auth::guard('admin')->user()->uid);
  906. }
  907. //type = 3 视频
  908. $core_attach = $core_attach->where('type', 3);
  909. $core_attach = $core_attach->orderby('created_at', 'desc')->paginate(33);
  910. foreach ($core_attach as &$meterial) {
  911. $meterial['url'] = yz_tomedia($meterial['attachment']);
  912. unset($meterial['uid']);
  913. }
  914. return $this->successJson('ok', $core_attach);
  915. } else {
  916. $core_attach = new CoreAttach();
  917. $page_index = max(1, request()->page);
  918. $page_size = 5;
  919. if ($page_index<=1) {
  920. $page_index = 0;
  921. $offset = ($page_index)*$page_size;
  922. } else {
  923. $offset = ($page_index-1)*$page_size;
  924. }
  925. if (!$this->uniacid) {
  926. $core_attach = $core_attach->where('uid', \Auth::guard('admin')->user()->uid);
  927. }
  928. $total = $core_attach->count();
  929. $core_attach = $core_attach
  930. ->where('type', 3)
  931. ->where('uniacid', $this->uniacid)
  932. ->where('module_upload_dir', $this->common['module_upload_dir'])
  933. ->orderby('createtime', 'desc')
  934. ->offset($offset)
  935. ->limit(24)
  936. ->get();
  937. foreach ($core_attach as &$meterial) {
  938. $meterial['url'] = yz_tomedia($meterial['attachment']);
  939. unset($meterial['uid']);
  940. }
  941. $pager = pagination($total, 1, 24, '', $context = array('before' => 5, 'after' => 4, 'isajax' => '1'));
  942. $result = array('items' => $core_attach, 'pager' => $pager);
  943. iajax(0, $result);
  944. }
  945. }
  946. public function delete()
  947. {
  948. $uid = \Auth::guard('admin')->user()->uid;
  949. $id = request()->id;
  950. if (!is_array($id)) {
  951. $id = array(intval($id));
  952. }
  953. $id = safe_gpc_array($id);
  954. if (config('app.framework') == 'platform') {
  955. $setting = SystemSetting::settingLoad('global', 'system_global');
  956. $remote = SystemSetting::settingLoad('remote', 'system_remote');
  957. $core_attach = \app\platform\modules\application\models\CoreAttach::where('id', $id);
  958. if (!$this->uniacid) {
  959. $core_attach = $core_attach->where('uid', $uid);
  960. } else {
  961. $core_attach = $core_attach->where('uniacid', $this->uniacid);
  962. }
  963. $core_attach = $core_attach->first();
  964. if ($core_attach['upload_type']) {
  965. $status = file_remote_delete($core_attach['attachment'], $core_attach['upload_type'], $remote);
  966. } else {
  967. $status = file_delete($core_attach['attachment']);
  968. }
  969. if (is_error($status)) {
  970. return $this->errorJson($status['message']);
  971. }
  972. if ($core_attach->delete()) {
  973. return $this->successJson('删除成功');
  974. } else {
  975. return $this->errorJson('删除数据表数据失败');
  976. }
  977. } else {
  978. $core_attach = CoreAttach::where('id', $id);
  979. if (!$this->uniacid) {
  980. $core_attach = $core_attach->where('uid', $uid);
  981. } else {
  982. $core_attach = $core_attach->where('uniacid', $this->uniacid);
  983. }
  984. $core_attach = $core_attach->first();
  985. if ($core_attach['upload_type']) {
  986. $status = file_remote_delete($core_attach['attachment']);
  987. } else {
  988. $status = file_delete($core_attach['attachment']);
  989. }
  990. if (is_error($status)) {
  991. return $this->errorJson($status['message']);
  992. }
  993. if ($core_attach->delete()) {
  994. return $this->successJson('删除成功');
  995. } else {
  996. return $this->errorJson('删除数据表数据失败');
  997. }
  998. }
  999. }
  1000. public function common()
  1001. {
  1002. $dest_dir = request()->dest_dir;
  1003. $type = in_array(request()->upload_type, array('image','audio','video')) ? request()->upload_type : 'image';
  1004. $option = array_elements(array('uploadtype', 'global', 'dest_dir'), $_POST);
  1005. $option['width'] = intval($option['width']);
  1006. $option['global'] = request()->global;
  1007. if (preg_match('/^[a-zA-Z0-9_\/]{0,50}$/', $dest_dir, $out)) {
  1008. $dest_dir = trim($dest_dir, '/');
  1009. $pieces = explode('/', $dest_dir);
  1010. if(count($pieces) > 3){
  1011. $dest_dir = '';
  1012. }
  1013. } else {
  1014. $dest_dir = '';
  1015. }
  1016. $module_upload_dir = '';
  1017. if($dest_dir != '') {
  1018. $module_upload_dir = sha1($dest_dir);
  1019. }
  1020. if ($option['global']) {
  1021. $folder = "{$type}s/global/";
  1022. if ($dest_dir) {
  1023. $folder .= '' . $dest_dir . '/';
  1024. }
  1025. } else {
  1026. $folder = "{$type}s/{$this->uniacid}";
  1027. if (!$dest_dir) {
  1028. $folder .= '/' . date('Y/m/');
  1029. } else {
  1030. $folder .= '/' . $dest_dir . '/';
  1031. }
  1032. }
  1033. return [
  1034. 'dest_dir' => $dest_dir,
  1035. 'module_upload_dir' => $module_upload_dir,
  1036. 'type' => $type,
  1037. 'options' => $option,
  1038. 'folder' => $folder,
  1039. ];
  1040. }
  1041. public function uploadVue()
  1042. {
  1043. $type = request()->upload_type;
  1044. $file = request()->file('file');
  1045. if (!$file) {
  1046. return $this->errorJson('请传入正确参数.');
  1047. }
  1048. if (!$file->isValid()) {
  1049. return $this->errorJson('上传失败.');
  1050. }
  1051. switch ($type) {
  1052. case 'video' :
  1053. $dir = 'videos';
  1054. break;
  1055. case 'audio' :
  1056. $dir = 'audio';
  1057. break;
  1058. default :
  1059. $dir = 'image';
  1060. break;
  1061. }
  1062. $uploadService = new UploadService();
  1063. $upload_res = $uploadService->upload($file, $type, $dir);
  1064. return json_encode(['location' => $upload_res['absolute_path']]);
  1065. }
  1066. }