提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.sys.modular.rest.service; |
懒 |
2 |
|
|
3 |
import cn.hutool.core.codec.Base64; |
|
4 |
import cn.hutool.core.io.IoUtil; |
|
5 |
import cn.stylefeng.guns.base.auth.context.LoginContextHolder; |
|
6 |
import cn.stylefeng.guns.base.auth.model.LoginUser; |
|
7 |
import cn.stylefeng.guns.base.consts.ConstantsContext; |
|
8 |
import cn.stylefeng.guns.sys.core.constant.DefaultAvatar; |
|
9 |
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum; |
|
10 |
import cn.stylefeng.guns.sys.modular.rest.entity.RestFileInfo; |
|
11 |
import cn.stylefeng.guns.sys.modular.rest.entity.RestUser; |
|
12 |
import cn.stylefeng.guns.sys.modular.rest.mapper.RestFileInfoMapper; |
|
13 |
import cn.stylefeng.roses.core.util.ToolUtil; |
|
14 |
import cn.stylefeng.roses.kernel.model.exception.ServiceException; |
|
15 |
import cn.stylefeng.roses.kernel.model.exception.enums.CoreExceptionEnum; |
|
16 |
import com.baomidou.mybatisplus.core.toolkit.IdWorker; |
|
17 |
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
18 |
import lombok.extern.slf4j.Slf4j; |
|
19 |
import org.springframework.stereotype.Service; |
|
20 |
import org.springframework.transaction.annotation.Transactional; |
|
21 |
import org.springframework.web.multipart.MultipartFile; |
|
22 |
|
|
23 |
import javax.annotation.Resource; |
|
24 |
import java.io.File; |
|
25 |
import java.io.FileInputStream; |
|
26 |
import java.io.FileNotFoundException; |
|
27 |
import java.math.BigDecimal; |
|
28 |
|
|
29 |
/** |
|
30 |
* <p> |
|
31 |
* 文件信息表 |
|
32 |
* 服务实现类 |
|
33 |
* </p> |
|
34 |
* |
|
35 |
* @author stylefeng |
|
36 |
* @since 2018-12-07 |
|
37 |
*/ |
|
38 |
@Service |
|
39 |
@Slf4j |
|
40 |
public class RestFileInfoService extends ServiceImpl<RestFileInfoMapper, RestFileInfo> { |
|
41 |
|
|
42 |
@Resource |
|
43 |
private RestUserService restUserService; |
|
44 |
|
|
45 |
/** |
|
46 |
* 更新头像 |
|
47 |
* |
|
48 |
* @author fengshuonan |
|
49 |
* @Date 2018/11/10 4:10 PM |
|
50 |
*/ |
|
51 |
@Transactional(rollbackFor = Exception.class) |
|
52 |
public void updateAvatar(String fileId) { |
|
53 |
LoginUser currentUser = LoginContextHolder.getContext().getUser(); |
|
54 |
if (currentUser == null) { |
|
55 |
throw new ServiceException(CoreExceptionEnum.NO_CURRENT_USER); |
|
56 |
} |
|
57 |
|
|
58 |
RestUser user = restUserService.getById(currentUser.getId()); |
|
59 |
|
|
60 |
//更新用户的头像 |
|
61 |
user.setAvatar(fileId); |
|
62 |
restUserService.updateById(user); |
|
63 |
} |
|
64 |
|
|
65 |
/** |
|
66 |
* 预览当前用户头像 |
|
67 |
* |
|
68 |
* @author fengshuonan |
|
69 |
* @Date 2019-05-04 17:04 |
|
70 |
*/ |
|
71 |
public byte[] previewAvatar() { |
|
72 |
|
|
73 |
//获取当前用户 |
|
74 |
LoginUser currentUser = LoginContextHolder.getContext().getUser(); |
|
75 |
if (currentUser == null) { |
|
76 |
throw new ServiceException(CoreExceptionEnum.NO_CURRENT_USER); |
|
77 |
} |
|
78 |
|
|
79 |
//获取当前用户的头像id |
|
80 |
RestUser user = restUserService.getById(currentUser.getId()); |
|
81 |
String avatarFileId = user.getAvatar(); |
|
82 |
|
|
83 |
try { |
|
84 |
//返回头像id的字节流 |
|
85 |
return getFileBytes(avatarFileId); |
|
86 |
} catch (Exception e) { |
|
87 |
|
|
88 |
//返回默认头像 |
|
89 |
return Base64.decode(DefaultAvatar.BASE_64_AVATAR); |
|
90 |
} |
|
91 |
} |
|
92 |
|
|
93 |
/** |
|
94 |
* 获取文件流 |
|
95 |
* |
|
96 |
* @author fengshuonan |
|
97 |
* @Date 2019-05-04 17:04 |
|
98 |
*/ |
|
99 |
public byte[] getFileBytes(String fileId) { |
|
100 |
|
|
101 |
if (ToolUtil.isEmpty(fileId)) { |
|
102 |
throw new ServiceException(BizExceptionEnum.FILE_NOT_FOUND); |
|
103 |
} |
|
104 |
|
|
105 |
RestFileInfo fileInfo = this.getById(fileId); |
|
106 |
if (fileInfo == null) { |
|
107 |
throw new ServiceException(BizExceptionEnum.FILE_NOT_FOUND); |
|
108 |
} else { |
|
109 |
try { |
|
110 |
String filePath = fileInfo.getFilePath(); |
|
111 |
return IoUtil.readBytes(new FileInputStream(filePath)); |
|
112 |
} catch (FileNotFoundException e) { |
|
113 |
log.error("文件未找到,id为:" + fileId, e); |
|
114 |
throw new ServiceException(BizExceptionEnum.FILE_NOT_FOUND); |
|
115 |
} |
|
116 |
} |
|
117 |
} |
|
118 |
|
|
119 |
/** |
|
120 |
* 上传文件 |
|
121 |
* |
|
122 |
* @author fengshuonan |
|
123 |
* @Date 2019-05-04 17:18 |
|
124 |
*/ |
|
125 |
public String uploadFile(MultipartFile file) { |
|
126 |
|
|
127 |
//生成文件的唯一id |
|
128 |
String fileId = IdWorker.getIdStr(); |
|
129 |
|
|
130 |
//获取文件后缀 |
|
131 |
String fileSuffix = ToolUtil.getFileSuffix(file.getOriginalFilename()); |
|
132 |
|
|
133 |
//获取文件原始名称 |
|
134 |
String originalFilename = file.getOriginalFilename(); |
|
135 |
|
|
136 |
//生成文件的最终名称 |
|
137 |
String finalName = fileId + "." + ToolUtil.getFileSuffix(originalFilename); |
|
138 |
|
|
139 |
try { |
|
140 |
//保存文件到指定目录 |
|
141 |
String fileSavePath = ConstantsContext.getFileUploadPath(); |
|
142 |
File newFile = new File(fileSavePath + finalName); |
|
143 |
file.transferTo(newFile); |
|
144 |
|
|
145 |
//保存文件信息 |
|
146 |
RestFileInfo fileInfo = new RestFileInfo(); |
|
147 |
fileInfo.setFileId(fileId); |
|
148 |
fileInfo.setFileName(originalFilename); |
|
149 |
fileInfo.setFileSuffix(fileSuffix); |
|
150 |
fileInfo.setFilePath(fileSavePath + finalName); |
|
151 |
fileInfo.setFinalName(finalName); |
|
152 |
|
|
153 |
//计算文件大小kb |
|
154 |
long kb = new BigDecimal(file.getSize()) |
|
155 |
.divide(BigDecimal.valueOf(1024)) |
|
156 |
.setScale(0, BigDecimal.ROUND_HALF_UP).longValue(); |
|
157 |
fileInfo.setFileSizeKb(kb); |
|
158 |
this.save(fileInfo); |
|
159 |
} catch (Exception e) { |
|
160 |
log.error("上传文件错误!", e); |
|
161 |
throw new ServiceException(BizExceptionEnum.UPLOAD_ERROR); |
|
162 |
} |
|
163 |
|
|
164 |
return fileId; |
|
165 |
|
|
166 |
} |
|
167 |
} |