jinlin
2025-03-01 86f02fee03614fef275c6e0c355d73318ca3025e
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
package com.example.server.sysOss.service;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.example.client.entity.ErrorCode;
import com.example.client.entity.RenException;
import com.example.client.service.BaseService;
import com.example.server.sysOss.dao.SysOssDao;
import com.example.server.sysOss.dto.OssDto;
import com.example.server.sysOss.model.SysOss;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
 
import javax.annotation.PostConstruct;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.io.*;
import java.lang.reflect.Field;
import java.security.Key;
import java.security.SecureRandom;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 文件上传
 *
 * @author hehz
 */
@Service
public class SysOssService extends BaseService<SysOssDao, SysOss> {
    @Autowired
    OssEncryptService ossEncryptService;
    @Value("${zt.oss.local-path}")
    private String localPath;
 
 
    private static final String CIPHER_ALGORITHM = "AES";
    private static final String key = "zhpt-key#%W";
 
    /**
     * 通过业务id BusiId和业务类型 busiType获取附件数据
     *
     * @return
     */
    public List<SysOss> getList(Long busiId, String busiType) {
        return baseDao.getList(busiId, busiType);
    }
 
    public void upload(SysOss oss) {
        oss.setId(IdWorker.getId());
        File oldFile = new File(oss.getPath());
        oss.setSize(NumberUtil.round(oldFile.length() / 1024, 2).doubleValue());
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        // 文件路径
        String path = DateUtil.format(new Date(), "yyyyMMdd") + "/" + uuid;
        String newPath = "life-protection/" + path + FilenameUtils.getExtension(oss.getPath());
        oss.setPath(newPath);
        oss.setType(FilenameUtils.getExtension(oss.getPath()));
        String newFullPath = localPath + newPath;
        File newFile = new File(newFullPath);
        try {
            FileUtils.copyToFile(ossEncryptService.encryptStream(FileUtils.openInputStream(oldFile), ossEncryptService.getKey(key)), newFile);
        } catch (Exception e) {
            throw new RenException(ErrorCode.OSS_UPLOAD_FILE_ERROR.getCode(), e, "");
        }
        this.insert(oss);
    }
 
 
    public Integer getBusiFieldNameCount(Long busiId, String busiType) {
        return baseDao.getBusiFieldNameCount(busiId, busiType);
    }
 
    public List<SysOss> getSysOssByBusiType(Map<String, Object> params) {
        return baseDao.getSysOssByBusiType(params);
    }
 
}