提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.infra.framework.file.core.client.db; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import cn.hutool.extra.spring.SpringUtil; |
|
5 |
import com.iailab.module.infra.dal.dataobject.file.FileContentDO; |
|
6 |
import com.iailab.module.infra.dal.mysql.file.FileContentMapper; |
|
7 |
import com.iailab.module.infra.framework.file.core.client.AbstractFileClient; |
|
8 |
|
|
9 |
import java.util.Comparator; |
|
10 |
import java.util.List; |
|
11 |
|
|
12 |
/** |
|
13 |
* 基于 DB 存储的文件客户端的配置类 |
|
14 |
* |
|
15 |
* @author iailab |
|
16 |
*/ |
|
17 |
public class DBFileClient extends AbstractFileClient<DBFileClientConfig> { |
|
18 |
|
|
19 |
private FileContentMapper fileContentMapper; |
|
20 |
|
|
21 |
public DBFileClient(Long id, DBFileClientConfig config) { |
|
22 |
super(id, config); |
|
23 |
} |
|
24 |
|
|
25 |
@Override |
|
26 |
protected void doInit() { |
|
27 |
fileContentMapper = SpringUtil.getBean(FileContentMapper.class); |
|
28 |
} |
|
29 |
|
|
30 |
@Override |
|
31 |
public String upload(byte[] content, String path, String type) { |
|
32 |
FileContentDO contentDO = new FileContentDO().setConfigId(getId()) |
|
33 |
.setPath(path).setContent(content); |
|
34 |
fileContentMapper.insert(contentDO); |
|
35 |
// 拼接返回路径 |
|
36 |
return super.formatFileUrl(config.getDomain(), path); |
|
37 |
} |
|
38 |
|
|
39 |
@Override |
|
40 |
public void delete(String path) { |
|
41 |
fileContentMapper.deleteByConfigIdAndPath(getId(), path); |
|
42 |
} |
|
43 |
|
|
44 |
@Override |
|
45 |
public byte[] getContent(String path) { |
|
46 |
List<FileContentDO> list = fileContentMapper.selectListByConfigIdAndPath(getId(), path); |
|
47 |
if (CollUtil.isEmpty(list)) { |
|
48 |
return null; |
|
49 |
} |
|
50 |
// 排序后,拿 id 最大的,即最后上传的 |
|
51 |
list.sort(Comparator.comparing(FileContentDO::getId)); |
|
52 |
return CollUtil.getLast(list).getContent(); |
|
53 |
} |
|
54 |
|
|
55 |
} |