fix: 上传修复

This commit is contained in:
zlg
2024-07-18 21:13:04 +08:00
parent 44400b30ba
commit 93fb58c4c5
3 changed files with 35 additions and 18 deletions

View File

@@ -1,8 +1,13 @@
package com.jd.platform.async.callback;
import java.io.IOException;
import java.rmi.ServerException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import com.jd.platform.async.wrapper.WorkerWrapper;
import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
/**
* 每个最小执行单元需要实现该接口
@@ -17,7 +22,7 @@ public interface IWorker<T, V> {
* @param object object
* @param allWrappers 任务包装
*/
V action(T object, Map<String, WorkerWrapper> allWrappers);
V action(T object, Map<String, WorkerWrapper> allWrappers) throws IOException, NoSuchAlgorithmException, InvalidKeyException, InternalException;
/**
* 超时、异常时,返回的默认值

View File

@@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSON;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.base.Preconditions;
import com.google.gson.Gson;
import com.mybatisflex.annotation.RelationOneToMany;
import com.schisandra.oss.application.convert.SchisandraOssMinioDTOConverter;
import com.schisandra.oss.application.convert.SchisandraOssQiniuDTOConverter;
@@ -14,6 +15,7 @@ import com.schisandra.oss.application.dto.SchisandraOssQiniuDTO;
import com.schisandra.oss.application.oss.constant.OssConstant;
import com.schisandra.oss.application.oss.core.minio.MinioOssClient;
import com.schisandra.oss.application.oss.core.minio.MinioOssConfiguration;
import com.schisandra.oss.application.oss.model.OssInfo;
import com.schisandra.oss.common.entity.Result;
import com.schisandra.oss.common.utils.CaffeineUtil;
import com.schisandra.oss.domain.bo.SchisandraOssAliBO;
@@ -170,13 +172,20 @@ public class SchisandraOssMinioController {
* @author zlg
* @date: 2024/6/26 13:55
*/
@Cacheable(value = "MinioListDir", key = "#userId+#bucket+#dirName",unless = "#result == null")
@GetMapping("listDir")
public Result listMinioInfo(@RequestParam String userId, @RequestParam String dirName, @RequestParam String bucket) throws Exception {
Preconditions.checkNotNull(userId, "不能为空");
MinioOssClient bean = SpringUtil.getBean(OssConstant.OssType.MINIO + userId);
bean.getMinioOssConfig().setBucketName(bucket);
return Result.ok(bean.listDir(bucket, dirName));
List<OssInfo> object= (List<OssInfo>) caffeineUtil.caffeineBuild().getIfPresent(userId+bucket+dirName);
if (object != null) {
return Result.ok(object);
}else {
List<OssInfo> list=bean.listDir(bucket,dirName);
caffeineUtil.caffeineBuild().put(userId+bucket+dirName, list);
return Result.ok(list);
}
}
/**
@@ -230,7 +239,7 @@ public class SchisandraOssMinioController {
* @author zlg
* @date: 2024/6/26 14:34
*/
@CacheEvict(value = "MinioListDir", key = "#userId+#bucket+#filePath")
@CacheEvict(value = "MinioListDir", key = "#userId+#bucket+#filePath.substring(filePath.lastIndexOf('/'))" )
@PostMapping("deleteFile")
public Result deleteMinioFile(@RequestParam String bucket, @RequestParam String userId, @RequestParam String filePath) {
Preconditions.checkNotNull(bucket, "不能为空");
@@ -238,9 +247,7 @@ public class SchisandraOssMinioController {
Preconditions.checkNotNull(filePath, "不能为空");
int index=filePath.lastIndexOf("/");
String fileName = filePath.substring(index + 1);
System.out.println(caffeineUtil.caffeineUtil().getIfPresent("MinioListDir" + userId + bucket + fileName));
caffeineUtil.caffeineUtil().invalidate("MinioListDir" + userId + bucket + fileName);
caffeineUtil.caffeineBuild().invalidate("MinioListDir" + userId + bucket + fileName);
MinioOssClient bean = SpringUtil.getBean(OssConstant.OssType.MINIO + userId);
bean.getMinioOssConfig().setBucketName(bucket);
bean.delete(filePath);
@@ -254,12 +261,17 @@ public class SchisandraOssMinioController {
* @author zlg
* @date: 2024/6/26 14:34
*/
@PostMapping("uploadFile")
public Result<Object> uploadMinioFile(@RequestParam String userId, @RequestParam MultipartFile file, @RequestParam String fileName, @RequestParam String bucket) throws IOException {
public Result<Object> uploadMinioFile(@RequestParam String userId, @RequestParam MultipartFile file, @RequestParam String path, @RequestParam String bucket) throws IOException {
Preconditions.checkNotNull(userId, "不能为空");
Preconditions.checkNotNull(fileName, "不能为空");
Preconditions.checkNotNull(path, "不能为空");
Preconditions.checkNotNull(bucket, "不能为空");
Preconditions.checkNotNull(file, "不能为空");
if (path.lastIndexOf("") == path.length() - 1) {
path=path+file.getName();
}else{
path=path+"/"+file.getOriginalFilename();
}
// 获取文件输入流
InputStream is = file.getInputStream();
MinioOssClient bean = SpringUtil.getBean(OssConstant.OssType.MINIO + userId);
@@ -268,13 +280,13 @@ public class SchisandraOssMinioController {
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = dateFormat.format(date);
String key = redisUtil.buildKey(USER_OSS_PREFIX, "upload", formattedDate, userId, fileName);
String key = redisUtil.buildKey(USER_OSS_PREFIX, "upload", formattedDate, userId, path);
HashMap<Object, Object> map = new HashMap<>();
map.put("fileName", fileName);
map.put("fileName", path);
map.put("descreption", "这是一个文件");
map.put("size", String.valueOf(file.getSize()));
map.put("time", formattedDate);
if (bean.upLoad(is, fileName, true) != null) {
if (bean.upLoad(is, path, true) != null) {
redisUtil.setNx(key, map, 30L, TimeUnit.DAYS);
return Result.ok();
} else {

View File

@@ -15,14 +15,14 @@ import java.util.concurrent.TimeUnit;
@Configuration
@Setter
@Slf4j
public class CaffeineUtil {
public class CaffeineUtil <T>{
public static int maximumSize=10000;
public static int expiresTime = 3;
public static int expiresTime = 1;
public static TimeUnit unitTime = TimeUnit.MINUTES;
// @Bean
public Cache<String,String> caffeineUtil(){
Cache<String, String> cache = Caffeine.newBuilder()
@Bean
public Cache<String,T> caffeineBuild(){
Cache<String, T> cache = Caffeine.newBuilder()
.maximumSize(maximumSize)
.expireAfterWrite(expiresTime, unitTime)
.build();