UpgradeServiceImpl.java
3.93 KB
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
package vion.service.impl.monitor;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.hutool.core.io.ManifestUtil;
import org.dromara.hutool.core.io.file.FileTypeUtil;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.crypto.SecureUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import vion.mapper.monitor.UpgradeMapper;
import vion.model.monitor.Upgrade;
import vion.service.monitor.IUpgradeService;
import java.io.File;
import java.io.IOException;
import java.util.jar.Attributes;
/**
* @author vion
* @date 2024/10/25
*/
@Service
public class UpgradeServiceImpl extends ServiceImpl<UpgradeMapper, Upgrade> implements IUpgradeService {
@Value("${fileUrl:}")
private String fileUrl;
@Override
public String saveAndFile(Upgrade upgrade) {
try {
var infile = upgrade.getInfile();
Assert.notNull(infile, "升级包不能为空");
var type = FileTypeUtil.getType(infile.getInputStream());
Assert.notEquals(type, "jar", "上传的升级包类型不是jar");
// todo 测试文件保存
String path = fileUrl + FileUtil.FILE_SEPARATOR
+ "agent" + FileUtil.FILE_SEPARATOR
+ upgrade.getVersion() + FileUtil.FILE_SEPARATOR
+ infile.getOriginalFilename();
File file = FileUtil.touch(path);
infile.transferTo(file);
upgrade.setSha256(SecureUtil.sha256(file));
upgrade.setUrl(path);
var manifest = ManifestUtil.getManifest(file);
var version = manifest.getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION);
Assert.equals(version, upgrade.getVersion(), "升级包版本号不一致");
var name = manifest.getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_TITLE);
Assert.equals(name, upgrade.getName(), "升级包名称不一致");
upgrade.setVersion(version);
// 打包时间为 utc 时间
var buildTime = manifest.getMainAttributes().getValue("Build-Timestamp");
upgrade.setBuildTime(buildTime);
this.save(upgrade);
return "升级包添加成功";
} catch (IOException e) {
log.error("升级包保存出错");
}
return "升级包添加失败";
}
@Override
public String update(Upgrade upgrade) {
Opt.ofNullable(upgrade.getInfile())
.ifPresent(infile -> {
String path = fileUrl + FileUtil.FILE_SEPARATOR
+ "agent" + FileUtil.FILE_SEPARATOR
+ upgrade.getVersion() + FileUtil.FILE_SEPARATOR
+ infile.getOriginalFilename();
File file = FileUtil.touch(path);
infile.transferTo(file);
upgrade.setSha256(SecureUtil.sha256(file));
upgrade.setUrl(path);
var manifest = ManifestUtil.getManifest(file);
var version = manifest.getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION);
Assert.equals(version, upgrade.getVersion(), "版本号不一致");
upgrade.setVersion(version);
// 打包时间为 utc 时间
var buildTime = manifest.getMainAttributes().getValue("Build-Timestamp");
upgrade.setBuildTime(buildTime);
});
return this.updateById(upgrade) ? "升级包更新成功" : "升级包更新失败";
}
@Override
public String remove(Long id) {
var url = this.getById(id).getUrl();
FileUtil.del(url);
return this.removeById(id) ? "升级包删除成功" : "升级包删除失败";
}
}