懒羊羊
2023-08-30 1ac2bc1590406d9babec036e154d8d08f34a6aa1
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
163
164
165
166
167
package cn.stylefeng.guns.sys.modular.rest.service;
 
import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.IoUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.consts.ConstantsContext;
import cn.stylefeng.guns.sys.core.constant.DefaultAvatar;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.modular.rest.entity.RestFileInfo;
import cn.stylefeng.guns.sys.modular.rest.entity.RestUser;
import cn.stylefeng.guns.sys.modular.rest.mapper.RestFileInfoMapper;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import cn.stylefeng.roses.kernel.model.exception.enums.CoreExceptionEnum;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
 
/**
 * <p>
 * 文件信息表
 * 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2018-12-07
 */
@Service
@Slf4j
public class RestFileInfoService extends ServiceImpl<RestFileInfoMapper, RestFileInfo> {
 
    @Resource
    private RestUserService restUserService;
 
    /**
     * 更新头像
     *
     * @author fengshuonan
     * @Date 2018/11/10 4:10 PM
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateAvatar(String fileId) {
        LoginUser currentUser = LoginContextHolder.getContext().getUser();
        if (currentUser == null) {
            throw new ServiceException(CoreExceptionEnum.NO_CURRENT_USER);
        }
 
        RestUser user = restUserService.getById(currentUser.getId());
 
        //更新用户的头像
        user.setAvatar(fileId);
        restUserService.updateById(user);
    }
 
    /**
     * 预览当前用户头像
     *
     * @author fengshuonan
     * @Date 2019-05-04 17:04
     */
    public byte[] previewAvatar() {
 
        //获取当前用户
        LoginUser currentUser = LoginContextHolder.getContext().getUser();
        if (currentUser == null) {
            throw new ServiceException(CoreExceptionEnum.NO_CURRENT_USER);
        }
 
        //获取当前用户的头像id
        RestUser user = restUserService.getById(currentUser.getId());
        String avatarFileId = user.getAvatar();
 
        try {
            //返回头像id的字节流
            return getFileBytes(avatarFileId);
        } catch (Exception e) {
 
            //返回默认头像
            return Base64.decode(DefaultAvatar.BASE_64_AVATAR);
        }
    }
 
    /**
     * 获取文件流
     *
     * @author fengshuonan
     * @Date 2019-05-04 17:04
     */
    public byte[] getFileBytes(String fileId) {
 
        if (ToolUtil.isEmpty(fileId)) {
            throw new ServiceException(BizExceptionEnum.FILE_NOT_FOUND);
        }
 
        RestFileInfo fileInfo = this.getById(fileId);
        if (fileInfo == null) {
            throw new ServiceException(BizExceptionEnum.FILE_NOT_FOUND);
        } else {
            try {
                String filePath = fileInfo.getFilePath();
                return IoUtil.readBytes(new FileInputStream(filePath));
            } catch (FileNotFoundException e) {
                log.error("文件未找到,id为:" + fileId, e);
                throw new ServiceException(BizExceptionEnum.FILE_NOT_FOUND);
            }
        }
    }
 
    /**
     * 上传文件
     *
     * @author fengshuonan
     * @Date 2019-05-04 17:18
     */
    public String uploadFile(MultipartFile file) {
 
        //生成文件的唯一id
        String fileId = IdWorker.getIdStr();
 
        //获取文件后缀
        String fileSuffix = ToolUtil.getFileSuffix(file.getOriginalFilename());
 
        //获取文件原始名称
        String originalFilename = file.getOriginalFilename();
 
        //生成文件的最终名称
        String finalName = fileId + "." + ToolUtil.getFileSuffix(originalFilename);
 
        try {
            //保存文件到指定目录
            String fileSavePath = ConstantsContext.getFileUploadPath();
            File newFile = new File(fileSavePath + finalName);
            file.transferTo(newFile);
 
            //保存文件信息
            RestFileInfo fileInfo = new RestFileInfo();
            fileInfo.setFileId(fileId);
            fileInfo.setFileName(originalFilename);
            fileInfo.setFileSuffix(fileSuffix);
            fileInfo.setFilePath(fileSavePath + finalName);
            fileInfo.setFinalName(finalName);
 
            //计算文件大小kb
            long kb = new BigDecimal(file.getSize())
                    .divide(BigDecimal.valueOf(1024))
                    .setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
            fileInfo.setFileSizeKb(kb);
            this.save(fileInfo);
        } catch (Exception e) {
            log.error("上传文件错误!", e);
            throw new ServiceException(BizExceptionEnum.UPLOAD_ERROR);
        }
 
        return fileId;
 
    }
}