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