|
|
|
@ -0,0 +1,131 @@
|
|
|
|
|
package cn.com.connor.bh.bhdemo.demos.web.controller;
|
|
|
|
|
|
|
|
|
|
import cn.com.connor.bh.bhdemo.demos.web.entity.MinIOUploadRequestPojo;
|
|
|
|
|
import cn.com.connor.bh.bhdemo.demos.web.service.MinIOService;
|
|
|
|
|
import cn.hutool.core.codec.Base64;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
|
import java.net.URLDecoder;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/minio")
|
|
|
|
|
public class MinIOController {
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(MinIOController.class);
|
|
|
|
|
|
|
|
|
|
private final ObjectMapper objectMapper;
|
|
|
|
|
|
|
|
|
|
public MinIOController(ObjectMapper objectMapper) {
|
|
|
|
|
this.objectMapper = objectMapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private MinIOService minIOService;
|
|
|
|
|
|
|
|
|
|
@PostMapping("/upload")
|
|
|
|
|
public ResponseEntity<Map<String, String>> uploadFile(@RequestParam("filePath") MultipartFile file,
|
|
|
|
|
@RequestParam("bucketFileName") String bucketFileName) throws Exception {
|
|
|
|
|
Map<String, String> response = new HashMap<>();
|
|
|
|
|
try {
|
|
|
|
|
logger.info("开始上传文件到MinIO,上传目标位置:{}", bucketFileName);
|
|
|
|
|
|
|
|
|
|
logger.info("开始上传文件到MinIO,上传目标位置:{},原始文件:{}", bucketFileName, file.getOriginalFilename());
|
|
|
|
|
if (bucketFileName == null || bucketFileName.isEmpty()) {
|
|
|
|
|
logger.error("上传文件路径或文件名不能为空!");
|
|
|
|
|
response.put("message", "上传文件路径或文件名不能为空!");
|
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String resultUrl = minIOService.uploadFile(file, bucketFileName);
|
|
|
|
|
if (resultUrl != null){
|
|
|
|
|
response.put("message", "File uploaded successfully");
|
|
|
|
|
response.put("url", resultUrl);
|
|
|
|
|
}else {
|
|
|
|
|
response.put("message", "文件上传失败,请联系管理员。");
|
|
|
|
|
}
|
|
|
|
|
return ResponseEntity.ok(response);
|
|
|
|
|
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
logger.error("文件上传参数非法", e);
|
|
|
|
|
response.put("message", "非法参数:" + e.getMessage());
|
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.error("文件上传过程中发生未知异常", e);
|
|
|
|
|
response.put("message", "文件上传失败,请联系管理员。");
|
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 清空minIO目录
|
|
|
|
|
* @param directory
|
|
|
|
|
* @return
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
@PostMapping("/deleteByDir")
|
|
|
|
|
public ResponseEntity<Map<String, String>> deleteByDir(@RequestParam("directory") String directory) throws Exception {
|
|
|
|
|
Map<String, String> response = new HashMap<>();
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
logger.info("开始清空MinIO目录,清空目标位置:{}", directory);
|
|
|
|
|
if (directory == null || directory.isEmpty()) {
|
|
|
|
|
logger.error("清空minIO目录路径不能为空!");
|
|
|
|
|
response.put("message", "清空minIO目录路径不能为空!");
|
|
|
|
|
return ResponseEntity.badRequest().body(response);
|
|
|
|
|
}
|
|
|
|
|
boolean deletResult = minIOService.deleteByDir(directory);
|
|
|
|
|
if (deletResult){
|
|
|
|
|
response.put("message", "清空minIO目录成功");
|
|
|
|
|
return ResponseEntity.ok(response);
|
|
|
|
|
}else {
|
|
|
|
|
response.put("message", "清空minIO目录失败,请联系管理员。");
|
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
|
|
|
|
|
}
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
logger.error("清空minIO目录过程中发生未知异常", e);
|
|
|
|
|
response.put("message", "清空minIO目录失败,请联系管理员。");
|
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String parseValueFromBody(String body, String paramName) {
|
|
|
|
|
String[] parts = body.split("&");
|
|
|
|
|
for (String part : parts) {
|
|
|
|
|
String[] keyValue = part.split("=");
|
|
|
|
|
if (keyValue.length == 2 && keyValue[0].equals(paramName)) {
|
|
|
|
|
try {
|
|
|
|
|
return URLDecoder.decode(keyValue[1], "GBK");
|
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String removeFirstAtSymbol(String str,String symbol) {
|
|
|
|
|
int atIndex = str.indexOf(symbol);
|
|
|
|
|
if (atIndex != -1) {
|
|
|
|
|
return str.substring(0, atIndex) + str.substring(atIndex + 1);
|
|
|
|
|
}
|
|
|
|
|
return str; // 如果没有找到 @ 符号,返回原字符串
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String cleanString(String input) {
|
|
|
|
|
// 使用正则表达式或其他方法清理无效字符
|
|
|
|
|
return input.replaceAll("[^\\x00-\\x7F]", "");
|
|
|
|
|
}
|
|
|
|
|
}
|