增加web端上传文件接口
This commit is contained in:
parent
9f33e2a9a3
commit
f20bc55e5f
@ -0,0 +1,47 @@
|
||||
package cn.axzo.oss.client.controller;
|
||||
|
||||
import cn.axzo.oss.client.vo.WebFileUploadVo;
|
||||
import cn.axzo.oss.common.utils.BeanConvertUtil;
|
||||
import cn.axzo.oss.manager.api.dto.request.ServerFileUploadDto;
|
||||
import cn.axzo.oss.manager.api.dto.response.ServerFileUploadResponse;
|
||||
import cn.axzo.oss.service.api.FileService;
|
||||
import cn.azxo.framework.common.model.CommonResponse;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Valid;
|
||||
|
||||
/**
|
||||
* 前端文件controller
|
||||
*
|
||||
* @author zhangtianyu
|
||||
* @date 2022/4/14 2:56 PM
|
||||
**/
|
||||
@RestController
|
||||
@RequestMapping("/webApi")
|
||||
public class WebFileController {
|
||||
@Resource
|
||||
private FileService fileService;
|
||||
|
||||
@SneakyThrows
|
||||
@PostMapping("/v1/file")
|
||||
public CommonResponse<WebFileUploadVo> upload(@Valid @RequestParam String appCode,
|
||||
@Valid @RequestParam String bizScene,
|
||||
@Valid @RequestParam MultipartFile file) {
|
||||
ServerFileUploadDto fileUploadDto = ServerFileUploadDto.builder()
|
||||
.appCode(appCode)
|
||||
.bizScene(bizScene)
|
||||
.fileName(file.getOriginalFilename())
|
||||
.fileContent(file.getBytes())
|
||||
.build();
|
||||
ServerFileUploadResponse response = fileService.upload(fileUploadDto);
|
||||
WebFileUploadVo result = BeanConvertUtil.copyBean(response, WebFileUploadVo.class);
|
||||
return CommonResponse.success(result);
|
||||
}
|
||||
|
||||
}
|
||||
@ -10,6 +10,7 @@ import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@ -23,6 +24,12 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
@RestControllerAdvice
|
||||
public class ControllerExceptionHandler {
|
||||
|
||||
@ExceptionHandler(value = MissingServletRequestParameterException.class)
|
||||
public CommonResponse handleMissingRequestParam(MissingServletRequestParameterException e) {
|
||||
log.warn("[oss] ControllerExceptionHandler.missing servlet request parameter Exception", e);
|
||||
return CommonResponse.error(CodeEnum.MISSING_REQUEST_PARAM.getCode(), e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = BizException.class)
|
||||
public CommonResponse bizException(BizException e){
|
||||
log.warn("[oss] ControllerExceptionHandler.bizException Exception", e);
|
||||
|
||||
@ -0,0 +1,22 @@
|
||||
package cn.axzo.oss.client.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 前端上传文件返回实体
|
||||
*
|
||||
* @author zhangtianyu
|
||||
* @date 2022/4/14 3:26 PM
|
||||
**/
|
||||
@Data
|
||||
public class WebFileUploadVo {
|
||||
private String url;
|
||||
/**
|
||||
* 文件 Key
|
||||
*/
|
||||
private String fileKey;
|
||||
/**
|
||||
* URL MD5
|
||||
*/
|
||||
private String urlMd5;
|
||||
}
|
||||
@ -40,6 +40,7 @@ public enum CodeEnum implements EnumBase<Integer> {
|
||||
NOT_FILE_FORMAT(108,"failed to get file format"),
|
||||
FILE_FORMAT_NOT_SUPPORTED(109,"file format is not supported"),
|
||||
FILE_UPLOAD_FAILED(109,"file upload failed"),
|
||||
MISSING_REQUEST_PARAM(110, "missing request param"),
|
||||
|
||||
;
|
||||
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
package cn.axzo.oss.manager.api.dto.request;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @program: oss
|
||||
@ -11,6 +12,9 @@ import lombok.Data;
|
||||
* @date: 2021-07-29 18:31
|
||||
**/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ServerFileUploadDto {
|
||||
private String appCode;
|
||||
private String bizScene;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user