33import cn .hutool .core .bean .BeanUtil ;
44import cn .hutool .core .io .FileUtil ;
55import io .github .talelin .merak .bo .FileBO ;
6- import io .github .talelin .merak .extensions .file .File ;
76import io .github .talelin .merak .extensions .file .FileConstant ;
87import io .github .talelin .merak .extensions .file .Uploader ;
98import io .github .talelin .merak .mapper .FileMapper ;
1817
1918import java .util .ArrayList ;
2019import java .util .List ;
21- import java .util .stream .Collectors ;
2220
2321/**
2422 * @author pedro
@@ -44,35 +42,39 @@ public class FileServiceImpl extends ServiceImpl<FileMapper, FileDO> implements
4442 */
4543 @ Override
4644 public List <FileBO > upload (MultiValueMap <String , MultipartFile > fileMap ) {
47- List <FileDO > tmp = new ArrayList <>();
48- List < File > files = uploader .upload (fileMap , file -> {
45+ List <FileBO > res = new ArrayList <>();
46+ uploader .upload (fileMap , file -> {
4947 FileDO found = this .baseMapper .selectByMd5 (file .getMd5 ());
50- if (found == null )
48+ // 数据库中不存在
49+ if (found == null ) {
50+ FileDO fileDO = new FileDO ();
51+ BeanUtil .copyProperties (file , fileDO );
52+ this .getBaseMapper ().insert (fileDO );
53+ res .add (transformDoToBo (fileDO , file .getKey ()));
5154 return true ;
52- tmp .add (found );
55+ }
56+ // 已存在,则直接转化返回
57+ res .add (transformDoToBo (found , file .getKey ()));
5358 return false ;
5459 });
55- tmp .addAll (files .stream ().map (file -> {
56- FileDO fileDO = new FileDO ();
57- BeanUtil .copyProperties (file , fileDO );
58- this .getBaseMapper ().insert (fileDO );
59- return fileDO ;
60- }).collect (Collectors .toList ()));
61-
62- List <FileBO > res = tmp .stream ().map (file -> {
63- FileBO bo = new FileBO ();
64- BeanUtil .copyProperties (file , bo );
65- if (file .getType ().equals (FileConstant .LOCAL )) {
66- String s = FileUtil .mainName (dir );
67- bo .setPath (domain + s + "/" + file .getName ());
68- }
69- return bo ;
70- }).collect (Collectors .toList ());
7160 return res ;
7261 }
7362
7463 @ Override
7564 public boolean checkFileExistByMd5 (String md5 ) {
7665 return this .getBaseMapper ().selectCountByMd5 (md5 ) > 0 ;
7766 }
67+
68+ private FileBO transformDoToBo (FileDO file , String key ) {
69+ FileBO bo = new FileBO ();
70+ BeanUtil .copyProperties (file , bo );
71+ if (file .getType ().equals (FileConstant .LOCAL )) {
72+ String s = FileUtil .mainName (dir );
73+ bo .setUrl (domain + s + "/" + file .getName ());
74+ } else {
75+ bo .setUrl (file .getPath ());
76+ }
77+ bo .setKey (key );
78+ return bo ;
79+ }
7880}
0 commit comments