UploadV3Controller.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <?php
  2. namespace app\backend\modules\upload\controllers;
  3. use app\backend\modules\upload\models\CoreAttach;
  4. use app\common\components\BaseController;
  5. use app\common\exceptions\ShopException;
  6. use app\common\services\ImageZip;
  7. use app\common\services\upload\UploadService;
  8. use app\platform\modules\application\models\CoreAttachTags;
  9. use app\platform\modules\system\models\SystemSetting;
  10. use getID3;
  11. class UploadV3Controller extends BaseController
  12. {
  13. protected $isPublic = true;
  14. protected $uniacid;
  15. protected $set;
  16. public function __construct()
  17. {
  18. $this->uniacid = \YunShop::app()->uniacid ?: 0;
  19. $this->set = UploadService::getSetting();
  20. }
  21. public function upload()
  22. {
  23. $file = request()->file('file');
  24. $type = request()->upload_type;
  25. $tagId = request()->tag_id;
  26. if (!$file) {
  27. return $this->errorJson('文件上传失败.');
  28. }
  29. if (!$file->isValid()) {
  30. return $this->errorJson('文件上传失败.');
  31. }
  32. // 获取文件相关信息
  33. $originalName = $file->getClientOriginalName(); // 文件原名
  34. $realPath = $file->getRealPath(); //临时文件的绝对路径
  35. $ext = $file->getClientOriginalExtension(); //文件后缀
  36. $uploadService = new UploadService();
  37. $upload_setting = $uploadService->getSetting();
  38. $auth_uid = \YunShop::app()->uid?:1;
  39. global $_W;
  40. if ($type == 'image') {
  41. try {
  42. $upload_res = $uploadService->upload($file, $type);
  43. } catch (ShopException $exception) {
  44. return $this->errorJson($exception->getMessage());
  45. }
  46. if (config('app.framework') == 'platform') {
  47. $data = [
  48. 'uniacid' => $this->uniacid,
  49. 'uid' => $auth_uid,
  50. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  51. 'attachment' => $upload_res['relative_path'],
  52. 'type' => 1,
  53. 'module_upload_dir' => '',
  54. 'group_id' => (int)$this->uniacid,
  55. 'upload_type' => $upload_setting['remote']['type'],
  56. 'tag_id' => $tagId
  57. ];
  58. \app\platform\modules\application\models\CoreAttach::create($data);
  59. } else {
  60. $data = [
  61. 'uniacid' => $this->uniacid,
  62. 'uid' => isset($_W['uid']) ? $_W['uid'] : 1,
  63. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  64. 'attachment' => $upload_res['relative_path'],
  65. 'type' => 1,
  66. 'createtime' => TIMESTAMP,
  67. 'module_upload_dir' => '',
  68. 'group_id' => 0,
  69. 'tag_id' => $tagId
  70. ];
  71. CoreAttach::create($data);
  72. }
  73. return $this->successJson('上传成功', [
  74. 'name' => $originalName,
  75. 'ext' => $ext,
  76. 'filename' => $upload_res['file_name'],
  77. 'attachment' => $upload_res['relative_path'],
  78. 'url' => $upload_res['absolute_path'],
  79. 'is_image' => 1,
  80. 'filesize' => 'null',
  81. 'group_id' => (int)$this->uniacid,
  82. 'state' => 'SUCCESS'
  83. ]);
  84. } elseif ($type == 'video') {
  85. try {
  86. $upload_res = $uploadService->upload($file, $type, 'videos');
  87. } catch (ShopException $exception) {
  88. return $this->errorJson($exception->getMessage());
  89. }
  90. if (config('app.framework') == 'platform') {
  91. $getID3 = new getID3();
  92. $ThisFileInfo = $getID3->analyze($realPath); //分析文件,$path为音频文件的地址
  93. $timeline = $ThisFileInfo['playtime_seconds']; //这个获得的便是音频文件的时长
  94. $data = [
  95. 'uniacid' => $this->uniacid,
  96. 'uid' => $auth_uid,
  97. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  98. 'attachment' => $upload_res['relative_path'],
  99. 'type' => 3,
  100. 'module_upload_dir' => '',
  101. 'group_id' => (int)$this->uniacid,
  102. 'upload_type' => $upload_setting['remote']['type'],
  103. 'tag_id' => $tagId,
  104. 'timeline' => $timeline
  105. ];
  106. \app\platform\modules\application\models\CoreAttach::create($data);
  107. return $this->successJson('上传成功', [
  108. 'name' => $originalName,
  109. 'ext' => $ext,
  110. 'filename' => $upload_res['file_name'],
  111. 'attachment' => $upload_res['relative_path'],
  112. 'url' => $upload_res['absolute_path'],
  113. 'is_image' => 0,
  114. 'filesize' => 'null',
  115. 'group_id' => (int)$this->uniacid,
  116. 'timeline' => $timeline
  117. ]);
  118. } else {
  119. $getID3 = new getID3();
  120. $ThisFileInfo = $getID3->analyze($realPath); //分析文件,$path为音频文件的地址
  121. $timeline=$ThisFileInfo['playtime_seconds']; //这个获得的便是音频文件的时长
  122. $data = [
  123. 'uniacid' => $this->uniacid,
  124. 'uid' => isset($_W['uid']) ? $_W['uid'] : 1,
  125. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  126. 'attachment' => $upload_res['relative_path'],
  127. 'type' => 3,
  128. 'createtime' => TIMESTAMP,
  129. 'module_upload_dir' => '',
  130. 'group_id' => 0,
  131. 'tag_id' => $tagId,
  132. 'timeline' => $timeline
  133. ];
  134. CoreAttach::create($data);
  135. return $this->successJson('上传成功', [
  136. 'name' => $originalName,
  137. 'ext' => $ext,
  138. 'filename' => $upload_res['file_name'],
  139. 'attachment' => $upload_res['relative_path'],
  140. 'url' => $upload_res['absolute_path'],
  141. 'is_image' => 0,
  142. 'filesize' => 'null',
  143. 'group_id' => (int)$this->uniacid,
  144. ]);
  145. }
  146. } elseif ($type == 'audio') {
  147. try {
  148. $upload_res = $uploadService->upload($file, $type, 'audios');
  149. } catch (ShopException $exception) {
  150. return $this->errorJson($exception->getMessage());
  151. }
  152. if (config('app.framework') == 'platform') {
  153. $getID3 = new getID3();
  154. $ThisFileInfo = $getID3->analyze($realPath); //分析文件,$path为音频文件的地址
  155. $timeline = $ThisFileInfo['playtime_seconds']; //这个获得的便是音频文件的时长
  156. $data = [
  157. 'uniacid' => $this->uniacid,
  158. 'uid' => $auth_uid,
  159. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  160. 'attachment' => $upload_res['relative_path'],
  161. 'type' => 2,
  162. 'module_upload_dir' => '',
  163. 'group_id' => (int)$this->uniacid,
  164. 'upload_type' => $upload_setting['remote']['type'],
  165. 'tag_id' => $tagId,
  166. 'timeline' => $timeline
  167. ];
  168. \app\platform\modules\application\models\CoreAttach::create($data);
  169. return $this->successJson('上传成功', [
  170. 'name' => $originalName,
  171. 'ext' => $ext,
  172. 'filename' => $upload_res['file_name'],
  173. 'attachment' => $upload_res['relative_path'],
  174. 'url' => $upload_res['absolute_path'],
  175. 'is_image' => 0,
  176. 'filesize' => 'null',
  177. 'group_id' => (int)$this->uniacid,
  178. 'timeline' => $timeline
  179. ]);
  180. } else {
  181. $getID3 = new getID3();
  182. $ThisFileInfo = $getID3->analyze($realPath); //分析文件,$path为音频文件的地址
  183. $timeline = $ThisFileInfo['playtime_seconds']; //这个获得的便是音频文件的时长
  184. $data = [
  185. 'uniacid' => $this->uniacid,
  186. 'uid' => isset($_W['uid']) ? $_W['uid'] : 1,
  187. 'filename' => safe_gpc_html(htmlspecialchars_decode($originalName, ENT_QUOTES)),
  188. 'attachment' => $upload_res['relative_path'],
  189. 'type' => 2,
  190. 'createtime' => TIMESTAMP,
  191. 'module_upload_dir' => '',
  192. 'group_id' => 0,
  193. 'tag_id' => $tagId,
  194. 'timeline' => $timeline
  195. ];
  196. CoreAttach::create($data);
  197. return $this->successJson('上传成功', [
  198. 'name' => $originalName,
  199. 'ext' => $ext,
  200. 'filename' => $upload_res['file_name'],
  201. 'attachment' => $upload_res['relative_path'],
  202. 'url' => $upload_res['absolute_path'],
  203. 'is_image' => 0,
  204. 'filesize' => 'null',
  205. 'group_id' => (int)$this->uniacid,
  206. ]);
  207. }
  208. } elseif ($type == 'file') {
  209. try {
  210. $upload_res = $uploadService->upload($file, $type, 'files');
  211. } catch (ShopException $exception) {
  212. return $this->errorJson($exception->getMessage());
  213. }
  214. return $this->successJson('上传成功', [
  215. 'name' => $originalName,
  216. 'ext' => $ext,
  217. 'filename' => $upload_res['file_name'],
  218. 'attachment' => $upload_res['relative_path'],
  219. 'url' => $upload_res['absolute_path'],
  220. 'is_image' => 0,
  221. 'filesize' => 'null',
  222. 'group_id' => (int)$this->uniacid,
  223. ]);
  224. }
  225. return true;
  226. }
  227. public function fetch()
  228. {
  229. $url = trim(request()->url);
  230. $resp = ihttp_get($url);
  231. if (!$resp) {
  232. return $this->errorJson('提取文件失败');
  233. }
  234. if (strexists($resp['headers']['Content-Type'], 'image')) {
  235. switch ($resp['headers']['Content-Type']) {
  236. case 'application/x-jpg':
  237. case 'image/jpeg':
  238. $ext = 'jpg';
  239. break;
  240. case 'image/png':
  241. $ext = 'png';
  242. break;
  243. case 'image/gif':
  244. $ext = 'gif';
  245. break;
  246. default:
  247. return $this->errorJson('提取资源失败, 资源文件类型错误.');
  248. break;
  249. }
  250. } else {
  251. return $this->errorJson('提取资源失败, 仅支持图片提取.');
  252. }
  253. $originName = pathinfo($url, PATHINFO_BASENAME);
  254. $newOriginalName = md5($originName . str_random(6)) . '.' . $ext;
  255. //本地上传
  256. $result = \Storage::disk('image')->put($newOriginalName, $resp['content']);
  257. if (!$result) {
  258. return $this->successJson('上传失败');
  259. }
  260. $url = \Storage::disk('image')->url($newOriginalName);
  261. if (config('app.framework') == 'platform') {
  262. $remote = $this->set['remote'];
  263. $data = [
  264. 'uniacid' => $this->uniacid,
  265. 'uid' => \YunShop::app()->uid,
  266. 'filename' => $newOriginalName,
  267. 'attachment' => $url,
  268. 'type' => 1,
  269. 'module_upload_dir' => '',
  270. 'group_id' => intval($this->uniacid),
  271. 'upload_type' => $remote['type'],
  272. 'tag_id' => 0
  273. ];
  274. \app\platform\modules\application\models\CoreAttach::create($data);
  275. //远程上传
  276. if ($remote['type'] != 0) {
  277. file_remote_upload($url, true, $remote);
  278. }
  279. return $this->successJson('上传成功', [
  280. 'img' => $url,
  281. 'img_url' => yz_tomedia($url),
  282. ]);
  283. } else {
  284. //全局配置
  285. global $_W;
  286. $remote = $this->set['remote'];
  287. $data = [
  288. 'uniacid' => $this->uniacid,
  289. 'uid' => isset($_W['uid']) ? $_W['uid'] : 1,
  290. 'filename' => $newOriginalName,
  291. 'attachment' => $url,
  292. 'type' => 1,
  293. 'createtime' => TIMESTAMP,
  294. 'module_upload_dir' => '',
  295. 'group_id' => 0,
  296. 'tag_id' => 0
  297. ];
  298. CoreAttach::create($data);
  299. //远程上传
  300. if ($remote['type'] != 0) {
  301. file_remote_upload_wq($url, true, $remote);
  302. }
  303. return $this->successJson('上传成功', [
  304. 'img' => $url,
  305. 'img_url' => yz_tomedia($url),
  306. ]);
  307. }
  308. }
  309. public function getImage()
  310. {
  311. if (config('app.framework') == 'platform') {
  312. $result = $this->getNewImage();
  313. } else {
  314. $result = $this->getWqImageV2();
  315. }
  316. return $this->successJson('ok', $result);
  317. }
  318. public function getWqImageV2()
  319. {
  320. $year = request()->year;
  321. $month = intval(request()->month);
  322. $pageSize = request()->pageSize;
  323. $core_attach = new CoreAttach;
  324. $core_attach = $core_attach->where('uniacid', $this->uniacid);
  325. $tagId = request()->tag_id;
  326. if (is_numeric($tagId)) {
  327. if ($tagId === 0) {
  328. $core_attach = $core_attach->where(function($query) {
  329. $query->where('tag_id', 0)->orWhere('tag_id', null);
  330. });
  331. } else {
  332. $core_attach = $core_attach->where('tag_id', $tagId);
  333. }
  334. }
  335. global $_W;
  336. if (\YunShop::app()->isfounder !== true) {
  337. $core_attach = $core_attach->where('uid', $_W['uid']?:1);
  338. }
  339. if ($year || $month) {
  340. $start_time = $month ? strtotime("{$year}-{$month}-01") : strtotime("{$year}-1-01");
  341. $end_time = $month ? strtotime('+1 month', $start_time) : strtotime('+12 month', $start_time);
  342. $core_attach = $core_attach->where('createtime', '>=', $start_time)->where('createtime', '<=', $end_time);
  343. }
  344. $core_attach = $core_attach->select('id','attachment')->where('type', 1);
  345. $core_attach = $core_attach->orderby('createtime', 'desc')->orderby('id', 'desc');
  346. $core_attach->search(request()->date);
  347. $core_attach = $core_attach->paginate($pageSize);
  348. if (!empty($core_attach)) {
  349. $core_attach = $core_attach->toArray();
  350. }
  351. foreach ($core_attach['data'] as &$attach) {
  352. $attach['attach'] = yz_tomedia($attach['attachment']);
  353. $attach['url'] = $attach['attach'];
  354. }
  355. return $core_attach;
  356. }
  357. public function getWqImage()
  358. {
  359. $year = request()->year;
  360. $month = intval(request()->month);
  361. $page = max(1, intval(request()->page));
  362. $page_size = 33;
  363. if ($page <= 1) {
  364. $page = 0;
  365. $offset = ($page)*$page_size;
  366. } else {
  367. $offset = ($page-1)*$page_size;
  368. }
  369. $core_attach = new CoreAttach;
  370. $core_attach = $core_attach->where('uniacid', $this->uniacid);
  371. global $_W;
  372. if (\YunShop::app()->isfounder !== true) {
  373. $core_attach = $core_attach->where('uid', $_W['uid']?:1);
  374. }
  375. if ($year || $month) {
  376. $start_time = $month ? strtotime("{$year}-{$month}-01") : strtotime("{$year}-1-01");
  377. $end_time = $month ? strtotime('+1 month', $start_time) : strtotime('+12 month', $start_time);
  378. $core_attach = $core_attach->where('createtime', '>=', $start_time)->where('createtime', '<=', $end_time);
  379. }
  380. $core_attach->search(request()->date);
  381. $core_attach = $core_attach->where('type', 1);
  382. $core_attach = $core_attach->orderby('createtime', 'desc');
  383. $count = $core_attach->count();
  384. $core_attach = $core_attach->offset($offset)->limit($page_size)->get();
  385. foreach ($core_attach as &$attach) {
  386. $attach['attach'] = yz_tomedia($attach['attachment']);
  387. $attach['url'] = $attach['attach'];
  388. }
  389. $pager = pagination($count, $page, $page_size,'',$context = array('before' => 5, 'after' => 4, 'isajax' => '1'));
  390. $result = array('items' => $core_attach, 'pager' => $pager);
  391. iajax(0, $result);
  392. }
  393. public function getNewImage()
  394. {
  395. $core_attach = new \app\platform\modules\application\models\CoreAttach();
  396. $pageSize = request()->pageSize;
  397. $core_attach = $core_attach->search(request()->date)->where('uniacid', $this->uniacid)->where('type', 1);
  398. if ($tagId = request()->tag_id AND is_numeric($tagId)) {
  399. if ($tagId === 0) {
  400. $core_attach = $core_attach->where(function($query) {
  401. $query->where('tag_id', 0)->orWhere('tag_id', null);
  402. });
  403. } else {
  404. $core_attach = $core_attach->where('tag_id', $tagId);
  405. }
  406. }
  407. if (\YunShop::app()->isfounder !== true) {
  408. $core_attach = $core_attach->where('uid', \YunShop::app()->uid);
  409. }
  410. $core_attach = $core_attach->select('id','attachment','filename')
  411. ->orderby('created_at', 'desc')
  412. ->orderby('id', 'desc')
  413. ->paginate($pageSize);
  414. foreach ($core_attach as &$attach) {
  415. $attach['url'] = yz_tomedia($attach['attachment']);
  416. }
  417. return $core_attach;
  418. }
  419. public function getVideo()
  420. {
  421. $date = request()->date;
  422. $tag_id = request()->tag_id;
  423. $page_size = request()->pageSize;
  424. $search = ['year'=>$date['year'],'month'=>$date['month'],'tag_id'=>$tag_id];
  425. if (config('app.framework') == 'platform') {
  426. $core_attach = \app\platform\modules\application\models\CoreAttach::search($search);
  427. $core_attach = $core_attach->orderby('created_at', 'desc');
  428. } else {
  429. $core_attach = CoreAttach::search($search);
  430. $core_attach = $core_attach->orderby('createtime', 'desc');
  431. }
  432. $core_attach = $core_attach->where('type', 3)->paginate($page_size);
  433. foreach ($core_attach as &$attach) {
  434. $attach['url'] = yz_tomedia($attach['attachment']);
  435. }
  436. return $this->successJson('ok', $core_attach);
  437. }
  438. public function getAudio()
  439. {
  440. if (config('app.framework') == 'platform') {
  441. $core_attach = new \app\platform\modules\application\models\CoreAttach();
  442. if (request()->year != '不限') {
  443. $search['year'] = request()->year;
  444. }
  445. if (request()->month != '不限') {
  446. $search['month'] = request()->month;
  447. }
  448. $pageSize = request()->pageSize;
  449. $core_attach = $core_attach->search($search)->where('uniacid', $this->uniacid);
  450. $tagTitle = '';
  451. if ($tagId = request()->tag_id AND !empty($tagId)) {
  452. $core_attach->where('tag_id', $tagId);
  453. $tag = CoreAttachTags::find($tagId);
  454. $tagTitle = $tag?$tag->title:'';
  455. }
  456. if ($tagTitle != '未分组') {
  457. $core_attach = $core_attach->where('uid', \YunShop::app()->uid);
  458. }
  459. //type = 2 音频
  460. $core_attach = $core_attach->where('type', 2);
  461. $core_attach = $core_attach->orderby('created_at', 'desc')->paginate($pageSize);
  462. foreach ($core_attach as &$attach) {
  463. $attach['url'] = yz_tomedia($attach['attachment']);
  464. }
  465. return $this->successJson('ok', $core_attach);
  466. } else {
  467. $core_attach = new CoreAttach();
  468. $page_index = max(1, request()->page);
  469. $page_size = 5;
  470. if ($page_index <= 1) {
  471. $page_index = 0;
  472. $offset = $page_index * $page_size;
  473. } else {
  474. $offset = ($page_index - 1) * $page_size;
  475. }
  476. $core_attach = $core_attach->where(['type'=>2,'uniacid'=>$this->uniacid]);
  477. if (!$this->uniacid) {
  478. $core_attach = $core_attach->where('uid', \YunShop::app()->uid);
  479. }
  480. $total = $core_attach->count();
  481. if (request()->platform_type == '1') {
  482. $search['year'] = request()->year;
  483. $search['month'] = request()->month;
  484. $list = $core_attach->search($search)->orderby('createtime', 'desc')->paginate(8);
  485. $list->map(function ($l) {
  486. $l->url = yz_tomedia($l->attachment);
  487. });
  488. return $this->successJson('ok', $list);
  489. }
  490. $core_attach = $core_attach->orderby('createtime', 'desc')->offset($offset)->limit(24)->get();
  491. foreach ($core_attach as &$attach) {
  492. $attach['url'] = yz_tomedia($attach['attachment']);
  493. }
  494. $pager = pagination($total, 1, 24, '', $context = array('before' => 5, 'after' => 4, 'isajax' => '1'));
  495. $result = array('items' => $core_attach, 'pager' => $pager);
  496. iajax(0, $result);
  497. }
  498. return true;
  499. }
  500. public function delete()
  501. {
  502. $id = request()->id;
  503. if (!is_array($id)) {
  504. $id = array(intval($id));
  505. }
  506. $id = safe_gpc_array($id);
  507. if (config('app.framework') == 'platform') {
  508. $core_attach = \app\platform\modules\application\models\CoreAttach::find($id);
  509. if ($core_attach['upload_type']) {
  510. $status = file_remote_delete($core_attach['attachment'], $core_attach['upload_type'], $this->set['remote']);
  511. } else {
  512. $status = file_delete($core_attach['attachment']);
  513. }
  514. if (is_error($status)) {
  515. return $this->errorJson($status['message']);
  516. }
  517. if (!$core_attach->delete()) {
  518. return $this->errorJson('删除数据表数据失败');
  519. }
  520. return $this->successJson('删除成功');
  521. } else {
  522. $core_attach = CoreAttach::find($id);
  523. if ($core_attach['upload_type']) {
  524. $status = file_remote_delete($core_attach['attachment']);
  525. } else {
  526. $status = file_delete($core_attach['attachment']);
  527. }
  528. if (is_error($status)) {
  529. return $this->errorJson($status['message']);
  530. }
  531. if (!$core_attach->delete()) {
  532. return $this->errorJson('删除数据表数据失败');
  533. }
  534. return $this->successJson('删除成功');
  535. }
  536. }
  537. }