2021与蓝度共同重构项目,服务端
zhanzhiqin
2022-04-20 312224aeab92015542dd1396d349c5e68adb570e
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.sandu.ximon.admin.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.file.FileUploadDto;
import com.sandu.common.object.BaseConditionVO;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.ximon.admin.minio.BroadcastFileUtils;
import com.sandu.ximon.admin.minio.entity.FileSuffix;
import com.sandu.ximon.admin.minio.entity.MinIoConstant;
import com.sandu.ximon.admin.minio.utils.MinIoUtil;
import com.sandu.ximon.admin.param.IpVolumeFileParam;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.dao.domain.IpVolumeFile;
import com.sandu.ximon.dao.mapper.IpVolumeFileMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
/**
 * @Author liuhaonan
 * @Date 2021/12/16 10:48
 * @Version 1.0
 */
 
@Service
@AllArgsConstructor
public class IpVolumeFileService extends BaseServiceImpl<IpVolumeFileMapper, IpVolumeFile> {
    private final IpVolumeFileMapper ipVolumeFileMapper;
    private final ClientService clientService;
    private MinIoUtil minIoUtil;
 
 
    public boolean addFile(IpVolumeFileParam fileParam) {
        IpVolumeFile file = new IpVolumeFile();
//        if(SecurityUtils.getClientId()!=null){
//            file.setUserName(SecurityUtils.getUsername());
//        }
//
        if (SecurityUtils.getClientId() != null) {
            file.setUserName(SecurityUtils.getUsername());
            file.setUserId(SecurityUtils.getClientId());
            file.setClientId(clientService.getClientId());
        }
 
        file.setFileName(fileParam.getFileName());
        file.setFileUrl(fileParam.getFileUrl());
        file.setOriginSize(fileParam.getOriginSize());
        return save(file);
    }
 
    public boolean deleteFile(Long fileId) {
        IpVolumeFile byId = getById(fileId);
        String bucketName;
        if (SecurityUtils.getClientId() == null) {
            bucketName = MinIoConstant.ADMIN_BROADCAST_FILE.getBucketName();
        } else {
            bucketName = MinIoConstant.BROADCAST_FILE.getBucketName() + SecurityUtils.getUserId();
        }
        if (byId == null) {
            throw new BusinessException("找不到对应文件");
        }
        boolean b = minIoUtil.deleteFile(bucketName, byId.getFileName());
//        if (!b) {
//            throw new BusinessException("服务器删除失败");
//        }
 
 
        return removeById(fileId);
    }
 
    public LambdaQueryWrapper<IpVolumeFile> listFile(BaseConditionVO baseConditionVO, String keyword) {
        PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize());
        LambdaQueryWrapper<IpVolumeFile> wrapper = Wrappers.lambdaQuery(IpVolumeFile.class);
 
        //不是超管
        if (SecurityUtils.getClientId() != null) {
            wrapper = wrapper.eq(IpVolumeFile::getClientId, SecurityUtils.getUserId())
                    .or(file -> {
                        file.eq(IpVolumeFile::getUserId, SecurityUtils.getUserId());
                    });
        }
 
        if (keyword != null && !keyword.isEmpty()) {
            wrapper.like(IpVolumeFile::getFileName, keyword);
        }
        return wrapper;
    }
 
 
    /**
     * 文件上传
     *
     * @param file
     * @param
     * @return
     */
    public FileUploadDto upload(MultipartFile file) {
        log.error("音柱文件上传:(filename:" + file.getOriginalFilename() + "),(size: " + file.getSize() + "),用户id:" + SecurityUtils.getUserId());
        //  校验是否为mp3文件
        if (!BroadcastFileUtils.isMp3File(file.getOriginalFilename(), FileSuffix.MP3)) {
            log.error("音柱文件上传:(上传失败, 非MP3文件) (filename:" + file.getOriginalFilename() + "),(size: " + file.getSize() + "),用户id:" + SecurityUtils.getUserId());
            throw new BusinessException("上传失败, 非MP3文件");
        }
        //  判断该文件是否存在
        String bucketName;
        if (SecurityUtils.getClientId() == null) {
            bucketName = MinIoConstant.ADMIN_BROADCAST_FILE.getBucketName();
        } else {
            bucketName = MinIoConstant.BROADCAST_FILE.getBucketName() + SecurityUtils.getUserId();
        }
        if (minIoUtil.objectExists(bucketName, file.getOriginalFilename())) {
            log.error("音柱文件上传:(上传失败, 同名文件已存在) (filename:" + file.getOriginalFilename() + "),(size: " + file.getSize() + "),用户id:" + SecurityUtils.getUserId());
            throw new BusinessException("上传失败, 同名文件已存在");
        }
 
 
        String upload = minIoUtil.upload(bucketName, file);
 
        if (null == upload) {
            log.error("音柱文件上传:(上传失败, 文件服务器出错) (filename:" + file.getOriginalFilename() + "),(size: " + file.getSize() + "),用户id:" + SecurityUtils.getUserId());
            throw new BusinessException("上传失败, 文件服务器出错");
        }
        FileUploadDto fileUploadDto = new FileUploadDto();
        fileUploadDto.setFileUrl(upload);
        fileUploadDto.setFileSize(file.getSize());
        fileUploadDto.setFileName(file.getOriginalFilename());
        fileUploadDto.setFileType(file.getContentType());
        return fileUploadDto;
    }
 
}