AttachmentController.php 5.3 KB

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