Merge branch 'feature/joint_log' into 'pre'
Feature/joint log See merge request infra/oss!45
This commit is contained in:
commit
029d339791
@ -25,7 +25,6 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.servlet.http.Part;
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@ -112,14 +111,14 @@ public class WebFileController {
|
|||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
@GetMapping("/v1/file/download")
|
@GetMapping("/v1/file/download")
|
||||||
@CrossOrigin
|
@CrossOrigin
|
||||||
@PreBuildContext
|
|
||||||
public void download(@Valid ServerFileDownloadDto dto, HttpServletResponse response) {
|
public void download(@Valid ServerFileDownloadDto dto, HttpServletResponse response) {
|
||||||
ServerFileDownloadResponse result = fileService.download(dto);
|
ServerFileDownloadResponse result = fileService.download(dto);
|
||||||
InputStream inputStream = null;
|
InputStream inputStream = null;
|
||||||
OutputStream outputStream = response.getOutputStream();
|
OutputStream outputStream = response.getOutputStream();
|
||||||
try {
|
try {
|
||||||
inputStream = result.getFileStream();
|
inputStream = result.getFileStream();
|
||||||
response.setContentType("application/x-download");
|
response.setContentType("image/jpg");
|
||||||
|
response.setCharacterEncoding("UTF-8");
|
||||||
response.addHeader("Content-Disposition", "attachment;filename=" + result.getFileName() + "." + result.getFileFormat());
|
response.addHeader("Content-Disposition", "attachment;filename=" + result.getFileName() + "." + result.getFileFormat());
|
||||||
IOUtils.copy(inputStream, outputStream);
|
IOUtils.copy(inputStream, outputStream);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@ -18,7 +18,10 @@ public abstract class CommonConstants {
|
|||||||
*/
|
*/
|
||||||
public static final int ONE = 1;
|
public static final int ONE = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* app端oss bucketName:axzo-pro
|
||||||
|
*/
|
||||||
|
public static final String APP_PRO_BUCKET_NAME = "axzo-pro";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否删除
|
* 是否删除
|
||||||
|
|||||||
@ -43,6 +43,8 @@ public enum CodeEnum implements EnumBase<Integer> {
|
|||||||
MISSING_REQUEST_PARAM(110, "missing request param"),
|
MISSING_REQUEST_PARAM(110, "missing request param"),
|
||||||
FILE_NAME_TOO_LONG(111, "file name too long max 128"),
|
FILE_NAME_TOO_LONG(111, "file name too long max 128"),
|
||||||
|
|
||||||
|
FILE_NOT_FOUND(112, "file not found"),
|
||||||
|
|
||||||
;
|
;
|
||||||
|
|
||||||
private final Integer code;
|
private final Integer code;
|
||||||
|
|||||||
@ -0,0 +1,57 @@
|
|||||||
|
package cn.axzo.oss.integration.s3.client;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.axzo.oss.common.enums.CodeEnum;
|
||||||
|
import cn.axzo.oss.common.exception.S3Exception;
|
||||||
|
import cn.axzo.oss.common.utils.Utility;
|
||||||
|
import cn.axzo.oss.integration.s3.base.OssClientBase;
|
||||||
|
import cn.azxo.framework.common.utils.LogUtil;
|
||||||
|
import com.aliyun.oss.OSS;
|
||||||
|
import com.aliyun.oss.OSSClientBuilder;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @program: oss
|
||||||
|
* @description: 阿里云服务客户端(用于兼容app端阿里云oss accessKeyId和secretAccessKey)
|
||||||
|
* @author: mr.jie
|
||||||
|
* @date: 2021-07-29 15:49
|
||||||
|
**/
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class AliOssAppProClient implements OssClientBase {
|
||||||
|
|
||||||
|
private static final String APP_PRO_ACCESS_KEY_ID = "LTAI4GHX7wnnpcp94Vu7JGGs";
|
||||||
|
private static final String APP_PRO_ACCESS_KEY_SECRET = "8X5njFjCuUq1XNKkrz1rLV9xv1Mj3J";
|
||||||
|
private static final String APP_PRO_ENDPOINT = "https://oss-cn-hangzhou.aliyuncs.com";
|
||||||
|
|
||||||
|
private volatile static OSS instance = null;
|
||||||
|
|
||||||
|
public OSS getClient() {
|
||||||
|
if (Utility.objIsNull(instance)) {
|
||||||
|
throw new S3Exception(CodeEnum.S3_CLIENT_NULL.getCode(), "oss客户端未初始化");
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
@Override
|
||||||
|
public void initClient() {
|
||||||
|
log.info("initClient endpoint = {}, accessKeyId = {},secretAccessKey = {}", APP_PRO_ENDPOINT, APP_PRO_ACCESS_KEY_ID, APP_PRO_ACCESS_KEY_SECRET);
|
||||||
|
try {
|
||||||
|
instance = new OSSClientBuilder().build(APP_PRO_ENDPOINT, APP_PRO_ACCESS_KEY_ID, APP_PRO_ACCESS_KEY_SECRET);
|
||||||
|
} catch (Exception e) {
|
||||||
|
instance = null;
|
||||||
|
LogUtil.error("initClient error = {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getEndpoint() {
|
||||||
|
return APP_PRO_ENDPOINT;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,9 +1,9 @@
|
|||||||
package cn.axzo.oss.integration.s3.impl;
|
package cn.axzo.oss.integration.s3.impl;
|
||||||
|
|
||||||
import cn.axzo.oss.integration.s3.AliOssService;
|
import cn.axzo.oss.integration.s3.AliOssService;
|
||||||
|
import cn.axzo.oss.integration.s3.client.AliOssAppProClient;
|
||||||
import cn.axzo.oss.integration.s3.client.AliOssClient;
|
import cn.axzo.oss.integration.s3.client.AliOssClient;
|
||||||
import cn.azxo.framework.common.utils.LogUtil;
|
import cn.azxo.framework.common.utils.LogUtil;
|
||||||
import com.aliyun.oss.ClientException;
|
|
||||||
import com.aliyun.oss.OSS;
|
import com.aliyun.oss.OSS;
|
||||||
import com.aliyun.oss.OSSException;
|
import com.aliyun.oss.OSSException;
|
||||||
import com.aliyun.oss.model.GetObjectRequest;
|
import com.aliyun.oss.model.GetObjectRequest;
|
||||||
@ -18,9 +18,10 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
|
|
||||||
import java.io.FilterInputStream;
|
import java.io.FilterInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
import static cn.axzo.oss.common.constans.CommonConstants.APP_PRO_BUCKET_NAME;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @program: oss
|
* @program: oss
|
||||||
* @description: 阿里oss服务
|
* @description: 阿里oss服务
|
||||||
@ -34,6 +35,9 @@ public class AliOssServiceImpl implements AliOssService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private AliOssClient aliOssClient;
|
private AliOssClient aliOssClient;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AliOssAppProClient aliOssAppProClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* upload file
|
* upload file
|
||||||
*/
|
*/
|
||||||
@ -99,6 +103,9 @@ public class AliOssServiceImpl implements AliOssService {
|
|||||||
@Override
|
@Override
|
||||||
public InputStream downloadFile(String bucket, String tgtFileKey, String style) {
|
public InputStream downloadFile(String bucket, String tgtFileKey, String style) {
|
||||||
OSS client = aliOssClient.getClient();
|
OSS client = aliOssClient.getClient();
|
||||||
|
if (bucket.contains(APP_PRO_BUCKET_NAME)) {
|
||||||
|
client = aliOssAppProClient.getClient();
|
||||||
|
}
|
||||||
GetObjectRequest request = new GetObjectRequest(bucket, tgtFileKey);
|
GetObjectRequest request = new GetObjectRequest(bucket, tgtFileKey);
|
||||||
if (StringUtils.isNotEmpty(style)) {
|
if (StringUtils.isNotEmpty(style)) {
|
||||||
request.setProcess(style);
|
request.setProcess(style);
|
||||||
|
|||||||
@ -35,6 +35,8 @@ import java.util.*;
|
|||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static cn.axzo.oss.common.constans.CommonConstants.APP_PRO_BUCKET_NAME;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author admin
|
* @Author admin
|
||||||
* @Description
|
* @Description
|
||||||
@ -45,16 +47,18 @@ import java.util.stream.Collectors;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class FileServiceImpl implements FileService {
|
public class FileServiceImpl implements FileService {
|
||||||
|
|
||||||
private static String FILE_UPLOAD_CODE = "OSS_FILE_UPLOAD";
|
private static final String FILE_UPLOAD_CODE = "OSS_FILE_UPLOAD";
|
||||||
private static String FILE_UPLOAD_NAME = "文件上传";
|
private static final String FILE_UPLOAD_NAME = "文件上传";
|
||||||
private static String APPCODE_FACE = "face";
|
private static final String APPCODE_FACE = "face";
|
||||||
private static String SCENE_FACE = "face";
|
private static final String SCENE_FACE = "face";
|
||||||
private static String APPCODE_IDCARD = "idcard";
|
private static final String APPCODE_IDCARD = "idcard";
|
||||||
private static String SCENE_IDCARD = "idcard";
|
private static final String SCENE_IDCARD = "idcard";
|
||||||
private static String APPCODE_BANKCARD = "bankcard";
|
private static final String APPCODE_BANKCARD = "bankcard";
|
||||||
private static String SCENE_BANKCARD = "bankcard";
|
private static final String SCENE_BANKCARD = "bankcard";
|
||||||
|
|
||||||
private static String IS_URL = "https://";
|
private static final String IS_URL = "https://";
|
||||||
|
|
||||||
|
private static final String HTTP_COM = ".com/";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private FileManager fileManager;
|
private FileManager fileManager;
|
||||||
@ -96,10 +100,16 @@ public class FileServiceImpl implements FileService {
|
|||||||
log.warn("delete file is null,url = {}, urlMd5 = {}", dto.getUrl(), urlMd5);
|
log.warn("delete file is null,url = {}, urlMd5 = {}", dto.getUrl(), urlMd5);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String tgtFileKey = Utility
|
//String tgtFileKey = Utility.generateFileKey(file.getDirectory(), file.getFileUuid(), file.getFileFormat());
|
||||||
.generateFileKey(file.getDirectory(), file.getFileUuid(), file.getFileFormat());
|
//兼容app端oss历史上传文件的url处理
|
||||||
|
String tgtFileKey = file.getFileUrl().substring(file.getFileUrl().indexOf(".com/") + 5);
|
||||||
log.debug("delete tgtFileKey = {}", tgtFileKey);
|
log.debug("delete tgtFileKey = {}", tgtFileKey);
|
||||||
boolean deleteFlag = fileManager.delete(file.getBucketName(), tgtFileKey);
|
String bucketName = file.getBucketName();
|
||||||
|
if (file.getFileUrl().contains(APP_PRO_BUCKET_NAME)) {
|
||||||
|
//兼容处理,支持app端bucketName:axzo-pro
|
||||||
|
bucketName = APP_PRO_BUCKET_NAME;
|
||||||
|
}
|
||||||
|
boolean deleteFlag = fileManager.delete(bucketName, tgtFileKey);
|
||||||
log.info("delete deleteFlag = {}", deleteFlag);
|
log.info("delete deleteFlag = {}", deleteFlag);
|
||||||
if (deleteFlag) {
|
if (deleteFlag) {
|
||||||
File updateFile = new File();
|
File updateFile = new File();
|
||||||
@ -151,12 +161,18 @@ public class FileServiceImpl implements FileService {
|
|||||||
File file = fileDao.getByFileUuid(fileKey);
|
File file = fileDao.getByFileUuid(fileKey);
|
||||||
if (Utility.objIsNull(file)) {
|
if (Utility.objIsNull(file)) {
|
||||||
log.warn("file download is null, fileKey = {}", fileKey);
|
log.warn("file download is null, fileKey = {}", fileKey);
|
||||||
return null;
|
BizException.error(Utility.objIsNotNull(file), CodeEnum.FILE_NOT_FOUND);
|
||||||
}
|
}
|
||||||
String tgtFileKey = Utility
|
//String tgtFileKey = Utility.generateFileKey(file.getDirectory(), file.getFileUuid(), file.getFileFormat());
|
||||||
.generateFileKey(file.getDirectory(), file.getFileUuid(), file.getFileFormat());
|
//兼容app端oss历史上传文件的url处理
|
||||||
|
String tgtFileKey = file.getFileUrl().substring(file.getFileUrl().indexOf(".com/") + 5);
|
||||||
log.info("file download tgtFileKey = {}", tgtFileKey);
|
log.info("file download tgtFileKey = {}", tgtFileKey);
|
||||||
InputStream fileStream = fileManager.downloadFile(file.getBucketName(), tgtFileKey, dto.getStyle());
|
String bucketName = file.getBucketName();
|
||||||
|
if (file.getFileUrl().contains(APP_PRO_BUCKET_NAME)) {
|
||||||
|
//兼容处理,支持app端bucketName:axzo-pro
|
||||||
|
bucketName = APP_PRO_BUCKET_NAME;
|
||||||
|
}
|
||||||
|
InputStream fileStream = fileManager.downloadFile(bucketName, tgtFileKey, dto.getStyle());
|
||||||
return setFileDownloadResponse(file, fileStream);
|
return setFileDownloadResponse(file, fileStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user