添加minio相关接口

This commit is contained in:
ETHEREAL 2024-06-11 12:02:12 +08:00
parent b9ffbdb088
commit 64afc10223
8 changed files with 75 additions and 29 deletions

View File

@ -6,21 +6,21 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//@Data
//@Configuration
//@ConfigurationProperties(prefix = "minio")
//public class MinioConfig {
// private String endpoint;
// private String bucket;
// private String accessKey;
// private String secretKey;
//
// @Bean
// public MinioClient minioClient(){
// return MinioClient.builder()
// .endpoint(endpoint)
// .credentials(accessKey, secretKey)
// .build();
// }
//
//}
@Data
@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
private String endpoint;
private String bucket;
private String accessKey;
private String secretKey;
@Bean
public MinioClient minioClient(){
return MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
}
}

View File

@ -24,4 +24,6 @@ public interface CommonConstant {
String USERNAME = "username";
String STATUS = "status";
String BUCKET = "catchtheletters";
}

View File

@ -11,7 +11,9 @@ import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
/**
@ -105,7 +107,14 @@ public class LevelController {
return R.ok(levelService.getScoreInfo( page.getLevelId(),page.getPage(), page.getSize()));
}
@ApiOperation(value = "获取最新5个版本")
@GetMapping("/new-version")
public R<List<VersionDownload>> getNewVersion(){
return versionService.getNewDownloads();
}
@PostMapping("/test")
public R<String> testMinio(MultipartFile file) throws Exception {
return R.ok(versionService.testMinIO(file));
}
}

View File

@ -2,7 +2,9 @@ package com.example.catchTheLetters.service;
import com.example.catchTheLetters.entity.VersionDownload;
import com.example.catchTheLetters.utils.R;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
/**
@ -33,11 +35,13 @@ public interface VersionService {
* @param version 版本号
* @param url 地址
*/
R<String> addVersionDownload(Long time, String version, String url);
R<String> addVersionDownload(Long time, String version, String url, MultipartFile file);
/**
* 获取最新的5个版本
* @return 版本集合
*/
R<List<VersionDownload>> getNewDownloads();
String testMinIO(MultipartFile file) throws Exception;
}

View File

@ -91,11 +91,8 @@ public class LevelServiceImpl implements LevelService {
public RankVo getRankInfo(String levelId, String token) {
// 在ScoreInfo表当中查询levelId为levelId的所有数据按照分数降序排列取前10条数据
var info = mongoTemplate.find(new Query(Criteria.where("levelId").is(levelId)).limit(10), ScoreInfo.class);
var rankVo = new RankVo();
rankVo.setScoreInfos(info);
// 如果有token就查自己的分数数据
if (token != null) {
var map = JwtUtil.getPayload(token);

View File

@ -1,16 +1,27 @@
package com.example.catchTheLetters.service.impl;
import com.example.catchTheLetters.config.MinioConfig;
import com.example.catchTheLetters.constant.CommonConstant;
import com.example.catchTheLetters.entity.Version;
import com.example.catchTheLetters.entity.VersionDownload;
import com.example.catchTheLetters.service.VersionService;
import com.example.catchTheLetters.utils.R;
import io.minio.*;
import io.minio.errors.*;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
@Service
@ -19,6 +30,11 @@ public class VersionServiceImpl implements VersionService {
@Resource
private MongoTemplate mongoTemplate;
@Resource
private MinioClient minioClient;
@Autowired
private MinioConfig minioConfig;
@Override
public Long getVersion(String tableName) {
var res = mongoTemplate.findOne(new Query(Criteria.where("table").is(tableName)), Version.class);
@ -37,7 +53,7 @@ public class VersionServiceImpl implements VersionService {
}
@Override
public R<String> addVersionDownload(Long time, String version, String url) {
public R<String> addVersionDownload(Long time, String version, String url, MultipartFile file) {
VersionDownload versionDownload = new VersionDownload();
versionDownload.setUrl(url);
versionDownload.setTime(time);
@ -47,6 +63,7 @@ public class VersionServiceImpl implements VersionService {
return R.fail("添加数据库失败");
}
//TODO
return R.ok(insert.getId());
}
@ -58,4 +75,12 @@ public class VersionServiceImpl implements VersionService {
List<VersionDownload> list = mongoTemplate.find(query, VersionDownload.class);
return R.ok(list);
}
@Override
public String testMinIO(MultipartFile file) throws Exception {
System.out.println(minioConfig.getBucket());
minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioConfig.getBucket()).build());
// boolean bucketFound = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioConfig.getBucket()).build());
return "未找到";
}
}

View File

@ -24,8 +24,8 @@ spring:
server:
port: 3536
#minio:
# endpoint: http://1.14.105.160:9000
# bucketName:
# accessKey:
# secretKey:
minio:
endpoint: http://1.14.105.160:9000
bucket: catchtheletters
accessKey: spyn
secretKey: pengjunda1

View File

@ -4,6 +4,7 @@ import com.example.catchTheLetters.controller.LevelController;
import com.example.catchTheLetters.entity.Level;
import com.example.catchTheLetters.entity.ScoreInfo;
import com.example.catchTheLetters.entity.VersionDownload;
import com.example.catchTheLetters.service.VersionService;
import com.example.catchTheLetters.utils.R;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Disabled;
@ -29,6 +30,9 @@ class TestInsertLevel {
@Resource
private MongoTemplate mongoTemplate;
@Resource
private VersionService versionService;
private final List<String> words = new ArrayList<>() {
{
add("apple");
@ -114,4 +118,9 @@ class TestInsertLevel {
var list = mongoTemplate.find(query, VersionDownload.class);
System.out.println(list);
}
@Test
@Disabled
void minIOTest(){
}
}