This commit is contained in:
s2042968 2025-02-12 15:22:01 +08:00
commit 5d773f7ef3
19 changed files with 398 additions and 26 deletions

View File

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.electromagnetic.data</groupId>
<artifactId>electromagnetic-data-new</artifactId>
<version>1.0</version>
</parent>
<artifactId>electrmangnetic-backup</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.22</version>
</dependency>
<dependency>
<groupId>com.electromagnetic.data</groupId>
<artifactId>electromagnetic-common</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>electromagnetic-backup</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.12</version>
<executions>
<execution>
<configuration>
<mainClass>com.electromagnetic.industry.software.backup.MainApp</mainClass>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,11 @@
package com.electromagnetic.industry.software.backup;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}

View File

@ -0,0 +1,53 @@
package com.electromagnetic.industry.software.backup.controller;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.electromagnetic.industry.software.backup.pojo.BackupPro;
import com.electromagnetic.industry.software.common.pojo.BackupFileResLog;
import com.electromagnetic.industry.software.backup.service.FileService;
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.Date;
@RestController
@RequestMapping("/data/file/backup")
@Slf4j
public class FileController {
@Resource
private FileService fileService;
@Resource
private BackupPro backupPro;
@RequestMapping("/upload")
public ElectromagneticResult<?> upload(@RequestParam("file") MultipartFile file, @RequestParam("path") String path) {
BackupFileResLog backupFileResLog = BackupFileResLog.builder().backupStartTime(new Date()).fileName(file.getOriginalFilename()).backupStatus(true).build();
try {
fileService.upload(file, path);
} catch (Exception e) {
String details = ExceptionUtil.stacktraceToString(e);
backupFileResLog.setBackupStatus(false);
backupFileResLog.setFailInfoDetail(details);
log.error("备份文件失败,原因--->{}", e.getMessage(), e);
}
JSONConfig jsonConfig = JSONConfig.create();
jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
backupFileResLog.setBackupEndTime(new Date());
StringBuffer info = new StringBuffer()
.append("\n")
.append("#")
.append("\n")
.append(JSONUtil.toJsonStr(backupFileResLog, jsonConfig));
FileUtil.appendUtf8String(info.toString(), backupPro.getLogPath());
return ElectromagneticResultUtil.success(JSONUtil.toJsonStr(backupFileResLog, jsonConfig));
}
}

View File

@ -0,0 +1,25 @@
package com.electromagnetic.industry.software.backup.pojo;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Setter
@Component
@ConfigurationProperties(prefix = "ele.backup")
public class BackupPro {
private String saveFolder;
private String logPath;
private String winPrefix;
public String getSaveFolder() {
return System.getProperty("os.name").toLowerCase().startsWith("win") ? winPrefix + saveFolder : saveFolder;
}
public String getLogPath() {
return System.getProperty("os.name").toLowerCase().startsWith("win") ? winPrefix + logPath : logPath;
}
}

View File

@ -0,0 +1,11 @@
package com.electromagnetic.industry.software.backup.service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
public interface FileService {
void upload(MultipartFile file, String path) throws IOException;
}

View File

@ -0,0 +1,28 @@
package com.electromagnetic.industry.software.backup.serviceimp;
import cn.hutool.core.io.FileUtil;
import com.electromagnetic.industry.software.backup.pojo.BackupPro;
import com.electromagnetic.industry.software.backup.service.FileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
@Service
public class FileServiceImpl implements FileService {
@Resource
private BackupPro backupPro;
@Override
public void upload(MultipartFile file, String path) throws IOException {
String saveFolder = backupPro.getSaveFolder();
String fileName = file.getOriginalFilename();
String destPath = saveFolder + File.separator + path + File.separator + fileName;
if (!FileUtil.exist(destPath)) {
FileUtil.writeFromStream(file.getInputStream(), destPath);
}
}
}

View File

@ -0,0 +1,12 @@
ele:
backup:
saveFolder: "/szsd/ele/data/backup/"
logPath: "/szsd/ele/data/backup.log"
winPrefix: "D:/tmp"
spring:
servlet:
multipart:
max-file-size: 500MB
max-request-size: 500MB

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>com.electromagnetic.data</groupId> <groupId>com.electromagnetic.data</groupId>
<artifactId>electromagnetic-data-new</artifactId> <artifactId>electromagnetic-data-new</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0</version>
</parent> </parent>
<artifactId>electrmangnetic</artifactId> <artifactId>electrmangnetic</artifactId>
@ -61,7 +61,7 @@
<dependency> <dependency>
<groupId>com.electromagnetic.data</groupId> <groupId>com.electromagnetic.data</groupId>
<artifactId>electromagnetic-common</artifactId> <artifactId>electromagnetic-common</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -8,7 +8,6 @@ import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
@Aspect @Aspect
public class RoleCheckAspect { public class RoleCheckAspect {

View File

@ -0,0 +1,9 @@
package com.electromagnetic.industry.software.manage.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.electromagnetic.industry.software.manage.pojo.models.EleFileBackupLog;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface EleFileBackupLogMapper extends BaseMapper<EleFileBackupLog> {
}

View File

@ -116,6 +116,16 @@ public class EdFileInfo extends BaseModel {
@TableField(value = "prj_dir") @TableField(value = "prj_dir")
private Boolean prjDir; private Boolean prjDir;
/** 当一个文件作废时其所有的历史文件也会跟着作废此时该文件及其历史文件的all_deleted=true**/
@TableField(value = "all_deleted")
private Boolean allDeleted;
/**
* 当文件被管理员永久物理删除此时为true
*/
@TableField(value = "permanent_deleted")
private Boolean permanentDeleted;
public void newInit() { public void newInit() {
String userId = UserThreadLocal.getUserId(); String userId = UserThreadLocal.getUserId();
String newFileDbId = IdWorker.getSnowFlakeIdString(); String newFileDbId = IdWorker.getSnowFlakeIdString();

View File

@ -0,0 +1,35 @@
package com.electromagnetic.industry.software.manage.pojo.models;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@TableName("ele_file_backup_log")
@Data
@NoArgsConstructor
public class EleFileBackupLog {
public EleFileBackupLog(Date reqStartTime, Date reqEndTime, long fileTime, String id) {
this.reqStartTime = reqStartTime;
this.reqEndTime = reqEndTime;
this.fileTime = fileTime;
this.id = id;
}
private String id;
private Date reqStartTime;
private Date backupStartTime;
private Date backupEndTime;
private Date reqEndTime;
private Date createTime;
private String fileName;
private Boolean backupStatus;
private String failInfoDetail;
private long fileTime;
}

View File

@ -209,7 +209,8 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
.set(EdFileInfo::getUpdatedBy, currentUserId) .set(EdFileInfo::getUpdatedBy, currentUserId)
.set(EdFileInfo::getUpdatedTime, now) .set(EdFileInfo::getUpdatedTime, now)
.set(EdFileInfo::getEffectFlag, false) .set(EdFileInfo::getEffectFlag, false)
.eq(EdFileInfo::getId, id)); .set(EdFileInfo::getAllDeleted, true)
.eq(EdFileInfo::getFileId, fileInfo.getFileId()));
return ElectromagneticResultUtil.success(true); return ElectromagneticResultUtil.success(true);
} }
@ -412,7 +413,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
AES aes = SecureUtil.aes(FILE_SEC_PASSWD.getBytes()); AES aes = SecureUtil.aes(FILE_SEC_PASSWD.getBytes());
try ( try (
InputStream inputStream = Files.newInputStream(Paths.get(destColibPath)); InputStream inputStream = Files.newInputStream(Paths.get(destColibPath));
OutputStream outputStream = Files.newOutputStream(Paths.get(zipDirPath)); OutputStream outputStream = Files.newOutputStream(Paths.get(zipDirPath))
) { ) {
aes.decrypt(inputStream, outputStream, true); aes.decrypt(inputStream, outputStream, true);
} catch (Exception e) { } catch (Exception e) {
@ -1141,11 +1142,10 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
} }
// 合并分片 // 合并分片
String destColibPath = uploadDataDir + File.separator + currentUserId + File.separator + fileName; String destColibPath = uploadDataDir + File.separator + currentUserId + File.separator + fileName;
File[] partFiles = FileUtil.ls(uploadDataDir + File.separator + currentUserId + File.separator + identifier); String path = uploadDataDir + File.separator + currentUserId + File.separator + identifier;
BufferedOutputStream outputStream = FileUtil.getOutputStream(destColibPath);
for (File partFile : partFiles) { for (int i = 1; i <= totalChunks; i++) {
try (BufferedOutputStream outputStream = FileUtil.getOutputStream(destColibPath); try (BufferedInputStream inputStream = FileUtil.getInputStream(path + File.separator + i + UPLOAD_FILE_CHUNK_SUFFIX)) {
BufferedInputStream inputStream = FileUtil.getInputStream(partFile)) {
IoUtil.copy(inputStream, outputStream); IoUtil.copy(inputStream, outputStream);
} catch (Exception e) { } catch (Exception e) {
FileUtil.del(destColibPath); FileUtil.del(destColibPath);
@ -1154,7 +1154,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
throw new BizException(info); throw new BizException(info);
} }
} }
Arrays.stream(partFiles).forEach(e -> FileUtil.del(e.getAbsolutePath())); FileUtil.del(path);
return destColibPath; return destColibPath;
} }
@ -1171,6 +1171,9 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
if(file.getId().length()<6){ if(file.getId().length()<6){
throw new StringIndexOutOfBoundsException("此文件的FILE_CODE小于六位"+id); throw new StringIndexOutOfBoundsException("此文件的FILE_CODE小于六位"+id);
} }
if(file.getPrjDir().equals(Boolean.TRUE)){
return id;
}
return file.getFileCode().substring(0, 6); return file.getFileCode().substring(0, 6);
} }

View File

@ -206,7 +206,7 @@ public class EdPrjServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo>
Assert.isTrue(EleCommonUtil.isFileNameValid(folderName), NAME_VALID_MSG); Assert.isTrue(EleCommonUtil.isFileNameValid(folderName), NAME_VALID_MSG);
// 检查当前目录下有文件如果有则不允许添加 // 检查当前目录下有文件如果有则不允许添加
long count = this.baseMapper.selectCount(Wrappers.lambdaQuery(EdFileInfo.class) long count = this.baseMapper.selectCount(Wrappers.lambdaQuery(EdFileInfo.class)
.eq(EdFileInfo::getDataStatus, EleDataTypeEnum.FILE.code) .eq(EdFileInfo::getFileType, EleDataTypeEnum.FILE.code)
.eq(EdFileInfo::getParentId, parentId) .eq(EdFileInfo::getParentId, parentId)
.eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code)); .eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code));
Assert.isTrue(count == 0, "该层级目录下存在文件,不允许再定义层级目录"); Assert.isTrue(count == 0, "该层级目录下存在文件,不允许再定义层级目录");

View File

@ -0,0 +1,93 @@
package com.electromagnetic.industry.software.manage.tasks;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.electromagnetic.industry.software.common.pojo.BackupFileResLog;
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
import com.electromagnetic.industry.software.manage.mapper.EleFileBackupLogMapper;
import com.electromagnetic.industry.software.manage.pojo.models.EleFileBackupLog;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileFilter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class BackupTask extends ServiceImpl<EleFileBackupLogMapper, EleFileBackupLog> {
private String host;
private int port;
private String prjDir;
@Resource
private Environment environment;
@PostConstruct
public void init() {
String osName = System.getProperty("os.name").toLowerCase();
String tmp = osName.startsWith("win") ? environment.getProperty("data.windows.path") : environment.getProperty("data.linux.path");
prjDir = FileUtil.getParent(tmp, 1);
}
// @Scheduled(cron = "0 0 1 * * ?")
public void backup() {
long lastFileTime = getLastFileTime();
List<File> files = filter(lastFileTime, prjDir);
for (File file : files) {
String fileCode = getFileCode(file);
Date reqStartTime = new Date();
BackupFileResLog resLog = backup(file.getAbsolutePath());
Date reqEndTime = new Date();
EleFileBackupLog resDb = new EleFileBackupLog(reqStartTime, reqEndTime, file.lastModified(), fileCode);
BeanUtil.copyProperties(resLog, resDb);
this.saveOrUpdate(resDb);
}
}
private static List<File> filter(long time, String dir) {
FileFilter filter = file -> file.lastModified() > time;
return FileUtil.loopFiles(dir, filter);
}
private String getFileCode(File file) {
String fileName = file.getName();
int index = fileName.lastIndexOf(".");
return fileName.substring(index + 1);
}
private long getLastFileTime() {
List<EleFileBackupLog> eleFileBackupLogs = this.baseMapper.selectList(Wrappers.lambdaQuery(EleFileBackupLog.class)
.select(EleFileBackupLog::getFileTime)
.orderByDesc(EleFileBackupLog::getFileTime)
.last("limit 1"));
if (eleFileBackupLogs.isEmpty()) {
return 0L;
}
return eleFileBackupLogs.get(0).getFileTime();
}
private BackupFileResLog backup(String filePath) {
String prjPath = "";
Map<String, Object> map = new HashMap<>();
map.put("file", new File(filePath));
map.put("path", prjPath);
String url = StrFormatter.format("http://{}:{}/data/file/backup/upload", host, port);
String res = HttpUtil.post(url, map);
ElectromagneticResult<?> resObj = JSONUtil.toBean(res, ElectromagneticResult.class);
String data = JSONUtil.toJsonStr(resObj.getData());
return JSONUtil.toBean(data, BackupFileResLog.class);
}
}

View File

@ -1,25 +1,27 @@
#required #required
spring.application.name=electromagnetic-data spring.application.name=electromagnetic-data
spring.datasource.typd=com.alibaba.druid.pool.DruidDataSource spring.datasource.typd=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://139.224.43.89:3306/em_data?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8&allowMultiQueries=true&rewriteBatchedStatements=true spring.datasource.url=jdbc:mysql://127.0.0.1:3306/em_data_dev?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8&allowMultiQueries=true&rewriteBatchedStatements=true
spring.datasource.username=comac spring.datasource.username=user
spring.datasource.password=2024*Comac spring.datasource.password=123123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis-plus.mapper-locations=classpath:sqlmapper/*.xml mybatis-plus.mapper-locations=classpath:sqlmapper/*.xml
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
spring.servlet.multipart.max-file-size=500MB spring.servlet.multipart.max-file-size=500MB
spring.servlet.multipart.max-request-size=10MB spring.servlet.multipart.max-request-size=500MB
pagehelper.helperDialect=mysql pagehelper.helperDialect=mysql
pagehelper.reasonable=false pagehelper.reasonable=false
server.port=12395 server.port=12396
file.security.passwd=adknfhkj87654knd
#windows文件存储目录用于测试 #windows文件存储目录用于测试
data.windows.path=D:/tmp/eleData/project/ data.windows.path=D:/tmp/szsd/data/eleData/dev/project/
data.linux.path=/szsd/data/eleData/project/ data.upload.windows.tmp.path=D:/tmp/szsd/data/eleData/dev/upload/
data.upload.windows.tmp.path=D:/tmp/eleData/upload/ data.download.windows.tmp.path=D:/tmp/szsd/data/eleData/dev/download/
data.upload.linux.tmp.path=/szsd/data/eleData/upload/
data.download.windows.tmp.path=D:/tmp/eleData/download/ data.linux.path=/szsd/data/eleData/dev/project/
data.download.linux.tmp.path=/szsd/data/eleData/download/ data.upload.linux.tmp.path=/szsd/data/eleData/dev/upload/
data.download.linux.tmp.path=/szsd/data/eleData/dev/download/
prj.folder.max.length=6 prj.folder.max.length=6
spring.jackson.time-zone=GMT+8

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>com.electromagnetic.data</groupId> <groupId>com.electromagnetic.data</groupId>
<artifactId>electromagnetic-data-new</artifactId> <artifactId>electromagnetic-data-new</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0</version>
</parent> </parent>
<artifactId>electromagnetic-common</artifactId> <artifactId>electromagnetic-common</artifactId>

View File

@ -0,0 +1,16 @@
package com.electromagnetic.industry.software.common.pojo;
import lombok.Builder;
import lombok.Data;
import java.util.Date;
@Data
@Builder
public class BackupFileResLog {
private Date backupStartTime;
private String fileName;
private Boolean backupStatus;
private Date backupEndTime;
private String failInfoDetail;
}

View File

@ -6,11 +6,12 @@
<groupId>com.electromagnetic.data</groupId> <groupId>com.electromagnetic.data</groupId>
<artifactId>electromagnetic-data-new</artifactId> <artifactId>electromagnetic-data-new</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>
<module>electrmangnetic</module> <module>electrmangnetic</module>
<module>electromagnetic-common</module> <module>electromagnetic-common</module>
<module>electrmangnetic-backup</module>
</modules> </modules>
<properties> <properties>