Commit e2123512 authored by coderTomato's avatar coderTomato

上传zip创建工程

parent 58042f38
package com.dlink.controller;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import com.dlink.common.result.ProTableResult;
import com.dlink.common.result.Result;
import com.dlink.dto.CatalogueTaskDTO;
......@@ -8,15 +10,13 @@ import com.dlink.service.CatalogueService;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* CatalogueController
......@@ -31,6 +31,109 @@ public class CatalogueController {
@Autowired
private CatalogueService catalogueService;
@Value("${dlink.uploadpath}")
private String uploadpath;
@PostMapping("/upload/{id}")
public Result<String> upload(MultipartFile file,@PathVariable Integer id) {
//获取上传的路径
String filePath = uploadpath;
//获取源文件的名称
String fileName = file.getOriginalFilename();
String zipPath = filePath+fileName;
String unzipFileName = fileName.substring(0,fileName.lastIndexOf("."));
String unzipPath = filePath+unzipFileName;
File unzipFile = new File(unzipPath);
File zipFile = new File(zipPath);
if(unzipFile.exists()){
FileUtil.del(zipFile);
return Result.failed("工程已存在");
}
try {
//文件写入上传的路径
FileUtil.writeBytes(file.getBytes(), zipPath);
Thread.sleep(1L);
if(!unzipFile.exists()){
ZipUtil.unzip(zipPath,filePath);
Catalogue cata = getCatalogue(id, unzipFileName);
traverseFile(unzipPath,cata);
}
} catch (Exception e) {
return Result.failed(e.getMessage());
}finally {
FileUtil.del(zipFile);
}
return Result.succeed("上传zip包并创建工程成功");
}
private void traverseFile(String sourcePath, Catalogue catalog) throws Exception{
File file = new File(sourcePath);
File[] fs = file.listFiles();
if(fs == null){
throw new RuntimeException("目录层级有误");
}
for (File fl : fs) {
if(fl.isFile()){
System.out.println(fl.getName());
CatalogueTaskDTO dto = getCatalogueTaskDTO(fl.getName(), catalogueService.findByParentIdAndName(catalog.getParentId(), catalog.getName()).getId());
String fileText = getFileText(fl);
catalogueService.createCatalogAndFileTask(dto,fileText);
}else{
Catalogue newCata = getCatalogue(catalogueService.findByParentIdAndName(catalog.getParentId(), catalog.getName()).getId(), fl.getName());
traverseFile(fl.getPath(),newCata);
}
}
}
private String getFileText(File sourceFile){
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try{
if(sourceFile.isFile() && sourceFile.exists()){
InputStreamReader isr = new InputStreamReader(new FileInputStream(sourceFile));
br = new BufferedReader(isr);
String lineText = null;
while ((lineText = br.readLine()) != null){
sb.append(lineText).append("\n");
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
private Catalogue getCatalogue(Integer parentId, String name){
Catalogue subcata = new Catalogue();
subcata.setTaskId(null);
subcata.setName(name);
subcata.setType("null");
subcata.setParentId(parentId);
subcata.setIsLeaf(false);
catalogueService.saveOrUpdate(subcata);
return subcata;
}
private CatalogueTaskDTO getCatalogueTaskDTO(String alias, Integer parentId){
CatalogueTaskDTO catalogueTaskDTO = new CatalogueTaskDTO();
catalogueTaskDTO.setName(UUID.randomUUID().toString().substring(0,6)+alias);
catalogueTaskDTO.setAlias(alias);
catalogueTaskDTO.setId(null);
catalogueTaskDTO.setParentId(parentId);
catalogueTaskDTO.setLeaf(true);
//catalogueTaskDTO.setDialect("FlinkSql");
return catalogueTaskDTO;
}
/**
* 新增或者更新
*/
......
......@@ -3,6 +3,7 @@ package com.dlink.mapper;
import com.dlink.db.mapper.SuperMapper;
import com.dlink.model.Catalogue;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* CatalogueMapper
......@@ -12,4 +13,5 @@ import org.apache.ibatis.annotations.Mapper;
**/
@Mapper
public interface CatalogueMapper extends SuperMapper<Catalogue> {
Catalogue findByParentIdAndName(@Param("parent_id")Integer parent_id, @Param("name")String name);
}
......@@ -3,6 +3,7 @@ package com.dlink.service;
import com.dlink.db.service.ISuperService;
import com.dlink.dto.CatalogueTaskDTO;
import com.dlink.model.Catalogue;
import org.apache.ibatis.annotations.Param;
import java.util.List;
......@@ -15,9 +16,9 @@ import java.util.List;
public interface CatalogueService extends ISuperService<Catalogue> {
List<Catalogue> getAllData();
Catalogue findByParentIdAndName(Integer parent_id, String name);
Catalogue createCatalogueAndTask(CatalogueTaskDTO catalogueTaskDTO);
Catalogue createCatalogAndFileTask(CatalogueTaskDTO catalogueTaskDTO, String ment);
boolean toRename(Catalogue catalogue);
boolean removeCatalogueAndTaskById(Integer id);
......
......@@ -10,6 +10,7 @@ import com.dlink.model.Task;
import com.dlink.service.CatalogueService;
import com.dlink.service.StatementService;
import com.dlink.service.TaskService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -30,11 +31,19 @@ public class CatalogueServiceImpl extends SuperServiceImpl<CatalogueMapper, Cata
@Autowired
private StatementService statementService;
@Autowired
private CatalogueMapper catalogueMapper;
@Override
public List<Catalogue> getAllData() {
return this.list();
}
@Override
public Catalogue findByParentIdAndName(Integer parent_id, String name) {
return catalogueMapper.findByParentIdAndName(parent_id, name);
}
@Transactional(rollbackFor=Exception.class)
@Override
public Catalogue createCatalogueAndTask(CatalogueTaskDTO catalogueTaskDTO) {
......@@ -53,6 +62,25 @@ public class CatalogueServiceImpl extends SuperServiceImpl<CatalogueMapper, Cata
return catalogue;
}
@Override
public Catalogue createCatalogAndFileTask(CatalogueTaskDTO catalogueTaskDTO, String ment) {
Task task = new Task();
task.setName(catalogueTaskDTO.getName());
task.setAlias(catalogueTaskDTO.getAlias());
task.setDialect(catalogueTaskDTO.getDialect());
task.setStatement(ment);
task.setEnabled(true);
taskService.saveOrUpdateTask(task);
Catalogue catalogue = new Catalogue();
catalogue.setName(catalogueTaskDTO.getAlias());
catalogue.setIsLeaf(true);
catalogue.setTaskId(task.getId());
catalogue.setType(catalogueTaskDTO.getDialect());
catalogue.setParentId(catalogueTaskDTO.getParentId());
this.save(catalogue);
return catalogue;
}
@Transactional(rollbackFor=Exception.class)
@Override
public boolean toRename(Catalogue catalogue) {
......
......@@ -20,6 +20,11 @@
id, name, task_id, type,parent_id,is_leaf, enabled, create_time, update_time
</sql>
<select id="findByParentIdAndName" resultType="com.dlink.model.Catalogue">
select a.*
from dlink_catalogue a
where parent_id=#{parent_id} and name=#{name}
</select>
<select id="selectForProTable" resultType="com.dlink.model.Catalogue">
select
......
......@@ -11,6 +11,8 @@
Date: 24/11/2021 09:19:12
*/
create database if not exists dlink;
use dlink;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
......
import React from 'react';
import {Button, message,Modal,Upload} from "antd";
import {UploadOutlined} from "@ant-design/icons";
interface UploadModalProps {
visible: boolean;
action: string;
handleOk: ()=>void;
onCancel: ()=>void;
buttonTitle: string;
}
const UploadModal:React.FC<UploadModalProps> = (props:any) => {
const {visible,handleOk,onCancel,action,buttonTitle} = props;
const handlers = {
name: 'file',
action: action,
maxCount: 1,
multiple: true,
onChange(info:any) {
if (info.file.status !== 'uploading') {
console.log(info.file, info.fileList);
}
if (info.file.status === 'done') {
console.log('info:',info);
if(info.file.response.code == "-1"){
message.error(`${info.file.response.msg} `);
}else{
message.success(`${info.file.name} file uploaded successfully`);
}
} else if (info.file.status === 'error') {
message.error(`${info.file.name} file upload failed.`);
}
},
};
return (
<div>
<Modal title="上传文件"
visible={visible}
onOk={handleOk}
onCancel={onCancel}
maskClosable={false}
>
<Upload {...handlers}>
<Button size="small" icon={<UploadOutlined />}>{buttonTitle}</Button>
</Upload>
</Modal>
</div>
);
};
export default UploadModal;
......@@ -7,16 +7,14 @@ import {convertToTreeData, getTreeNodeByKey, TreeDataNode} from "@/components/St
import style from "./index.less";
import {StateType} from "@/pages/FlinkSqlStudio/model";
import {
getInfoById, handleAddOrUpdate, handleAddOrUpdateWithResult, handleRemoveById,
handleSubmit
getInfoById, handleAddOrUpdate, handleAddOrUpdateWithResult, handleRemoveById, handleSubmit
} from "@/components/Common/crud";
import UpdateCatalogueForm from './components/UpdateCatalogueForm';
import SimpleTaskForm from "@/components/Studio/StudioTree/components/SimpleTaskForm";
import { Scrollbars } from "react-custom-scrollbars";
import {getIcon} from "@/components/Studio/icon";
import {showEnv} from "@/components/Studio/StudioEvent/DDL";
const { DirectoryTree } = Tree;
import UploadModal from "@/components/Studio/StudioTree/components/UploadModal";
type StudioTreeProps = {
......@@ -47,13 +45,21 @@ const StudioTree: React.FC<StudioTreeProps> = (props) => {
const [taskFormValues, setTaskFormValues] = useState({});
const [rightClickNode, setRightClickNode] = useState<TreeDataNode>();
const [available, setAvailable] = useState<boolean>(true);
const [isUploadModalVisible, setIsUploadModalVisible] = useState(false);
const [dataList, setDataList] = useState<any[]>([]);
const [uploadNodeId, setUploadNodeId] = useState(0);
const sref: any = React.createRef<Scrollbars>();
const { DirectoryTree } = Tree;
const getTreeData = async () => {
const result = await getCatalogueTreeData();
let data = result.datas;
let list = data;
let expandList = new Array();
for(let i=0;i<list.length;i++){
if(list[i].parentId == 0|| list[i].parentId == 37){
expandList.push(list[i].id)
}
list[i].title=list[i].name;
list[i].key=list[i].id;
if(list[i].isLeaf){
......@@ -62,6 +68,7 @@ const StudioTree: React.FC<StudioTreeProps> = (props) => {
}
data = convertToTreeData(list, 0);
setTreeData(data);
setDataList(expandList);
};
const openByKey = async (key:any)=>{
......@@ -95,6 +102,8 @@ const StudioTree: React.FC<StudioTreeProps> = (props) => {
createCatalogue(rightClickNode);
}else if(key=='CreateRootCatalogue'){
createRootCatalogue();
} else if(key == 'ShowUploadModal'){
showUploadModal(rightClickNode);
}else if(key=='CreateTask'){
createTask(rightClickNode);
}else if(key=='Rename'){
......@@ -104,6 +113,12 @@ const StudioTree: React.FC<StudioTreeProps> = (props) => {
}
};
const showUploadModal=(node:TreeDataNode|undefined)=>{
if(node == undefined) return;
setUploadNodeId(node.id);
setIsUploadModalVisible(true);
}
const toOpen=(node:TreeDataNode|undefined)=>{
if(!available){return}
setAvailable(false);
......@@ -262,6 +277,7 @@ const StudioTree: React.FC<StudioTreeProps> = (props) => {
menuItems=(<>
<Menu.Item key='CreateCatalogue'>{'创建目录'}</Menu.Item>
<Menu.Item key='CreateRootCatalogue'>{'创建根目录'}</Menu.Item>
<Menu.Item key='ShowUploadModal'>{'上传zip包创建工程'}</Menu.Item>
<Menu.Item key='CreateTask'>{'创建作业'}</Menu.Item>
<Menu.Item key='Rename'>{'重命名'}</Menu.Item>
<Menu.Item disabled>{'删除'}</Menu.Item>
......@@ -410,6 +426,11 @@ const StudioTree: React.FC<StudioTreeProps> = (props) => {
/>
) : null}
</Scrollbars>
<UploadModal visible={isUploadModalVisible} action={`/api/catalogue/upload/${uploadNodeId}`} handleOk={()=>{
setIsUploadModalVisible(false);
setExpandedKeys(dataList);
getTreeData();
}} onCancel={()=>{setIsUploadModalVisible(false)}} buttonTitle="上传zip包并创建工程" />
</div>
);
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment