tj
2025-06-05 2d549a04870d1315868a7cf19952b64e8071e711
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.cloudroam.service.impl;
 
import com.cloudroam.bo.FileBO;
import com.cloudroam.module.file.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudroam.mapper.FileMapper;
import com.cloudroam.model.FileDO;
import com.cloudroam.module.file.*;
import com.cloudroam.module.file.*;
import com.cloudroam.service.FileService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author 
 * @author 
 * @author 
 * 文件服务接口实现类
 */
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, FileDO> implements FileService {
 
    @Autowired
    private Uploader uploader;
 
    /**
     * 文件上传配置信息
     */
    @Autowired
    private FileProperties fileProperties;
 
    /**
     * 为什么不做批量插入
     * 1. 文件上传的数量一般不多,3个左右
     * 2. 批量插入不能得到数据的id字段,不利于直接返回数据
     * 3. 批量插入也仅仅只是一条sql语句的事情,如果真的需要,可以自行尝试一下
     */
    @Override
    public List<FileBO> upload(MultiValueMap<String, MultipartFile> fileMap) {
        List<FileBO> res = new ArrayList<>();
 
        uploader.upload(fileMap, new UploadHandler() {
            @Override
            public boolean preHandle(File file) {
                FileDO found = baseMapper.selectByMd5(file.getMd5());
                // 数据库中不存在,存储操作放在上传到本地或云上之后
                if (found == null) {
                    return true;
                }
                // 已存在,则直接转化返回
                res.add(transformDoToBo(found, file.getKey()));
                return false;
            }
 
            @Override
            public void afterHandle(File file) {
                // 保存到数据库
                FileDO fileDO = new FileDO();
                BeanUtils.copyProperties(file, fileDO);
                getBaseMapper().insert(fileDO);
                res.add(transformDoToBo(fileDO, file.getKey()));
            }
        });
        return res;
    }
 
    @Override
    public boolean checkFileExistByMd5(String md5) {
        return this.getBaseMapper().selectCountByMd5(md5) > 0;
    }
 
    private FileBO transformDoToBo(FileDO file, String key) {
        FileBO bo = new FileBO();
        BeanUtils.copyProperties(file, bo);
        if (file.getType().equals(FileTypeEnum.LOCAL.getValue())) {
            String s = fileProperties.getServePath().split("/")[0];
 
            // replaceAll 是将 windows 平台下的 \ 替换为 /
            if(System.getProperties().getProperty("os.name").toUpperCase().contains("WINDOWS")){
                bo.setUrl(fileProperties.getDomain() + s + "/" + file.getPath().replaceAll("\\\\","/"));
            } else {
                bo.setUrl(fileProperties.getDomain() + s + "/" + file.getPath());
            }
        } else {
            bo.setUrl(file.getPath());
        }
        bo.setKey(key);
        return bo;
    }
}