AttachmentController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Helper\AttachmentHelper;
  4. use App\Models\BaseAttachmentModel;
  5. use Illuminate\Http\Request;
  6. use App\Services\Base\Attachment;
  7. use App\Services\Base\ErrorCode;
  8. use Validator, Response;
  9. class AttachmentController extends Controller
  10. {
  11. use AttachmentHelper;
  12. /**
  13. * @api {get} /api/attachment/download/{md5} 下载文件(图片)
  14. * @apiDescription 下载文件(图片)(get code)
  15. * @apiGroup Attachment
  16. * @apiPermission none
  17. * @apiVersion 0.1.0
  18. * @apiParam {string} md5 图片md5码
  19. * @apiSuccessExample {json} Success-Response:
  20. * HTTP/1.1 200 OK
  21. * {
  22. * 文件二进制码
  23. * }
  24. * @apiErrorExample {json} Error-Response:
  25. * HTTP/1.1 404 Not found
  26. */
  27. public function download($md5)
  28. {
  29. $attachment = Attachment::where(['md5' => $md5])->first();
  30. if (!$attachment) {
  31. return view('errors.404');
  32. }
  33. return Response::download($attachment->path, $attachment->name, [
  34. 'Content-type' => $attachment->file_type,
  35. 'Accept-Ranges' => 'bytes',
  36. 'Accept-Length' => $attachment->size,
  37. ]);
  38. }
  39. /**
  40. * @api {post} /api/attachment/upload 通用上传接口
  41. * @apiDescription 通用上传接口
  42. * @apiGroup Attachment
  43. * @apiPermission none
  44. * @apiVersion 0.1.0
  45. * @apiParam {string} tag 附件标签 avatar video dream
  46. * @apiParam {File} file 附件(可以多个,使用file.xxx,可返回多个)[默认大小【10M】, 类型图片png jpg gif,视频类型mp4]
  47. * @apiSuccessExample {json} Success-Response:
  48. * HTTP/1.1 200 OK
  49. * {
  50. * "state": true,
  51. * "code": 0,
  52. * "message": "",
  53. * "data": [
  54. * "file": "f72e7dad80f597ed6621a009e82243ad",
  55. * //文件访问url http://localhost/attachment/f72e7dad80f597ed6621a009e82243ad
  56. * ]
  57. * }
  58. * @apiErrorExample {json} Error-Response:
  59. * HTTP/1.1 400 Bad Request
  60. * {
  61. * "state": false,
  62. * "code": 1000,
  63. * "message": "传入参数不正确",
  64. * "data": null or []
  65. * }
  66. * 可能出现的错误代码:
  67. * 200 SAVE_USER_FAILED 保存用户数据失败
  68. * 201 ATTACHMENT_MKDIR_FAILED 创建附件目录失败
  69. * 202 ATTACHMENT_UPLOAD_INVALID 上传附件文件无效
  70. * 203 ATTACHMENT_SAVE_FAILED 保存附件失败
  71. * 204 ATTACHMENT_MOVE_FAILED 移动附件失败
  72. * 205 ATTACHMENT_DELETE_FAILED 删除附件文件失败
  73. * 206 ATTACHMENT_RECORD_DELETE_FAILED 删除附件记录失败
  74. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  75. * 1101 INCORRECT_VERIFY_CODE 输入验证码错误
  76. * 1105 USER_DOES_NOT_EXIST 用户不存在
  77. * 1200 ATTACHMENT_UPLOAD_FAILED 附件上传失败
  78. * 1201 ATTACHMENT_SIZE_EXCEEDED 附件大小超过限制
  79. * 1202 ATTACHMENT_MIME_NOT_ALLOWED 附件类型不允许
  80. * 1203 ATTACHMENT_NOT_EXIST 附件不存在
  81. */
  82. public function upload(Request $request) {
  83. \Log::info($request->all());
  84. $validator = Validator::make($request->all(),
  85. [
  86. 'tag' => 'required|alpha_dash',
  87. ],
  88. [
  89. 'tag.required' => 'tag必填',
  90. 'tag.alpha_dash' => 'tag只能为字母数字中/下划线',
  91. ]
  92. );
  93. if ($validator->fails()) {
  94. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '', $validator->messages());
  95. }
  96. $result = $this->uploadAttachment($request, $request->get('file'), $request->get('tag'), 10 * 1024 * 1024, [
  97. 'image/jpeg',
  98. 'image/png',
  99. 'image/gif',
  100. 'video/mp4',
  101. ]);
  102. if (is_array($result)) {
  103. return $this->api($result);
  104. } elseif (is_string($result)) {
  105. return $this->api(['file' => $result]);
  106. } else {
  107. return $this->error($result);
  108. }
  109. }
  110. /**
  111. * @api {get} /api/attachment/delete/{md5} 删除文件(图片)
  112. * @apiDescription 删除文件(图片)
  113. * @apiGroup Attachment
  114. * @apiPermission Passport
  115. * @apiVersion 0.1.0
  116. * @apiParam {string} md5 图片md5码
  117. * @apiSuccessExample {json} Success-Response:
  118. * HTTP/1.1 200 OK
  119. * {
  120. * "state": true,
  121. * "code": 0,
  122. * "message": "",
  123. * "data": {
  124. * "result": true/false
  125. * }
  126. * }
  127. * @apiErrorExample {json} Error-Response:
  128. * HTTP/1.1 400 Bad Request
  129. * {
  130. * "state": false,
  131. * "code": 1000,
  132. * "message": "传入参数不正确",
  133. * "data": null or []
  134. * }
  135. * 可能出现的错误代码:
  136. * 205 ATTACHMENT_DELETE_FAILED 删除附件文件失败
  137. * 206 ATTACHMENT_RECORD_DELETE_FAILED 删除附件记录失败
  138. * 1203 ATTACHMENT_NOT_EXIST 附件不存在
  139. */
  140. public function delete($md5) {
  141. $result = $this->deleteAttachment($md5);
  142. if ($result === 0) {
  143. return $this->api(['result' => true]);
  144. } else {
  145. return $this->error($result);
  146. }
  147. }
  148. public function index(){
  149. $result = BaseAttachmentModel::all()->sortByDesc('id');
  150. return $this->api($result);
  151. }
  152. }