fix: [mongo测试文件存储]
This commit is contained in:
parent
1c6b2f75b6
commit
7fe4cda97b
6
pom.xml
6
pom.xml
|
@ -298,6 +298,12 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.kafka</groupId>
|
<groupId>org.springframework.kafka</groupId>
|
||||||
<artifactId>spring-kafka</artifactId>
|
<artifactId>spring-kafka</artifactId>
|
||||||
|
|
|
@ -5,26 +5,23 @@ import com.guwan.backend.annotation.OperationLog;
|
||||||
import com.guwan.backend.client.SimpleTTSClient;
|
import com.guwan.backend.client.SimpleTTSClient;
|
||||||
import com.guwan.backend.client.VoiceServiceClient;
|
import com.guwan.backend.client.VoiceServiceClient;
|
||||||
import com.guwan.backend.common.Result;
|
import com.guwan.backend.common.Result;
|
||||||
import com.guwan.backend.pojo.entity.BookContent;
|
|
||||||
import com.guwan.backend.mongodb.EveryReadDetailOfMongodb;
|
import com.guwan.backend.mongodb.EveryReadDetailOfMongodb;
|
||||||
import com.guwan.backend.mongodb.EveryReadDetailOfMongodbService;
|
import com.guwan.backend.mongodb.EveryReadDetailOfMongodbService;
|
||||||
import com.guwan.backend.mongodb.User;
|
|
||||||
import com.guwan.backend.mongodb.MongodbUserService;
|
import com.guwan.backend.mongodb.MongodbUserService;
|
||||||
|
import com.guwan.backend.mongodb.User;
|
||||||
|
import com.guwan.backend.pojo.entity.BookContent;
|
||||||
import com.guwan.backend.util.MinioUtil;
|
import com.guwan.backend.util.MinioUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
|
|
@ -4,19 +4,20 @@ import cn.easyes.core.conditions.LambdaEsQueryWrapper;
|
||||||
import com.guwan.backend.common.Result;
|
import com.guwan.backend.common.Result;
|
||||||
import com.guwan.backend.elasticsearch.document.ProductDocument;
|
import com.guwan.backend.elasticsearch.document.ProductDocument;
|
||||||
import com.guwan.backend.elasticsearch.mapper.ProductEsMapper;
|
import com.guwan.backend.elasticsearch.mapper.ProductEsMapper;
|
||||||
|
import com.guwan.backend.mongodb.FileStorageService;
|
||||||
import com.guwan.backend.util.MinioUtil;
|
import com.guwan.backend.util.MinioUtil;
|
||||||
|
import com.mongodb.client.gridfs.model.GridFSFile;
|
||||||
import io.minio.MinioClient;
|
import io.minio.MinioClient;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.elasticsearch.search.SearchHit;
|
import org.elasticsearch.search.SearchHit;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@ -37,6 +38,8 @@ public class DemoController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private ProductEsMapper productEsMapper;
|
private ProductEsMapper productEsMapper;
|
||||||
|
|
||||||
|
private final FileStorageService fileStorageService;
|
||||||
|
|
||||||
@PostMapping("/uploadFile")
|
@PostMapping("/uploadFile")
|
||||||
public Result<String> uploadFile(String bucketName, MultipartFile file){
|
public Result<String> uploadFile(String bucketName, MultipartFile file){
|
||||||
return Result.success(minioUtil.getUrl(minioUtil.getFileUrl
|
return Result.success(minioUtil.getUrl(minioUtil.getFileUrl
|
||||||
|
@ -97,4 +100,17 @@ public class DemoController {
|
||||||
return Result.success("demo222");
|
return Result.success("demo222");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping("/mongoFile")
|
||||||
|
public void mongoFile(@RequestPart("file") MultipartFile file) throws IOException {
|
||||||
|
fileStorageService.storeFile(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/mongoFile")
|
||||||
|
public void getMongoFile(String fileId) throws IOException {
|
||||||
|
GridFSFile file = fileStorageService.getFile(fileId);
|
||||||
|
System.out.println("file = " + file);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,12 +93,12 @@ public class QuestionsController {
|
||||||
Questions question = questionsService.getById(id);
|
Questions question = questionsService.getById(id);
|
||||||
|
|
||||||
|
|
||||||
System.out.println(MultipleChoiceCompare.compare(question.getPoint(), question.getChoices(),
|
System.out.println(MultipleChoiceCompare.compare(question.getPoint(), question.getAnswer(),
|
||||||
"A"));
|
"A"));
|
||||||
|
|
||||||
System.out.println(MultipleChoiceCompare.compare(question.getPoint(), question.getChoices(),
|
System.out.println(MultipleChoiceCompare.compare(question.getPoint(), question.getAnswer(),
|
||||||
"A, B"));
|
"A, B"));
|
||||||
System.out.println(MultipleChoiceCompare.compare(question.getPoint(), question.getChoices(),
|
System.out.println(MultipleChoiceCompare.compare(question.getPoint(), question.getAnswer(),
|
||||||
"A, C"));
|
"A, C"));
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,67 @@
|
||||||
|
package com.guwan.backend.mongodb;
|
||||||
|
|
||||||
|
import com.mongodb.client.gridfs.model.GridFSFile;
|
||||||
|
import org.bson.types.ObjectId;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.mongodb.core.query.Criteria;
|
||||||
|
import org.springframework.data.mongodb.core.query.Query;
|
||||||
|
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class FileStorageService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private GridFsTemplate gridFsTemplate;
|
||||||
|
|
||||||
|
public String storeFile(MultipartFile file) throws IOException {
|
||||||
|
// 获取文件名和输入流
|
||||||
|
String fileName = file.getOriginalFilename();
|
||||||
|
InputStream inputStream = file.getInputStream();
|
||||||
|
|
||||||
|
// 存储文件到 GridFS
|
||||||
|
return gridFsTemplate.store(inputStream, fileName).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public GridFSFile getFile(String fileId) throws IOException {
|
||||||
|
|
||||||
|
ObjectId objectId = new ObjectId(fileId);
|
||||||
|
|
||||||
|
|
||||||
|
Query query = new Query(Criteria.where("_id").is(objectId));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 根据文件 ID 获取文件
|
||||||
|
GridFSFile gridFSFile = gridFsTemplate.findOne(query);
|
||||||
|
|
||||||
|
if (gridFSFile == null) {
|
||||||
|
throw new IOException("文件未找到: " + fileId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取文件的输入流
|
||||||
|
try (InputStream inputStream = gridFsTemplate.getResource(gridFSFile)
|
||||||
|
.getInputStream()) {
|
||||||
|
// 将文件保存到本地
|
||||||
|
Path localPath = Paths.get("D:\\00_桌面\\新建文件夹", gridFSFile.getFilename());
|
||||||
|
Files.copy(inputStream, localPath, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
System.out.println("文件已保存到: " + localPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
return gridFSFile;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteFile(String fileId) {
|
||||||
|
// 根据文件 ID 删除文件
|
||||||
|
gridFsTemplate.delete(new Query(Criteria.where("_id").is(fileId)));
|
||||||
|
}
|
||||||
|
}
|
|
@ -49,7 +49,7 @@ public class PapersServiceImpl extends ServiceImpl<PapersMapper, Papers>
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
redisUtil.set(PAPER_CACHE_KEY + id, paper);
|
redisUtil.set(PAPER_CACHE_KEY + id, paper,30);
|
||||||
|
|
||||||
return paper;
|
return paper;
|
||||||
}
|
}
|
||||||
|
|
|
@ -212,7 +212,7 @@ logging:
|
||||||
xxl:
|
xxl:
|
||||||
job:
|
job:
|
||||||
admin:
|
admin:
|
||||||
addresses: http://192.168.0.6:9001/xxl-job-admin
|
addresses: http://192.168.0.121:9001/xxl-job-admin
|
||||||
accessToken: GuwanTest
|
accessToken: GuwanTest
|
||||||
executor:
|
executor:
|
||||||
appname: xxl-job-executor-guwan
|
appname: xxl-job-executor-guwan
|
||||||
|
|
|
@ -1,64 +0,0 @@
|
||||||
package com.guwan.backend.service;
|
|
||||||
|
|
||||||
import com.guwan.backend.pojo.entity.Book;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.mockito.InjectMocks;
|
|
||||||
import org.mockito.Mock;
|
|
||||||
import org.mockito.MockitoAnnotations;
|
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
|
||||||
import static org.mockito.Mockito.*;
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
|
||||||
class BookServiceTest {
|
|
||||||
|
|
||||||
@Mock
|
|
||||||
private BookMapper bookMapper;
|
|
||||||
|
|
||||||
@InjectMocks
|
|
||||||
private BookServiceImpl bookService;
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void setUp() {
|
|
||||||
MockitoAnnotations.openMocks(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void addBook_Success() {
|
|
||||||
// 准备测试数据
|
|
||||||
Book book = new Book();
|
|
||||||
book.setName("测试书籍");
|
|
||||||
book.setAuthor("测试作者");
|
|
||||||
book.setIsbn("9787000000000");
|
|
||||||
|
|
||||||
when(bookMapper.insert(any(Book.class))).thenReturn(1);
|
|
||||||
|
|
||||||
// 执行测试
|
|
||||||
Book result = bookService.addBook(book);
|
|
||||||
|
|
||||||
// 验证结果
|
|
||||||
assertNotNull(result);
|
|
||||||
assertEquals("测试书籍", result.getName());
|
|
||||||
verify(bookMapper, times(1)).insert(any(Book.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getBookByIsbn_Success() {
|
|
||||||
// 准备测试数据
|
|
||||||
Book book = new Book();
|
|
||||||
book.setId(1L);
|
|
||||||
book.setName("测试书籍");
|
|
||||||
book.setIsbn("9787000000000");
|
|
||||||
|
|
||||||
when(bookMapper.selectOne(any())).thenReturn(book);
|
|
||||||
|
|
||||||
// 执行测试
|
|
||||||
Book result = bookService.getBookByIsbn("9787000000000");
|
|
||||||
|
|
||||||
// 验证结果
|
|
||||||
assertNotNull(result);
|
|
||||||
assertEquals("测试书籍", result.getName());
|
|
||||||
assertEquals("9787000000000", result.getIsbn());
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue