AttachmentController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. * "real": "真实路径"
  56. * //文件访问url http://localhost/attachment/f72e7dad80f597ed6621a009e82243ad
  57. * ]
  58. * }
  59. * @apiErrorExample {json} Error-Response:
  60. * HTTP/1.1 400 Bad Request
  61. * {
  62. * "state": false,
  63. * "code": 1000,
  64. * "message": "传入参数不正确",
  65. * "data": null or []
  66. * }
  67. * 可能出现的错误代码:
  68. * 200 SAVE_USER_FAILED 保存用户数据失败
  69. * 201 ATTACHMENT_MKDIR_FAILED 创建附件目录失败
  70. * 202 ATTACHMENT_UPLOAD_INVALID 上传附件文件无效
  71. * 203 ATTACHMENT_SAVE_FAILED 保存附件失败
  72. * 204 ATTACHMENT_MOVE_FAILED 移动附件失败
  73. * 205 ATTACHMENT_DELETE_FAILED 删除附件文件失败
  74. * 206 ATTACHMENT_RECORD_DELETE_FAILED 删除附件记录失败
  75. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  76. * 1101 INCORRECT_VERIFY_CODE 输入验证码错误
  77. * 1105 USER_DOES_NOT_EXIST 用户不存在
  78. * 1200 ATTACHMENT_UPLOAD_FAILED 附件上传失败
  79. * 1201 ATTACHMENT_SIZE_EXCEEDED 附件大小超过限制
  80. * 1202 ATTACHMENT_MIME_NOT_ALLOWED 附件类型不允许
  81. * 1203 ATTACHMENT_NOT_EXIST 附件不存在
  82. */
  83. public function upload(Request $request) {
  84. \Log::info($request->all());
  85. $validator = Validator::make($request->all(),
  86. [
  87. 'tag' => 'required|alpha_dash',
  88. ],
  89. [
  90. 'tag.required' => 'tag必填',
  91. 'tag.alpha_dash' => 'tag只能为字母数字中/下划线',
  92. ]
  93. );
  94. if ($validator->fails()) {
  95. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '', $validator->messages());
  96. }
  97. $result = $this->uploadAttachment($request, $request->get('file'), $request->get('tag'), 10 * 1024 * 1024, [
  98. 'image/jpeg',
  99. 'image/png',
  100. 'image/gif',
  101. 'video/mp4',
  102. ]);
  103. //dd($result);
  104. if (is_array($result)) {
  105. return $this->api($result);
  106. } elseif (is_string($result)) {
  107. return $this->api(['file' => $result]);
  108. } else {
  109. return $this->error($result);
  110. }
  111. }
  112. /**
  113. * @api {get} /api/attachment/delete/{md5} 删除文件(图片)
  114. * @apiDescription 删除文件(图片)
  115. * @apiGroup Attachment
  116. * @apiPermission Passport
  117. * @apiVersion 0.1.0
  118. * @apiParam {string} md5 图片md5码
  119. * @apiSuccessExample {json} Success-Response:
  120. * HTTP/1.1 200 OK
  121. * {
  122. * "state": true,
  123. * "code": 0,
  124. * "message": "",
  125. * "data": {
  126. * "result": true/false
  127. * }
  128. * }
  129. * @apiErrorExample {json} Error-Response:
  130. * HTTP/1.1 400 Bad Request
  131. * {
  132. * "state": false,
  133. * "code": 1000,
  134. * "message": "传入参数不正确",
  135. * "data": null or []
  136. * }
  137. * 可能出现的错误代码:
  138. * 205 ATTACHMENT_DELETE_FAILED 删除附件文件失败
  139. * 206 ATTACHMENT_RECORD_DELETE_FAILED 删除附件记录失败
  140. * 1203 ATTACHMENT_NOT_EXIST 附件不存在
  141. */
  142. public function delete($md5) {
  143. $result = $this->deleteAttachment($md5);
  144. if ($result === 0) {
  145. return $this->api(['result' => true]);
  146. } else {
  147. return $this->error($result);
  148. }
  149. }
  150. public function index(){
  151. $result = BaseAttachmentModel::all()->sortByDesc('id');
  152. return $this->api($result);
  153. }
  154. }