UpgradeServiceImpl.java 3.93 KB
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(version, 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) ? "升级包删除成功" : "升级包删除失败";
    }
}