2021与蓝度共同重构项目,服务端
zhanzhiqin
2022-05-26 14299a0feab7b141df293cd1bdf7d7546fe6f19e
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.sandu.ximon.admin.service;
 
import com.alibaba.fastjson.JSON;
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.admin.utils.StoreOperationRecordsUtils;
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());
 
        /**
         * 音柱文件新增 日志记录开始
         */
        String content = "音柱文件名称:" + fileParam.getFileName() + ",文件大小:" + fileParam.getOriginSize() + ",文件路径:" + fileParam.getFileUrl();
        StoreOperationRecordsUtils.storeOperationData(null, null, "音柱文件新增", content);
        /**
         * 音柱文件新增 日志记录结束
         */
        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("服务器删除失败");
//        }
        /**
         * 音柱文件删除 日志记录开始
         */
        String content = "删除文件id:"+fileId+"音柱文件名称:" + byId.getFileName() + ",文件大小:" + byId.getOriginSize() + ",文件路径:" + byId.getFileUrl();
        StoreOperationRecordsUtils.storeOperationData(null, null, "音柱文件删除", content);
        /**
         * 音柱文件删除 日志记录结束
         */
 
        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());
 
        /**
         * 音柱文件上传 日志记录开始
         */
        String content = "文件内容:"+ JSON.toJSONString(fileUploadDto);
        StoreOperationRecordsUtils.storeOperationData(null, null, "音柱文件上传", content);
        /**
         * 音柱文件上传 日志记录结束
         */
 
        return fileUploadDto;
    }
 
}