Skip to content

Commit 942b1a6

Browse files
fuhaocolorful3
authored andcommitted
fix issue #131
1 parent bfcf21e commit 942b1a6

4 files changed

Lines changed: 37 additions & 20 deletions

File tree

src/main/java/io/github/talelin/latticy/module/file/AbstractUploader.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
public abstract class AbstractUploader implements Uploader {
2020

21-
private PreHandler preHandler;
21+
private UploadHandler uploadHandler;
2222

2323
@Override
2424
public List<File> upload(MultiValueMap<String, MultipartFile> fileMap) {
@@ -29,8 +29,8 @@ public List<File> upload(MultiValueMap<String, MultipartFile> fileMap) {
2929
}
3030

3131
@Override
32-
public List<File> upload(MultiValueMap<String, MultipartFile> fileMap, PreHandler preHandler) {
33-
this.preHandler = preHandler;
32+
public List<File> upload(MultiValueMap<String, MultipartFile> fileMap, UploadHandler uploadHandler) {
33+
this.uploadHandler = uploadHandler;
3434
return this.upload(fileMap);
3535
}
3636

@@ -64,12 +64,16 @@ private void handleOneFile0(List<File> res, long singleFileLimit, MultipartFile
6464
extension(ext).
6565
build();
6666
// 如果预处理器不为空,且处理结果为false,直接返回, 否则处理
67-
if (preHandler != null && !preHandler.handle(fileData)) {
67+
if (uploadHandler != null && !uploadHandler.preHandle(fileData)) {
6868
return;
6969
}
7070
boolean ok = handleOneFile(bytes, newFilename);
7171
if (ok) {
7272
res.add(fileData);
73+
// 上传到本地或云上成功之后,调用afterHandle
74+
if (uploadHandler != null) {
75+
uploadHandler.afterHandle(fileData);
76+
}
7377
}
7478
}
7579

src/main/java/io/github/talelin/latticy/module/file/PreHandler.java renamed to src/main/java/io/github/talelin/latticy/module/file/UploadHandler.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
*
66
* @author pedro@TaleLin
77
*/
8-
public interface PreHandler {
8+
public interface UploadHandler {
99

1010
/**
1111
* 在文件写入本地或者上传到云之前调用此方法
1212
*
1313
* @return 是否上传,若返回false,则不上传
1414
*/
15-
boolean handle(File file);
15+
boolean preHandle(File file);
16+
17+
/**
18+
* 在文件写入本地或者上传到云之后调用此方法
19+
*/
20+
void afterHandle(File file);
1621
}

src/main/java/io/github/talelin/latticy/module/file/Uploader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public interface Uploader {
2424
* 上传文件
2525
*
2626
* @param fileMap 文件map
27-
* @param preHandler 预处理器
27+
* @param uploadHandler 预处理器
2828
* @return 文件数据
2929
*/
30-
List<File> upload(MultiValueMap<String, MultipartFile> fileMap, PreHandler preHandler);
30+
List<File> upload(MultiValueMap<String, MultipartFile> fileMap, UploadHandler uploadHandler);
3131
}

src/main/java/io/github/talelin/latticy/service/impl/FileServiceImpl.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import io.github.talelin.latticy.bo.FileBO;
55
import io.github.talelin.latticy.mapper.FileMapper;
66
import io.github.talelin.latticy.model.FileDO;
7-
import io.github.talelin.latticy.module.file.FileConstant;
8-
import io.github.talelin.latticy.module.file.FileProperties;
9-
import io.github.talelin.latticy.module.file.Uploader;
7+
import io.github.talelin.latticy.module.file.*;
108
import io.github.talelin.latticy.service.FileService;
119
import org.springframework.beans.BeanUtils;
1210
import org.springframework.beans.factory.annotation.Autowired;
@@ -43,19 +41,29 @@ public class FileServiceImpl extends ServiceImpl<FileMapper, FileDO> implements
4341
@Override
4442
public List<FileBO> upload(MultiValueMap<String, MultipartFile> fileMap) {
4543
List<FileBO> res = new ArrayList<>();
46-
uploader.upload(fileMap, file -> {
47-
FileDO found = this.baseMapper.selectByMd5(file.getMd5());
48-
// 数据库中不存在
49-
if (found == null) {
44+
45+
uploader.upload(fileMap, new UploadHandler() {
46+
@Override
47+
public boolean preHandle(File file) {
48+
FileDO found = baseMapper.selectByMd5(file.getMd5());
49+
// 数据库中不存在,存储操作放在上传到本地或云上之后,
50+
// 修复issue131:https://github.com/TaleLin/lin-cms-spring-boot/issues/131
51+
if (found == null) {
52+
return true;
53+
}
54+
// 已存在,则直接转化返回
55+
res.add(transformDoToBo(found, file.getKey()));
56+
return false;
57+
}
58+
59+
@Override
60+
public void afterHandle(File file) {
61+
// 保存到数据库, 修复issue131:https://github.com/TaleLin/lin-cms-spring-boot/issues/131
5062
FileDO fileDO = new FileDO();
5163
BeanUtils.copyProperties(file, fileDO);
52-
this.getBaseMapper().insert(fileDO);
64+
getBaseMapper().insert(fileDO);
5365
res.add(transformDoToBo(fileDO, file.getKey()));
54-
return true;
5566
}
56-
// 已存在,则直接转化返回
57-
res.add(transformDoToBo(found, file.getKey()));
58-
return false;
5967
});
6068
return res;
6169
}

0 commit comments

Comments
 (0)