提交代码0323

This commit is contained in:
lilin 2025-03-23 12:56:43 +08:00
parent 88e0ce82cd
commit 9c0486770e
11 changed files with 1079 additions and 164 deletions

View File

@ -0,0 +1,68 @@
package com.yfd.platform.modules.experimentalData.enums;
import ch.qos.logback.core.status.Status;
import io.netty.handler.codec.compression.CompressionException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
/**
* 支持的压缩格式枚举
* - ZIP: 标准ZIP格式
* - TAR: 未压缩的TAR归档
* - TAR_GZ: GZIP压缩的TAR归档
* - TAR_BZ2: BZIP2压缩的TAR归档
* - TAR_XZ: XZ压缩的TAR归档
*/
@Getter
@AllArgsConstructor
public enum CompressionFormat {
ZIP("zip"),
TAR_GZ("tar.gz", "tgz"),
SEVEN_ZIP("7z");
private final Set<String> aliases = new HashSet<>();
CompressionFormat(String... aliases) {
Collections.addAll(this.aliases, aliases);
}
/**
* 从字符串转换压缩格式
* @param formatStr 格式字符串不区分大小写
* @return 对应的枚举值
* @throws IllegalArgumentException 格式不支持时抛出
*/
public static CompressionFormat fromString(String formatStr) {
String normalized = formatStr.toLowerCase().trim();
for (CompressionFormat format : values()) {
if (format.aliases.contains(normalized)) {
return format;
}
}
throw new IllegalArgumentException("不支持的压缩格式: " + formatStr
+ ",支持格式: " + getSupportedFormats());
}
/**
* 获取支持格式列表
* @return 格式列表字符串逗号分隔
*/
public static String getSupportedFormats() {
return Arrays.stream(values())
.map(f -> String.join("|", f.aliases))
.collect(Collectors.joining(", "));
}
/**
* 获取主扩展名
*/
public String getPrimaryExtension() {
return aliases.iterator().next();
}
}

View File

@ -32,6 +32,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.File;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.*;
@ -280,9 +281,25 @@ public class TsNodesServiceImpl extends ServiceImpl<TsNodesMapper, TsNodes> impl
}
//序号
tsnodes.setNodeOrder(orderno);
int valueAdded = tsNodesMapper.insert(tsnodes);
if (valueAdded == 1) {
LOGGER.info("tsnodes表结构增加成功");
//判断文件夹是否创建
AbstractBaseFileService<?> fileService = storageSourceContext.getByStorageKey("local");
boolean flag = fileService.isFolderCreated(File.separator + tsnodes.getNodeId());
//如果是false 说明没有创建 那就新建一个文件夹
if(!flag){
//本地创建文件夹
AbstractBaseFileService<?> fileServiceData = storageSourceContext.getByStorageKey("local");
boolean flagData = fileServiceData.newFolder(File.separator, tsnodes.getNodeId());
if(!flagData){
LOGGER.error("创建节点文件夹失败!");
return ResponseResult.error("新增节点失败!");
}
}
return ResponseResult.success();
} else {
LOGGER.error("tsnodes表结构增加失败");

View File

@ -74,12 +74,8 @@ public class FilesController {
if (ObjUtil.isEmpty(files)) {
return ResponseResult.error("参数为空");
}
Boolean isOk = filesService.addFiles(files);
if (isOk) {
return ResponseResult.success();
} else {
return ResponseResult.error();
}
return filesService.addFiles(files);
}
/**********************************

View File

@ -1,6 +1,7 @@
package com.yfd.platform.modules.specialDocument.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.modules.specialDocument.domain.Files;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.web.multipart.MultipartFile;
@ -38,7 +39,7 @@ public interface IFilesService extends IService<Files> {
* Files 文档内容
* 返回值说明: com.yfd.platform.config.ResponseResult 返回新增成功或者失败
***********************************/
Boolean addFiles(Files files);
ResponseResult addFiles(Files files);
/**********************************
* 用途说明: 修改专项文档管理-文档内容

View File

@ -188,16 +188,9 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
* 返回值说明: com.yfd.platform.config.ResponseResult 返回新增成功或者失败
***********************************/
@Override
public Boolean addFiles(Files files) {
public ResponseResult addFiles(Files files) {
Boolean value = false;
// 校验文件名是否包含非法字符
String fileName = files.getFileName();
if (containsInvalidCharacters(fileName)) {
return value;
}
// String[] splitIds = ids.split(","); BigDecimal
List<String> names = Arrays.asList(files.getFileName().split(","));
List<String> sizes = Arrays.asList(files.getFileSize().split(","));
@ -210,8 +203,7 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
// 数据校验
if (names.size() != sizes.size()) {
LOGGER.error("文件名称和文件大小的列表长度不一致");
return false;
return ResponseResult.error("文件名称和文件大小的列表长度不一致!");
}
List<Files> filesToSave = new ArrayList<>();
// 设置当前时间
@ -220,9 +212,25 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
Timestamp currentTime = Timestamp.valueOf(now);
files.setUploadTime(currentTime);
for (int i = 0; i < names.size(); i++) {
String name = names.get(i).trim();
String sizeStr = sizes.get(i).trim();
// 校验文件名是否包含非法字符
if (containsInvalidCharacters(name)) {
return ResponseResult.error("文件名包含非法字符!");
}
//校验是否真正上传
String pathAndName = files.getFilePath()+"/" + name;
//准备获取文件的信息
AbstractBaseFileService<?> fileService = storageSourceContext.getByStorageKey("minio");
FileItemResult fileItemResult = fileService.getFileItem(pathAndName);
if (fileItemResult == null || fileItemResult.getName() == null) {
return ResponseResult.error(name + "文件没有上传到空间,请重新选择上传!");
}
// 校验文件大小是否可以转换成数值
try {
Files files1 = new Files();
@ -237,7 +245,7 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
files1.setFileSize(sizeStr);
filesToSave.add(files1);
} catch (NumberFormatException e) {
LOGGER.error("文件大小必须是有效的数字");
return ResponseResult.error("文件大小必须是有效的数字!");
}
}
if(filesToSave.size()>0){
@ -251,7 +259,11 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
}
}
}
return value;
if (value) {
return ResponseResult.success("新增文件成功!");
} else {
return ResponseResult.error("新增文件失败!");
}
}
/**********************************

View File

@ -89,14 +89,13 @@ public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> impl
@Override
public Boolean addSdproject(Project project) {
//查询字典表获取项目类型对应数据字典
// QueryWrapper<SysDictionaryItems> queryWrapperSysDictionary = new QueryWrapper<>();
// queryWrapperSysDictionary.eq("parentcode", "zxxmlx");
// queryWrapperSysDictionary.eq("itemcode", project.getProjectType());
// queryWrapperSysDictionary.orderByAsc("orderno");
// SysDictionaryItems sysDictionaryItems = sysDictionaryItemsMapper.selectOne(queryWrapperSysDictionary);
// if(sysDictionaryItems != null){
// project.setProjectType(sysDictionaryItems.getDictName());
// }
QueryWrapper<SysDictionaryItems> queryWrapperSysDictionary = new QueryWrapper<>();
queryWrapperSysDictionary.eq("parentcode", "compressType");
queryWrapperSysDictionary.orderByAsc("orderno");
SysDictionaryItems sysDictionaryItems = sysDictionaryItemsMapper.selectOne(queryWrapperSysDictionary);
if(sysDictionaryItems != null){
project.setProjectType(sysDictionaryItems.getDictName());
}
//TODO 01.21沟通以后说是先不用管重复校验问题

View File

@ -3,9 +3,13 @@ package com.yfd.platform.modules.storage.service.base;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.HttpMethod;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.*;
import com.amazonaws.services.s3.transfer.Copy;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
import com.yfd.platform.constant.ZFileConstant;
import com.yfd.platform.exception.StorageSourceAutoConfigCorsException;
import com.yfd.platform.modules.config.service.SystemConfigService;
@ -592,14 +596,55 @@ public abstract class AbstractS3BaseFileService<P extends S3BaseParam> extends A
@Override
public boolean renameFile(String path, String name, String newName) {
String srcPath = StringUtils.concatTrimStartSlashes(param.getBasePath(), path, name);
String distPath = StringUtils.concatTrimStartSlashes(param.getBasePath(), path, newName);
String srcKey = StringUtils.concatTrimStartSlashes(param.getBasePath(), path, name);
String destKey = StringUtils.concatTrimStartSlashes(param.getBasePath(), path, newName);
String bucketName = param.getBucketName();
s3Client.copyObject(bucketName, srcPath, bucketName, distPath);
deleteFile(path, name);
TransferManager transferManager = TransferManagerBuilder.standard()
.withS3Client(s3Client)
.build();
try {
// 异步复制适合大文件
Copy copy = transferManager.copy(
param.getBucketName(), srcKey,
param.getBucketName(), destKey
);
copy.waitForCompletion(); // 等待复制完成
s3Client.deleteObject(param.getBucketName(), srcKey);
return true;
} catch (InterruptedException | AmazonServiceException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Rename failed", e);
} finally {
transferManager.shutdownNow();
}
}
public boolean renameFileWithTransferManager(String path, String name, String newName) {
String srcKey = StringUtils.concatTrimStartSlashes(param.getBasePath(), path, name);
String destKey = StringUtils.concatTrimStartSlashes(param.getBasePath(), path, newName);
TransferManager transferManager = TransferManagerBuilder.standard()
.withS3Client(s3Client)
.build();
try {
// 异步复制适合大文件
Copy copy = transferManager.copy(
param.getBucketName(), srcKey,
param.getBucketName(), destKey
);
copy.waitForCompletion(); // 等待复制完成
s3Client.deleteObject(param.getBucketName(), srcKey);
return true;
} catch (InterruptedException | AmazonServiceException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Rename failed", e);
} finally {
transferManager.shutdownNow();
}
}
//todo 改造之前的重命名
// @Override

View File

@ -288,4 +288,14 @@ public interface BaseFileService {
*/
List<FileItemResult> fileListData(String path, String name) throws Exception;
/**
* 创建新文件夹
*
* @param folderPath
* 文件夹路径
*
* @return 是否创建成功
*/
boolean isFolderCreated(String folderPath);
}

View File

@ -29,6 +29,8 @@ import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@ -112,6 +114,20 @@ public class LocalServiceImpl extends AbstractProxyTransferService<LocalParam> {
return FileUtil.mkdir(fullPath) != null;
}
/**
* 判断给定路径的文件夹是否已经创建
*
* @param folderPath 文件夹路径例如 "/folderName"
* @return true - 文件夹已存在false - 文件夹不存在
*/
public boolean isFolderCreated(String folderPath) {
// 将路径转换为绝对路径如果需要
Path path = Paths.get(folderPath).toAbsolutePath();
// 检查路径是否存在且是一个目录
return Files.exists(path) && Files.isDirectory(path);
}
@Override
public boolean deleteFile(String path, String name) {

View File

@ -35,4 +35,9 @@ public class MinIOServiceImpl extends AbstractS3BaseFileService<MinIOParam> {
return StorageTypeEnum.MINIO;
}
@Override
public boolean isFolderCreated(String folderPath) {
return false;
}
}