12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace App\Http\Controllers\V1;
- use Illuminate\Http\Request;
- use App\Helper\AttachmentHelper;
- use Illuminate\Support\Facades\Validator;
- class AttachmentController extends Controller
- {
- use AttachmentHelper;
- /**
- * * 可能出现的错误代码:
- * 200 SAVE_USER_FAILED 保存用户数据失败
- * 201 ATTACHMENT_MKDIR_FAILED 创建附件目录失败
- * 202 ATTACHMENT_UPLOAD_INVALID 上传附件文件无效
- * 203 ATTACHMENT_SAVE_FAILED 保存附件失败
- * 204 ATTACHMENT_MOVE_FAILED 移动附件失败
- * 205 ATTACHMENT_DELETE_FAILED 删除附件文件失败
- * 206 ATTACHMENT_RECORD_DELETE_FAILED 删除附件记录失败
- * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
- * 1101 INCORRECT_VERIFY_CODE 输入验证码错误
- * 1105 USER_DOES_NOT_EXIST 用户不存在
- * 1200 ATTACHMENT_UPLOAD_FAILED 附件上传失败
- * 1201 ATTACHMENT_SIZE_EXCEEDED 附件大小超过限制
- * 1202 ATTACHMENT_MIME_NOT_ALLOWED 附件类型不允许
- * 1203 ATTACHMENT_NOT_EXIST 附件不存在.
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function upload(Request $request)
- {
- try {
- $validator = Validator::make(
- $request->all(),
- [
- 'tag' => 'required|alpha_dash',
- ],
- [
- 'tag.required' => 'tag必填',
- 'tag.alpha_dash' => 'tag只能为字母数字中/下划线',
- ]
- );
- if ($validator->fails()) {
- throw new \Exception($validator->messages()->first());
- }
- $result = $this->uploadAttachment($request, $request->post('file'), $request->post('tag'), 10 * 1024 * 1024, [
- 'image/jpeg',
- 'image/png',
- 'image/gif',
- 'video/mp4',
- ]);
- } catch (\Exception $exception) {
- return $this->error($exception->getMessage());
- }
- return $this->success(['file' => $result], 0, trans('api.UPLOAD_SUCCESS'));
- }
- /**
- * @return \Illuminate\Http\JsonResponse
- * parse_url($url);
- * array (
- * 'scheme' => 'https',
- * 'host' => 'zhengda.oss-cn-chengdu.aliyuncs.com',
- * 'path' => '/api_avatar/2022-07/14/1271657787068220714.jpeg',
- * )
- */
- public function delete(Request $request)
- {
- $picUrl = $request->pic_url;
- if (empty($picUrl)) {
- return $this->error('路径必填!');
- }
- $arr = parse_url($picUrl);
- $path = substr($arr['path'], 1);
- $result = $this->deleteAttachment($path);
- if ($result) {
- return $this->success();
- }
- return $this->error();
- }
- }
|