org.dromara.jpom.controller.outgiving.OutGivingProjectController Maven / Gradle / Ivy
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Code Technology Studio
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.dromara.jpom.controller.outgiving;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.keepbx.jpom.IJsonMessage;
import cn.keepbx.jpom.model.BaseIdModel;
import cn.keepbx.jpom.model.JsonMessage;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.dromara.jpom.JpomApplication;
import org.dromara.jpom.common.BaseServerController;
import org.dromara.jpom.common.ServerConst;
import org.dromara.jpom.common.validator.ValidatorItem;
import org.dromara.jpom.common.validator.ValidatorRule;
import org.dromara.jpom.model.AfterOpt;
import org.dromara.jpom.model.BaseEnum;
import org.dromara.jpom.model.BaseNodeModel;
import org.dromara.jpom.model.data.NodeModel;
import org.dromara.jpom.model.data.ServerWhitelist;
import org.dromara.jpom.model.log.OutGivingLog;
import org.dromara.jpom.model.node.ProjectInfoCacheModel;
import org.dromara.jpom.model.outgiving.BaseNodeProject;
import org.dromara.jpom.model.outgiving.OutGivingModel;
import org.dromara.jpom.model.outgiving.OutGivingNodeProject;
import org.dromara.jpom.outgiving.OutGivingRun;
import org.dromara.jpom.permission.ClassFeature;
import org.dromara.jpom.permission.Feature;
import org.dromara.jpom.permission.MethodFeature;
import org.dromara.jpom.service.node.ProjectInfoCacheService;
import org.dromara.jpom.service.outgiving.DbOutGivingLogService;
import org.dromara.jpom.service.outgiving.OutGivingServer;
import org.dromara.jpom.system.ServerConfig;
import org.dromara.jpom.util.StringUtil;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
/**
* 分发文件管理
*
* @author bwcx_jzy
* @since 2019/4/21
*/
@RestController
@RequestMapping(value = "/outgiving")
@Feature(cls = ClassFeature.OUTGIVING)
@Slf4j
public class OutGivingProjectController extends BaseServerController {
private final OutGivingServer outGivingServer;
private final OutGivingWhitelistService outGivingWhitelistService;
private final ServerConfig serverConfig;
private final DbOutGivingLogService dbOutGivingLogService;
private final ProjectInfoCacheService projectInfoCacheService;
public OutGivingProjectController(OutGivingServer outGivingServer,
OutGivingWhitelistService outGivingWhitelistService,
ServerConfig serverConfig,
DbOutGivingLogService dbOutGivingLogService,
ProjectInfoCacheService projectInfoCacheService) {
this.outGivingServer = outGivingServer;
this.outGivingWhitelistService = outGivingWhitelistService;
this.serverConfig = serverConfig;
this.dbOutGivingLogService = dbOutGivingLogService;
this.projectInfoCacheService = projectInfoCacheService;
}
@RequestMapping(value = "getItemData.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public IJsonMessage getItemData(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "id error") String id,
HttpServletRequest request) {
String workspaceId = outGivingServer.getCheckUserWorkspace(request);
OutGivingModel outGivingServerItem = outGivingServer.getByKey(id, request);
Objects.requireNonNull(outGivingServerItem, "没有数据");
List outGivingNodeProjectList = outGivingServerItem.outGivingNodeProjectList();
//
Set nodeIds = outGivingNodeProjectList.stream().map(BaseNodeProject::getNodeId).collect(Collectors.toSet());
List nodeModels = nodeService.getByKey(nodeIds);
Map nodeMap = CollStreamUtil.toMap(nodeModels, BaseIdModel::getId, nodeModel -> nodeModel);
//
Set projectIds = outGivingNodeProjectList.stream().map(nodeProject -> BaseNodeModel.fullId(workspaceId, nodeProject.getNodeId(), nodeProject.getProjectId())).collect(Collectors.toSet());
List projectInfoCacheModels = projectInfoCacheService.getByKey(projectIds);
Map projectMap = CollStreamUtil.toMap(projectInfoCacheModels, BaseIdModel::getId, data -> data);
List collect = outGivingNodeProjectList
.stream()
.map(outGivingNodeProject -> {
NodeModel nodeModel = nodeMap.get(outGivingNodeProject.getNodeId());
JSONObject jsonObject = new JSONObject();
jsonObject.put("sortValue", outGivingNodeProject.getSortValue());
jsonObject.put("disabled", outGivingNodeProject.getDisabled());
jsonObject.put("nodeId", outGivingNodeProject.getNodeId());
jsonObject.put("projectId", outGivingNodeProject.getProjectId());
jsonObject.put("nodeName", nodeModel.getName());
String fullId = BaseNodeModel.fullId(workspaceId, outGivingNodeProject.getNodeId(), outGivingNodeProject.getProjectId());
jsonObject.put("id", fullId);
ProjectInfoCacheModel projectInfoCacheModel = projectMap.get(fullId);
if (projectInfoCacheModel != null) {
jsonObject.put("cacheProjectName", projectInfoCacheModel.getName());
}
OutGivingLog outGivingLog = dbOutGivingLogService.getByProject(id, outGivingNodeProject);
if (outGivingLog != null) {
jsonObject.put("outGivingStatus", outGivingLog.getStatus());
jsonObject.put("outGivingResult", outGivingLog.getResult());
jsonObject.put("lastTime", outGivingLog.getCreateTimeMillis());
jsonObject.put("fileSize", outGivingLog.getFileSize());
jsonObject.put("progressSize", outGivingLog.getProgressSize());
}
return jsonObject;
})
.collect(Collectors.toList());
JSONObject data = new JSONObject();
data.put("data", outGivingServerItem);
data.put("projectList", collect);
return JsonMessage.success("", data);
}
private File checkZip(File path, boolean unzip) {
if (unzip) {
boolean zip = false;
for (String i : StringUtil.PACKAGE_EXT) {
if (FileUtil.pathEndsWith(path, i)) {
zip = true;
break;
}
}
Assert.state(zip, "不支持的文件类型:" + path.getName());
}
return path;
}
/**
* 节点分发文件
*
* @param id 分发id
* @return json
* @throws IOException IO
*/
@RequestMapping(value = "upload-sharding", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.UPLOAD, log = false)
public IJsonMessage
© 2015 - 2024 Weber Informatics LLC | Privacy Policy