AttachmentController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace App\Http\Controllers\Admin\Base;
  3. use App\Http\Controllers\Admin\Controller;
  4. use App\Services\Base\Attachment;
  5. use App\Models\BaseAttachmentModel;
  6. use Request, Response;
  7. class AttachmentController extends Controller
  8. {
  9. private $_serviceAttachment;
  10. private $_serviceAttachmentBbs;
  11. private $_serviceForumAttachment;
  12. private $_serviceForumAttachmentUnused;
  13. public function __construct()
  14. {
  15. if( !$this->_serviceAttachment ) $this->_serviceAttachment = new Attachment();
  16. }
  17. /**
  18. * SWFUpload文件上传
  19. */
  20. public function upload()
  21. {
  22. $request = Request::all();
  23. //通过上传控件ID,区分文件上传类型
  24. if(isset($request['elementid']) && substr($request['elementid'], 0, 15) == 'upload_template'){
  25. $this->_uploadTemplate($request);
  26. //上传到本地
  27. }elseif(isset($request['position']) && $request['position'] == 'local'){
  28. $this->_uploadToServer($request);
  29. //上传专题文件
  30. }elseif(isset($request['position']) && $request['position'] == 'special'){
  31. $this->_uploadSpecial($request);
  32. //上传到阿里云
  33. } else{
  34. $this->_uploadToAlioss($request);
  35. }
  36. }
  37. /**
  38. * 上传到本地
  39. */
  40. private function _uploadToServer($request)
  41. {
  42. $return = $this->_serviceAttachment->localUpload('imgFile', $request);
  43. if($return['code']=='200'){
  44. echo json_encode(["error"=> 0,"url" => config('app.url').$return['fileurl']]);exit;
  45. }else{
  46. echo json_encode(["error" => 1,"message" => $return['message']]);exit;
  47. }
  48. }
  49. /**
  50. * 上传专题文件
  51. */
  52. private function _uploadSpecial($request)
  53. {
  54. $return = $this->_serviceAttachment->specialUpload('Filedata', $request);
  55. echo json_encode($return);exit;
  56. }
  57. /**
  58. * 上传到阿里云
  59. */
  60. private function _uploadToAlioss($request)
  61. {
  62. $return = [];
  63. if(isset($request['KindEditor'])){
  64. $data = $this->_serviceAttachment->aliUpload($request['field'], $request);
  65. if($data['code'] === 200){
  66. $return['error'] = 0;
  67. $return['url'] = $data['fileurl'];
  68. }else{
  69. $return['error'] = 1;
  70. $return['message'] = $data['message'];
  71. }
  72. }else{
  73. $return = $this->_serviceAttachment->aliUpload('Filedata', $request);
  74. }
  75. echo json_encode($return);exit;
  76. }
  77. /**
  78. * 控件上传
  79. */
  80. public function webupload()
  81. {
  82. $request = request()->all();
  83. $data = $this->_serviceAttachment->localUpload('file', $request, 'files');
  84. // return response()->json(array('data' => $data), 200);
  85. echo json_encode($data);exit;
  86. }
  87. /**
  88. * @api {get} /api/attachment/download/{md5} 下载文件(图片)
  89. * @apiDescription 下载文件(图片)(get code)
  90. * @apiGroup Attachment
  91. * @apiPermission none
  92. * @apiVersion 0.1.0
  93. * @apiParam {string} md5 图片md5码
  94. * @apiSuccessExample {json} Success-Response:
  95. * HTTP/1.1 200 OK
  96. * {
  97. * 文件二进制码
  98. * }
  99. * @apiErrorExample {json} Error-Response:
  100. * HTTP/1.1 404 Not found
  101. */
  102. public function download()
  103. {
  104. $request = Request::all();
  105. $attachment = BaseAttachmentModel::where(['md5' => $request['md5']])->first();
  106. if (!$attachment) {
  107. return view('errors.404');
  108. }
  109. return Response::download($attachment->path, $attachment->name, [
  110. 'Content-type' => $attachment->file_type,
  111. 'Accept-Ranges' => 'bytes',
  112. 'Accept-Length' => $attachment->size,
  113. ]);
  114. }
  115. private function _createAttachmentRecord($data)
  116. {
  117. $uid = Request::input('uid');
  118. $aid = $this->_serviceForumAttachment->create(['tid' => 0, 'pid' => 0, 'uid' => $uid, 'tableid' => 127]);
  119. if($aid){
  120. $info = [
  121. 'aid' => $aid,
  122. 'uid' => $uid,
  123. 'dateline' => SYSTEM_TIME,
  124. 'filename' => $data['filename'],
  125. 'filesize' => $data['filesize'],
  126. 'attachment' => $data['attachment'],
  127. 'isimage' => (isset($data['width']) && $data['width']) ? 1 : 0,
  128. 'remote' => 1,
  129. 'width' => isset($data['width']) ? $data['width'] : 0,
  130. 'thumb' => 0,
  131. ];
  132. if(!$this->_serviceForumAttachmentUnused->create($info)){
  133. return 0;
  134. }
  135. }
  136. return $aid;
  137. }
  138. }