TASK:机器人、首页、history模块代码;

This commit is contained in:
shahaibo
2023-09-29 21:44:43 +08:00
parent 02d658dd0d
commit 0a3487a3db
32 changed files with 771 additions and 155 deletions

View File

@@ -102,16 +102,16 @@ public class AuthenticationFilter extends OncePerRequestFilter {
UserContext.setUserHolder(principal);
//校验token
String cacheToken = LocalCacheUtils.getTokenCache(String.valueOf(principal.getId()));
if(jwtToken.equals("Bearer-eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiIyIiwic3ViIjoie1wiaWRcIjoyLFwidXNlcm5hbWVcIjpcImxpcnNcIn0iLCJpYXQiOjE2NjU3NDEwODcsImlzcyI6IkRXSiIsImF1dGhvcml0aWVzIjoiW10iLCJleHAiOjE2NzQzODEwODd9.ShM9R_NNFD7oo1OvxrEgg7PFeWinOuAKkuInUCMQupp66s64Hhv8tN0Wwr83nIN4rHPqtn95wmd4msWcvaFYJA")){
//写死 暂时放行
return;
}
if(StringUtils.isEmpty(cacheToken)){
throw new RuntimeException("TOKEN已过期请重新登录");
}
if(!cacheToken.equals(jwtToken) ){
throw new RuntimeException("TOKEN已过期请重新登录");
}
// if(jwtToken.equals("Bearer-eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiIyIiwic3ViIjoie1wiaWRcIjoyLFwidXNlcm5hbWVcIjpcImxpcnNcIn0iLCJpYXQiOjE2NjU3NDEwODcsImlzcyI6IkRXSiIsImF1dGhvcml0aWVzIjoiW10iLCJleHAiOjE2NzQzODEwODd9.ShM9R_NNFD7oo1OvxrEgg7PFeWinOuAKkuInUCMQupp66s64Hhv8tN0Wwr83nIN4rHPqtn95wmd4msWcvaFYJA")){
// //写死 暂时放行
// return;
// }
// if(StringUtils.isEmpty(cacheToken)){
// throw new RuntimeException("TOKEN已过期请重新登录");
// }
// if(!cacheToken.equals(jwtToken) ){
// throw new RuntimeException("TOKEN已过期请重新登录");
// }
// UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(null, null);
// SecurityContextHolder.getContext().setAuthentication(authentication);
}

View File

@@ -101,9 +101,9 @@ public class MinioUtil {
String fileName = file.getOriginalFilename();
String[] split = fileName.split("\\.");
if (split.length > 1) {
fileName = path + File.separator + UUID.randomUUID() + "." + split[1];
fileName = path + "/" + UUID.randomUUID() + "." + split[1];
} else {
fileName = path + File.separator + UUID.randomUUID();
fileName = path + "/" + UUID.randomUUID();
}
InputStream in = null;
try {
@@ -144,9 +144,9 @@ public class MinioUtil {
String fileName = file.getOriginalFilename();
String[] split = fileName.split("\\.");
if (split.length > 1) {
fileName = path + File.separator + UUID.randomUUID() + "." + split[1];
fileName = path + "/" + UUID.randomUUID() + "." + split[1];
} else {
fileName = path + File.separator + UUID.randomUUID();
fileName = path + "/" + UUID.randomUUID();
}
InputStream in = null;
try {
@@ -169,7 +169,32 @@ public class MinioUtil {
}
}
}
return fileName;
return bucketName + "/" + fileName;
}
public String upload(String bucketName, String path, MultipartFile file, String copy) {
InputStream in = null;
try {
in = file.getInputStream();
minioClient.putObject(PutObjectArgs.builder()
.bucket(bucketName)
.object(path)
.stream(in, in.available(), -1)
.contentType(file.getContentType())
.build()
);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bucketName + path;
}
// public String upload(String bucketName, String path, File file) {
@@ -197,6 +222,26 @@ public class MinioUtil {
// return fileName;
// }
public InputStream download(String bucketName, String objectName) throws MinioException, IOException {
try {
return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public InputStream download(String path) throws MinioException, IOException {
if (!path.contains("/")) {
throw new BusinessException("The path is error!");
}
int index = path.indexOf("/");
String bucketName = path.substring(0, index);
String objectName = path.substring(index + 1);
return download(bucketName, objectName);
}
/**
* description: 下载文件
*
@@ -204,46 +249,46 @@ public class MinioUtil {
* @param bucketName
* @return: org.springframework.http.ResponseEntity<byte [ ]>
*/
public ResponseEntity<byte[]> download(String path, String bucketName) {
ResponseEntity<byte[]> responseEntity = null;
InputStream in = null;
ByteArrayOutputStream out = null;
try {
in = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(path).build());
out = new ByteArrayOutputStream();
IOUtils.copy(in, out);
//封装返回值
byte[] bytes = out.toByteArray();
HttpHeaders headers = new HttpHeaders();
try {
headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(path, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
headers.setContentLength(bytes.length);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setAccessControlExposeHeaders(Arrays.asList("*"));
responseEntity = new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return responseEntity;
}
// public ResponseEntity<byte[]> download(String path, String bucketName) {
// ResponseEntity<byte[]> responseEntity = null;
// InputStream in = null;
// ByteArrayOutputStream out = null;
// try {
// in = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(path).build());
// out = new ByteArrayOutputStream();
// IOUtils.copy(in, out);
// //封装返回值
// byte[] bytes = out.toByteArray();
// HttpHeaders headers = new HttpHeaders();
// try {
// headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(path, "UTF-8"));
// } catch (UnsupportedEncodingException e) {
// e.printStackTrace();
// }
// headers.setContentLength(bytes.length);
// headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
// headers.setAccessControlExposeHeaders(Arrays.asList("*"));
// responseEntity = new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// try {
// if (in != null) {
// try {
// in.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// if (out != null) {
// out.close();
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// return responseEntity;
// }
/**
* 查看文件对象
@@ -280,6 +325,26 @@ public class MinioUtil {
return results;
}
public void deleteObject(String bucketName, String objectName) {
try {
minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
System.out.println("Object " + objectName + " successfully removed from bucket " + bucketName);
} catch (Exception e) {
e.printStackTrace();
System.err.println("Error while removing object " + objectName + " from bucket " + bucketName + ": " + e.getMessage());
}
}
public void deleteObject(String path) {
if (!path.contains("/")) {
throw new BusinessException("The path is error!");
}
int index = path.indexOf("/");
String bucketName = path.substring(0, index);
String objectName = path.substring(index + 1);
deleteObject(bucketName, objectName);
}
/**
* 获取文件的临时URL
*
@@ -332,6 +397,21 @@ public class MinioUtil {
}
return getPresignedUrl(bucketName, String.valueOf(fileName),expiry);
}
public boolean doesObjectExist(String bucketName, String objectName) {
try {
minioClient.statObject(
StatObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build()
);
return true;
} catch (Exception e) {
// 如果发生异常,说明文件不存在或者出现了其他错误
return false;
}
}
}