授权给第三方下载/上传接口添加-http-api
This commit is contained in:
parent
9aad8c2629
commit
c88250cf2e
@ -5,15 +5,14 @@ import cn.axzo.framework.auth.domain.ContextInfo;
|
|||||||
import cn.axzo.oss.common.utils.BeanConvertUtil;
|
import cn.axzo.oss.common.utils.BeanConvertUtil;
|
||||||
import cn.axzo.oss.http.api.ServerFileServiceApi;
|
import cn.axzo.oss.http.api.ServerFileServiceApi;
|
||||||
import cn.axzo.oss.http.model.*;
|
import cn.axzo.oss.http.model.*;
|
||||||
import cn.axzo.oss.manager.api.dto.request.FindFileKeyDto;
|
import cn.axzo.oss.manager.api.dto.request.*;
|
||||||
import cn.axzo.oss.manager.api.dto.request.FindFileUrlDto;
|
|
||||||
import cn.axzo.oss.manager.api.dto.request.ServerFileDeleteDto;
|
|
||||||
import cn.axzo.oss.manager.api.dto.request.ServerFileUploadDto;
|
|
||||||
import cn.axzo.oss.service.api.FileService;
|
import cn.axzo.oss.service.api.FileService;
|
||||||
import cn.azxo.framework.common.model.CommonResponse;
|
import cn.azxo.framework.common.model.CommonResponse;
|
||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@ -92,4 +91,25 @@ public class ServerFileController implements ServerFileServiceApi {
|
|||||||
FindFileKeyDto dto = BeanConvertUtil.copyBean(request, FindFileKeyDto.class);
|
FindFileKeyDto dto = BeanConvertUtil.copyBean(request, FindFileKeyDto.class);
|
||||||
return CommonResponse.success(BeanConverter.convert(fileService.findFileKey(dto), FindFileKeyResponse.class));
|
return CommonResponse.success(BeanConverter.convert(fileService.findFileKey(dto), FindFileKeyResponse.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 授权给第三方下载-生成临时url
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public CommonResponse<List<ApiSignUrlDownloadResponse>> signUrlDownload(@Valid @RequestBody ApiSignUrlDownloadRequest request) {
|
||||||
|
SignUrlDownloadDto dto = BeanConvertUtil.copyBean(request, SignUrlDownloadDto.class);
|
||||||
|
return CommonResponse.success(BeanConverter.convert(fileService.signUrlDownload(dto), ApiSignUrlDownloadResponse.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 授权给第三方上传-生成临时url
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public CommonResponse<ApiSignUrlUploadResponse> signUrlUpload(@Valid @RequestBody ApiSignUrlUploadRequest request) {
|
||||||
|
SignUrlUploadDto dto = BeanConvertUtil.copyBean(request, SignUrlUploadDto.class);
|
||||||
|
// 获取feign调用请求头携带的用户信息
|
||||||
|
String contextInfoLiteJsonStr = httpServletRequest.getHeader("X-CONTEXT-INFO-LITE");
|
||||||
|
ContextInfo.LiteSaasContext liteSaasContext = JSONUtil.toBean(contextInfoLiteJsonStr, ContextInfo.LiteSaasContext.class);
|
||||||
|
return CommonResponse.success(BeanConverter.convert(fileService.signUrlUpload(dto, liteSaasContext), ApiSignUrlUploadResponse.class));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,6 +25,7 @@ import javax.validation.Valid;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import static cn.axzo.oss.common.enums.CodeEnum.FILE_NAME_TOO_LONG;
|
import static cn.axzo.oss.common.enums.CodeEnum.FILE_NAME_TOO_LONG;
|
||||||
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
|
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
|
||||||
@ -270,6 +271,7 @@ public class WebFileController {
|
|||||||
@PostMapping(value = "/signUrl/download")
|
@PostMapping(value = "/signUrl/download")
|
||||||
@CrossOrigin
|
@CrossOrigin
|
||||||
public CommonResponse<List<SignUrlDownloadResponse>> signUrlDownload(@Valid @RequestBody SignUrlDownloadDto request) {
|
public CommonResponse<List<SignUrlDownloadResponse>> signUrlDownload(@Valid @RequestBody SignUrlDownloadDto request) {
|
||||||
|
//获取用户信息
|
||||||
return CommonResponse.success(fileService.signUrlDownload(request));
|
return CommonResponse.success(fileService.signUrlDownload(request));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -279,6 +281,7 @@ public class WebFileController {
|
|||||||
@PostMapping(value = "/signUrl/upload")
|
@PostMapping(value = "/signUrl/upload")
|
||||||
@CrossOrigin
|
@CrossOrigin
|
||||||
public CommonResponse<SignUrlUploadResponse> signUrlUpload(@Valid @RequestBody SignUrlUploadDto request) {
|
public CommonResponse<SignUrlUploadResponse> signUrlUpload(@Valid @RequestBody SignUrlUploadDto request) {
|
||||||
return CommonResponse.success(fileService.signUrlUpload(request));
|
ContextInfo.LiteSaasContext liteSaasContext = Objects.nonNull(ContextInfoHolder.get()) ? ContextInfoHolder.get().lite() : null;
|
||||||
|
return CommonResponse.success(fileService.signUrlUpload(request, liteSaasContext));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,9 +3,11 @@ package cn.axzo.oss.http.api;
|
|||||||
import cn.axzo.oss.http.model.*;
|
import cn.axzo.oss.http.model.*;
|
||||||
import cn.azxo.framework.common.model.CommonResponse;
|
import cn.azxo.framework.common.model.CommonResponse;
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,4 +59,17 @@ public interface ServerFileServiceApi {
|
|||||||
*/
|
*/
|
||||||
@RequestMapping(value = "api/v1/server/getFileKey", method = RequestMethod.POST)
|
@RequestMapping(value = "api/v1/server/getFileKey", method = RequestMethod.POST)
|
||||||
CommonResponse<List<FindFileKeyResponse>> getFileKey(FindFileKeyRequest request);
|
CommonResponse<List<FindFileKeyResponse>> getFileKey(FindFileKeyRequest request);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 授权给第三方下载-生成临时url
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "api/signUrl/download", method = RequestMethod.POST)
|
||||||
|
CommonResponse<List<ApiSignUrlDownloadResponse>> signUrlDownload(ApiSignUrlDownloadRequest request);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 授权给第三方上传-生成临时url
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "api/signUrl/upload", method = RequestMethod.POST)
|
||||||
|
CommonResponse<ApiSignUrlUploadResponse> signUrlUpload(ApiSignUrlUploadRequest request);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,28 @@
|
|||||||
|
package cn.axzo.oss.http.model;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotEmpty;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author: xudawei
|
||||||
|
* @date: 2024-03-12
|
||||||
|
* @description: 授权给第三方下载
|
||||||
|
**/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class ApiSignUrlDownloadRequest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件uuid
|
||||||
|
*/
|
||||||
|
@NotEmpty(message = "fileKeys not empty")
|
||||||
|
private List<String> fileKeys;
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
package cn.axzo.oss.http.model;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: xudawei
|
||||||
|
* @Date: 2024/03/12
|
||||||
|
* @Description: 授权给第三方下载
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class ApiSignUrlDownloadResponse {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件临时-URL
|
||||||
|
*/
|
||||||
|
private String signUrl;
|
||||||
|
/**
|
||||||
|
* 文件 Key
|
||||||
|
*/
|
||||||
|
private String fileKey;
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
package cn.axzo.oss.http.model;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author: xudawei
|
||||||
|
* @date: 2024-03-12
|
||||||
|
* @description: 授权给第三方下载
|
||||||
|
**/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class ApiSignUrlUploadRequest {
|
||||||
|
/**
|
||||||
|
* appCode
|
||||||
|
*/
|
||||||
|
@NotBlank(message = "appCode not blank")
|
||||||
|
private String appCode;
|
||||||
|
/**
|
||||||
|
* bizScene,对应目录
|
||||||
|
*/
|
||||||
|
@NotBlank(message = "bizScene not blank")
|
||||||
|
private String bizScene;
|
||||||
|
/**
|
||||||
|
* serviceName
|
||||||
|
*/
|
||||||
|
private String serviceName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通道类型
|
||||||
|
*/
|
||||||
|
private Integer channelType;
|
||||||
|
/**
|
||||||
|
* 桶名称
|
||||||
|
*/
|
||||||
|
private String bucketName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件名称
|
||||||
|
*/
|
||||||
|
private String fileName;
|
||||||
|
}
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
package cn.axzo.oss.http.model;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: xudawei
|
||||||
|
* @Date: 2024/03/12
|
||||||
|
* @Description: 授权给第三方下载
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class ApiSignUrlUploadResponse {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件临时-URL
|
||||||
|
*/
|
||||||
|
private String signUrl;
|
||||||
|
/**
|
||||||
|
* 文件 Key
|
||||||
|
*/
|
||||||
|
private String fileKey;
|
||||||
|
|
||||||
|
}
|
||||||
@ -23,7 +23,7 @@ public class SignUrlDownloadDto {
|
|||||||
/**
|
/**
|
||||||
* 文件uuid
|
* 文件uuid
|
||||||
*/
|
*/
|
||||||
@NotEmpty(message = "fileKey not empty")
|
@NotEmpty(message = "fileKeys not empty")
|
||||||
private List<String> fileKey;
|
private List<String> fileKeys;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,7 +33,7 @@ public class SignUrlUploadDto {
|
|||||||
private String serviceName;
|
private String serviceName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通道code
|
* 通道类型
|
||||||
*/
|
*/
|
||||||
private Integer channelType;
|
private Integer channelType;
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -60,5 +60,5 @@ public interface FileService {
|
|||||||
/**
|
/**
|
||||||
* 授权给第三方上传
|
* 授权给第三方上传
|
||||||
*/
|
*/
|
||||||
SignUrlUploadResponse signUrlUpload(SignUrlUploadDto dto);
|
SignUrlUploadResponse signUrlUpload(SignUrlUploadDto dto, ContextInfo.LiteSaasContext liteSaasContext);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -707,14 +707,14 @@ public class FileServiceImpl implements FileService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 授权给第三方下载-下载
|
* 授权给第三方-下载
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<SignUrlDownloadResponse> signUrlDownload(SignUrlDownloadDto dto) {
|
public List<SignUrlDownloadResponse> signUrlDownload(SignUrlDownloadDto dto) {
|
||||||
log.info("signUrl download dto = {}", JsonUtil.obj2Str(dto));
|
log.info("signUrl download dto = {}", JsonUtil.obj2Str(dto));
|
||||||
|
|
||||||
|
|
||||||
List<File> fileList = fileDao.getByFileUuids(dto.getFileKey());
|
List<File> fileList = fileDao.getByFileUuids(dto.getFileKeys());
|
||||||
return fileList.stream().map(item -> {
|
return fileList.stream().map(item -> {
|
||||||
// bucket下的key
|
// bucket下的key
|
||||||
String tgtFileKey = Utility.generateFileKey(item.getDirectory(), item.getFileUuid(), item.getFileFormat());
|
String tgtFileKey = Utility.generateFileKey(item.getDirectory(), item.getFileUuid(), item.getFileFormat());
|
||||||
@ -730,11 +730,15 @@ public class FileServiceImpl implements FileService {
|
|||||||
* 授权给第三方下载-上传
|
* 授权给第三方下载-上传
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public SignUrlUploadResponse signUrlUpload(SignUrlUploadDto dto) {
|
public SignUrlUploadResponse signUrlUpload(SignUrlUploadDto dto, ContextInfo.LiteSaasContext liteSaasContext) {
|
||||||
log.info("signUrl upload dto = {}", JsonUtil.obj2Str(dto));
|
log.info("signUrl upload dto = {}", JsonUtil.obj2Str(dto));
|
||||||
// 检查appCode
|
//1 校验
|
||||||
checkAppCode(dto.getAppCode());
|
checkAppCode(dto.getAppCode());
|
||||||
|
//2 获取文件配置(多个配置获取优先级高)
|
||||||
FileUploadConfig fileUploadConfig = this.signUrlBuildUploadConfig(dto);
|
FileUploadConfig fileUploadConfig = this.signUrlBuildUploadConfig(dto);
|
||||||
|
//操作日志记录
|
||||||
|
operateLog(dto.toString(), dto.getServiceName(), FILE_UPLOAD_CODE, FILE_UPLOAD_NAME, liteSaasContext);
|
||||||
|
//3 保存File对象
|
||||||
return this.signUrlSaveFile(dto, fileUploadConfig);
|
return this.signUrlSaveFile(dto, fileUploadConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -754,11 +758,16 @@ public class FileServiceImpl implements FileService {
|
|||||||
// 是否包含指定格式
|
// 是否包含指定格式
|
||||||
String fileFormat = dto.getFileName().substring(lastIndexOf + CommonConstants.ONE).toLowerCase();
|
String fileFormat = dto.getFileName().substring(lastIndexOf + CommonConstants.ONE).toLowerCase();
|
||||||
|
|
||||||
|
// 文件格式判断
|
||||||
|
String[] formats = fileUploadConfig.getFileFormat().split(FileClassEnum.COMMA.type);
|
||||||
|
// 是否包含指定格式
|
||||||
|
BizException.error(Arrays.asList(formats).contains(fileFormat), CodeEnum.FILE_FORMAT_NOT_SUPPORTED);
|
||||||
|
|
||||||
// 生成上传文件的唯一key
|
// 生成上传文件的唯一key
|
||||||
String tgtFileKey = Utility.generateFileKey(fileUploadConfig.getDirectory(), uuid, fileFormat);
|
String tgtFileKey = Utility.generateFileKey(fileUploadConfig.getDirectory(), uuid, fileFormat);
|
||||||
|
//1 调用阿里云/华为云 获取临时授权signUrl
|
||||||
String fileUrl = this.fileManager.signUrlUpload(dto.getBucketName(), tgtFileKey, dto.getFileName(),SIGN_URL_UPLOAD_EXPIRE_SECOND, fileUploadConfig.getChannelCode());
|
String fileUrl = this.fileManager.signUrlUpload(dto.getBucketName(), tgtFileKey, dto.getFileName(),SIGN_URL_UPLOAD_EXPIRE_SECOND, fileUploadConfig.getChannelCode());
|
||||||
|
//2 保存File对象
|
||||||
this.getOssFile(fileUploadConfig, dto.getFileName(), fileFormat, uuid,
|
this.getOssFile(fileUploadConfig, dto.getFileName(), fileFormat, uuid,
|
||||||
fileUrl, Utility.getMd5(fileUrl));
|
fileUrl, Utility.getMd5(fileUrl));
|
||||||
return SignUrlUploadResponse.builder()
|
return SignUrlUploadResponse.builder()
|
||||||
@ -779,7 +788,7 @@ public class FileServiceImpl implements FileService {
|
|||||||
FileBusinessScene scene = fileBusinessSceneManager
|
FileBusinessScene scene = fileBusinessSceneManager
|
||||||
.getByBucketNoAndScene(appChannelBucket.getAppChannelBucketNo(), dto.getBizScene());
|
.getByBucketNoAndScene(appChannelBucket.getAppChannelBucketNo(), dto.getBizScene());
|
||||||
|
|
||||||
// 通过渠道码和桶名称获取获取指定上传配置
|
// 通过渠道码和桶名称获取指定上传配置
|
||||||
fileUploadConfig = fileUploadConfigManager
|
fileUploadConfig = fileUploadConfigManager
|
||||||
.getByUploadConfig(scene.getAppChannelBucketNo(), scene.getDirectory());
|
.getByUploadConfig(scene.getAppChannelBucketNo(), scene.getDirectory());
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user