提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.infra.service.file; |
H |
2 |
|
|
3 |
import cn.hutool.core.lang.Assert; |
|
4 |
import cn.hutool.core.util.StrUtil; |
|
5 |
import com.iailab.framework.common.pojo.PageResult; |
|
6 |
import com.iailab.framework.common.util.io.FileUtils; |
|
7 |
import com.iailab.framework.common.util.object.BeanUtils; |
|
8 |
import com.iailab.module.infra.controller.admin.file.vo.file.FileCreateReqVO; |
|
9 |
import com.iailab.module.infra.controller.admin.file.vo.file.FilePageReqVO; |
|
10 |
import com.iailab.module.infra.controller.admin.file.vo.file.FilePresignedUrlRespVO; |
|
11 |
import com.iailab.module.infra.dal.dataobject.file.FileDO; |
|
12 |
import com.iailab.module.infra.dal.mysql.file.FileMapper; |
|
13 |
import com.iailab.module.infra.framework.file.core.client.FileClient; |
|
14 |
import com.iailab.module.infra.framework.file.core.client.s3.FilePresignedUrlRespDTO; |
|
15 |
import com.iailab.module.infra.framework.file.core.utils.FileTypeUtils; |
|
16 |
import lombok.SneakyThrows; |
|
17 |
import org.springframework.stereotype.Service; |
|
18 |
|
|
19 |
import javax.annotation.Resource; |
|
20 |
|
|
21 |
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
22 |
import static com.iailab.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS; |
|
23 |
|
|
24 |
/** |
|
25 |
* 文件 Service 实现类 |
|
26 |
* |
|
27 |
* @author iailab |
|
28 |
*/ |
|
29 |
@Service |
|
30 |
public class FileServiceImpl implements FileService { |
|
31 |
|
|
32 |
@Resource |
|
33 |
private FileConfigService fileConfigService; |
|
34 |
|
|
35 |
@Resource |
|
36 |
private FileMapper fileMapper; |
|
37 |
|
|
38 |
@Override |
|
39 |
public PageResult<FileDO> getFilePage(FilePageReqVO pageReqVO) { |
|
40 |
return fileMapper.selectPage(pageReqVO); |
|
41 |
} |
|
42 |
|
|
43 |
@Override |
|
44 |
@SneakyThrows |
|
45 |
public String createFile(String name, String path, byte[] content) { |
|
46 |
// 计算默认的 path 名 |
|
47 |
String type = FileTypeUtils.getMineType(content, name); |
|
48 |
if (StrUtil.isEmpty(path)) { |
|
49 |
path = FileUtils.generatePath(content, name); |
|
50 |
} |
|
51 |
// 如果 name 为空,则使用 path 填充 |
|
52 |
if (StrUtil.isEmpty(name)) { |
|
53 |
name = path; |
|
54 |
} |
|
55 |
|
|
56 |
// 上传到文件存储器 |
|
57 |
FileClient client = fileConfigService.getMasterFileClient(); |
|
58 |
Assert.notNull(client, "客户端(master) 不能为空"); |
|
59 |
String url = client.upload(content, path, type); |
|
60 |
|
|
61 |
// 保存到数据库 |
|
62 |
FileDO file = new FileDO(); |
|
63 |
file.setConfigId(client.getId()); |
|
64 |
file.setName(name); |
|
65 |
file.setPath(path); |
|
66 |
file.setUrl(url); |
|
67 |
file.setType(type); |
|
68 |
file.setSize(content.length); |
|
69 |
fileMapper.insert(file); |
|
70 |
return url; |
|
71 |
} |
|
72 |
|
|
73 |
@Override |
|
74 |
public Long createFile(FileCreateReqVO createReqVO) { |
|
75 |
FileDO file = BeanUtils.toBean(createReqVO, FileDO.class); |
|
76 |
fileMapper.insert(file); |
|
77 |
return file.getId(); |
|
78 |
} |
|
79 |
|
|
80 |
@Override |
|
81 |
public void deleteFile(Long id) throws Exception { |
|
82 |
// 校验存在 |
|
83 |
FileDO file = validateFileExists(id); |
|
84 |
|
|
85 |
// 从文件存储器中删除 |
|
86 |
FileClient client = fileConfigService.getFileClient(file.getConfigId()); |
|
87 |
Assert.notNull(client, "客户端({}) 不能为空", file.getConfigId()); |
|
88 |
client.delete(file.getPath()); |
|
89 |
|
|
90 |
// 删除记录 |
|
91 |
fileMapper.deleteById(id); |
|
92 |
} |
|
93 |
|
|
94 |
private FileDO validateFileExists(Long id) { |
|
95 |
FileDO fileDO = fileMapper.selectById(id); |
|
96 |
if (fileDO == null) { |
|
97 |
throw exception(FILE_NOT_EXISTS); |
|
98 |
} |
|
99 |
return fileDO; |
|
100 |
} |
|
101 |
|
|
102 |
@Override |
|
103 |
public byte[] getFileContent(Long configId, String path) throws Exception { |
|
104 |
FileClient client = fileConfigService.getFileClient(configId); |
|
105 |
Assert.notNull(client, "客户端({}) 不能为空", configId); |
|
106 |
return client.getContent(path); |
|
107 |
} |
|
108 |
|
|
109 |
@Override |
|
110 |
public FilePresignedUrlRespVO getFilePresignedUrl(String path) throws Exception { |
|
111 |
FileClient fileClient = fileConfigService.getMasterFileClient(); |
|
112 |
FilePresignedUrlRespDTO presignedObjectUrl = fileClient.getPresignedObjectUrl(path); |
|
113 |
return BeanUtils.toBean(presignedObjectUrl, FilePresignedUrlRespVO.class, |
|
114 |
object -> object.setConfigId(fileClient.getId())); |
|
115 |
} |
|
116 |
|
|
117 |
} |