package com.gkhy.safePlatform.minioDemo.service;
|
|
import com.gkhy.safePlatform.commons.enums.ResultCodes;
|
import com.gkhy.safePlatform.commons.vo.ResultVO;
|
import com.gkhy.safePlatform.minioDemo.config.MinioConfig;
|
import com.gkhy.safePlatform.minioDemo.enums.ModuleTypeEnums;
|
import com.gkhy.safePlatform.minioDemo.utils.FileUtil;
|
import io.minio.GetPresignedObjectUrlArgs;
|
import io.minio.MinioClient;
|
import io.minio.errors.*;
|
import io.minio.http.Method;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.security.InvalidKeyException;
|
import java.security.NoSuchAlgorithmException;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
import static com.gkhy.safePlatform.minioDemo.enums.ModuleTypeEnums.*;
|
|
@Service
|
public class FileService {
|
|
@Resource
|
private MinioConfig minioConfig;
|
|
@Autowired
|
private MinioClient minioClient;
|
|
@Resource
|
private FileUtil fileUtil;
|
|
/**
|
* 文件下载
|
* @param fileName
|
* @param response
|
*/
|
public void downloadFile(String fileName, HttpServletResponse response) {
|
fileUtil.downloadFile(fileName,response);
|
}
|
|
/**
|
* 文件上传
|
* @param file
|
* @param moduleType
|
* @return
|
*/
|
public ResultVO<String> uploadFile(MultipartFile file, Integer moduleType) {
|
String url = "";
|
String moduleName = initModuleName(moduleType);
|
|
// 文件名转换
|
String time = new SimpleDateFormat("yyyyMMdd").format(new Date());
|
String fileSuffixName = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".")+1);
|
String fileName = moduleName + "/" + time + "_" + initUUID()+"."+fileSuffixName;
|
try {
|
url=fileUtil.upload(file, fileName);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return new ResultVO<>(ResultCodes.OK, url);
|
}
|
|
/**
|
* 模块名称转换
|
*/
|
private String initModuleName(Integer moduleType){
|
String moduleName = "";
|
ModuleTypeEnums moduleTypeEnums = ModuleTypeEnums.getReviewStatus(moduleType);
|
assert moduleTypeEnums != null;
|
switch (moduleTypeEnums) {
|
case EMERGENCY:
|
moduleName = EMERGENCY.getModuleName();
|
break;
|
case EQUIPMENT:
|
moduleName = EQUIPMENT.getModuleName();
|
break;
|
case GOAL_MANAGE:
|
moduleName = GOAL_MANAGE.getModuleName();
|
break;
|
case INCIDENT_MANAGE:
|
moduleName = INCIDENT_MANAGE.getModuleName();
|
break;
|
default:
|
break;
|
}
|
return moduleName;
|
}
|
|
/**
|
* 生成8位UUID
|
*/
|
private String initUUID() {
|
String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
|
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
|
"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
|
"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
|
"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
|
"W", "X", "Y", "Z"};
|
StringBuffer shortBuffer = new StringBuffer();
|
String uuid = UUID.randomUUID().toString().replace("-", "");
|
for (int i = 0; i < 8; i++) {
|
String str = uuid.substring(i * 4, i * 4 + 4);
|
int x = Integer.parseInt(str, 16);
|
shortBuffer.append(chars[x % 0x3E]);
|
}
|
return shortBuffer.toString();
|
}
|
|
|
|
|
|
public String getPutUrl(String objName) {
|
String url = null;
|
try {
|
Map<String, String> reqParams = new HashMap<>();
|
reqParams.put("response-content-type", "application/json");
|
url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
.method(Method.PUT)
|
.bucket(minioConfig.getBucketName())
|
.object(objName)
|
.expiry(60 * 2)
|
.extraQueryParams(reqParams)
|
.build());
|
} catch (ErrorResponseException e) {
|
e.printStackTrace();
|
} catch (InsufficientDataException e) {
|
e.printStackTrace();
|
} catch (InternalException e) {
|
e.printStackTrace();
|
} catch (InvalidKeyException e) {
|
e.printStackTrace();
|
} catch (InvalidResponseException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
} catch (NoSuchAlgorithmException e) {
|
e.printStackTrace();
|
} catch (XmlParserException e) {
|
e.printStackTrace();
|
} catch (ServerException e) {
|
e.printStackTrace();
|
}
|
return url;
|
}
|
|
|
}
|