|
@@ -5,7 +5,6 @@ import com.mybatisflex.core.query.QueryWrapper;
|
|
import com.taais.common.core.config.TaaisConfig;
|
|
import com.taais.common.core.config.TaaisConfig;
|
|
import com.taais.common.core.core.domain.CommonResult;
|
|
import com.taais.common.core.core.domain.CommonResult;
|
|
import com.taais.common.core.core.page.PageResult;
|
|
import com.taais.common.core.core.page.PageResult;
|
|
-import com.taais.common.core.utils.MapstructUtils;
|
|
|
|
import com.taais.common.core.utils.StringUtils;
|
|
import com.taais.common.core.utils.StringUtils;
|
|
import com.taais.common.core.utils.file.FileUploadUtils;
|
|
import com.taais.common.core.utils.file.FileUploadUtils;
|
|
import com.taais.common.core.utils.file.FileUtils;
|
|
import com.taais.common.core.utils.file.FileUtils;
|
|
@@ -25,12 +24,16 @@ import jakarta.annotation.Resource;
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
import net.lingala.zip4j.model.FileHeader;
|
|
import net.lingala.zip4j.model.FileHeader;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
|
+import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.nio.file.Paths;
|
|
@@ -51,6 +54,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|
@RestController
|
|
@RestController
|
|
@RequestMapping("/demo/data")
|
|
@RequestMapping("/demo/data")
|
|
public class DataController extends BaseController {
|
|
public class DataController extends BaseController {
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(DataController.class);
|
|
@Resource
|
|
@Resource
|
|
private IDataService dataService;
|
|
private IDataService dataService;
|
|
@Autowired
|
|
@Autowired
|
|
@@ -96,12 +100,6 @@ public class DataController extends BaseController {
|
|
@RepeatSubmit()
|
|
@RepeatSubmit()
|
|
@PostMapping
|
|
@PostMapping
|
|
public CommonResult<Void> add(@Validated @RequestBody DataBo dataBo) {
|
|
public CommonResult<Void> add(@Validated @RequestBody DataBo dataBo) {
|
|
- QueryWrapper query = dataService.query();
|
|
|
|
- query.eq(Data::getBatchNum, dataBo.getBatchNum());
|
|
|
|
- long count = dataService.getMapper().selectCountByQuery(query);
|
|
|
|
- if (count > 0) {
|
|
|
|
- return CommonResult.fail("检测出现重复批次号,请仔细检查后重新添加!");
|
|
|
|
- }
|
|
|
|
boolean inserted = dataService.insert(dataBo);
|
|
boolean inserted = dataService.insert(dataBo);
|
|
if (!inserted) {
|
|
if (!inserted) {
|
|
return CommonResult.fail("新增数据管理记录失败!");
|
|
return CommonResult.fail("新增数据管理记录失败!");
|
|
@@ -144,91 +142,49 @@ public class DataController extends BaseController {
|
|
}
|
|
}
|
|
|
|
|
|
@PostMapping("/zip/upload")
|
|
@PostMapping("/zip/upload")
|
|
- public CommonResult<SysOssUploadVo> uploadZipFile(@RequestParam("file") MultipartFile file, @RequestParam("excelFile") MultipartFile excelFile, String labeled) throws Exception {
|
|
|
|
- System.out.println(labeled);
|
|
|
|
- List<DataVo> dataVos = ExcelUtil.importExcel(excelFile.getInputStream(), DataVo.class);
|
|
|
|
- Map<String, Data> picName2Obj = new HashMap<>();
|
|
|
|
- //为插入数据库做准备
|
|
|
|
- for (DataVo dataVo : dataVos) {
|
|
|
|
- picName2Obj.put(dataVo.getName(), MapstructUtils.convert(dataVo, Data.class));
|
|
|
|
- }
|
|
|
|
|
|
+ public CommonResult<SysOssUploadVo> uploadZipFile(@RequestParam("file") MultipartFile file, Data dataInfo) throws Exception {
|
|
String originalFilename = file.getOriginalFilename();
|
|
String originalFilename = file.getOriginalFilename();
|
|
assert originalFilename != null;
|
|
assert originalFilename != null;
|
|
String suffix = StringUtils.substring(originalFilename, originalFilename.lastIndexOf("."), originalFilename.length());
|
|
String suffix = StringUtils.substring(originalFilename, originalFilename.lastIndexOf("."), originalFilename.length());
|
|
-
|
|
|
|
-
|
|
|
|
// 解压
|
|
// 解压
|
|
String basedir = TaaisConfig.getUploadPath();
|
|
String basedir = TaaisConfig.getUploadPath();
|
|
- File zipOrRarTemp = FileUploadUtils.getAbsoluteFile(basedir, FileUploadUtils.extractFilename2(file));// 解压目录
|
|
|
|
|
|
+ // 解压目录
|
|
|
|
+ File zipOrRarTemp = FileUploadUtils.getAbsoluteFile(basedir, FileUploadUtils.extractFilename2(file));
|
|
String destZip = zipOrRarTemp.getAbsolutePath();
|
|
String destZip = zipOrRarTemp.getAbsolutePath();
|
|
|
|
|
|
file.transferTo(Paths.get(destZip));
|
|
file.transferTo(Paths.get(destZip));
|
|
String dest = zipOrRarTemp.getParent();
|
|
String dest = zipOrRarTemp.getParent();
|
|
|
|
|
|
- List<File> extractedFileList = new ArrayList<File>();
|
|
|
|
|
|
+ //取出最大值
|
|
|
|
+ Integer idMax = dataService.getIdMax();
|
|
|
|
+ if (idMax == null) {
|
|
|
|
+ idMax = 1;
|
|
|
|
+ } else {
|
|
|
|
+ idMax = idMax + 1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<Data> dataList = new ArrayList<>();
|
|
if (suffix.equals(".zip")) {
|
|
if (suffix.equals(".zip")) {
|
|
List<FileHeader> fileheaders = UnPackedUtil.unPackZip(zipOrRarTemp, dest);
|
|
List<FileHeader> fileheaders = UnPackedUtil.unPackZip(zipOrRarTemp, dest);
|
|
//添加解压目录
|
|
//添加解压目录
|
|
for (FileHeader fileHeader : fileheaders) {
|
|
for (FileHeader fileHeader : fileheaders) {
|
|
- if (!fileHeader.isDirectory()) {
|
|
|
|
- String fileHeaderName = fileHeader.getFileName();
|
|
|
|
- String fileHeaderSuffix = StringUtils.substring(fileHeaderName, fileHeaderName.lastIndexOf("."), fileHeaderName.length());
|
|
|
|
- if (!fileHeaderSuffix.equals(".jpg") && !fileHeaderSuffix.equals(".jpeg")
|
|
|
|
- && !fileHeaderSuffix.equals(".png")) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- extractedFileList.add(new File(dest, fileHeader.getFileName()));
|
|
|
|
|
|
+ boolean isOk = initFileInfo(dest, idMax, fileHeader.isDirectory(), fileHeader.getFileName(), dataList, dataInfo);
|
|
|
|
+ if (isOk) {
|
|
|
|
+ idMax++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
} else if (suffix.equals(".rar")) {
|
|
} else if (suffix.equals(".rar")) {
|
|
List<com.github.junrar.rarfile.FileHeader> fileheaders = UnPackedUtil.unPackRar(zipOrRarTemp, dest);
|
|
List<com.github.junrar.rarfile.FileHeader> fileheaders = UnPackedUtil.unPackRar(zipOrRarTemp, dest);
|
|
-
|
|
|
|
-
|
|
|
|
//添加解压目录
|
|
//添加解压目录
|
|
for (com.github.junrar.rarfile.FileHeader fileHeader : fileheaders) {
|
|
for (com.github.junrar.rarfile.FileHeader fileHeader : fileheaders) {
|
|
-
|
|
|
|
- if (!fileHeader.isDirectory()) {
|
|
|
|
- String fileHeaderName = fileHeader.getFileName();
|
|
|
|
- String fileHeaderSuffix = StringUtils.substring(fileHeaderName, fileHeaderName.lastIndexOf("."), fileHeaderName.length());
|
|
|
|
- if (!fileHeaderSuffix.equals(".jpg")
|
|
|
|
- && !fileHeaderSuffix.equals(".jpeg")
|
|
|
|
- && !fileHeaderSuffix.equals(".png")
|
|
|
|
- && !fileHeaderSuffix.equals(".txt")
|
|
|
|
- && !fileHeaderSuffix.equals(".bmp")) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- extractedFileList.add(new File(dest, fileHeader.getFileName()));
|
|
|
|
|
|
+ boolean isOk = initFileInfo(dest, idMax, fileHeader.isDirectory(), fileHeader.getFileName(), dataList, dataInfo);
|
|
|
|
+ if (isOk) {
|
|
|
|
+ idMax++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- //取出最大值
|
|
|
|
- Integer idMax = dataService.getIdMax();
|
|
|
|
- if (idMax == null) {
|
|
|
|
- idMax = 1;
|
|
|
|
- } else {
|
|
|
|
- idMax = idMax + 1;
|
|
|
|
- }
|
|
|
|
- List<Data> dataList = new ArrayList<>();
|
|
|
|
- for (File efile : extractedFileList) {
|
|
|
|
- String pathFileName = FileUploadUtils.getPathFileName(dest, efile.getName());
|
|
|
|
- Data mydata = picName2Obj.get(efile.getName());
|
|
|
|
- // 检查是否已标准抓住
|
|
|
|
- if (checkLabeled(pathFileName)) {
|
|
|
|
- mydata.setLabeled(Boolean.TRUE);
|
|
|
|
- } else {
|
|
|
|
- mydata.setLabeled(Boolean.FALSE);
|
|
|
|
- }
|
|
|
|
- mydata.setFormName(mydata.getName());
|
|
|
|
- String fileHeaderSuffix = StringUtils.substring(mydata.getName(), mydata.getName().lastIndexOf("."), mydata.getName().length());
|
|
|
|
- mydata.setName(idMax + fileHeaderSuffix);
|
|
|
|
- mydata.setUrl(pathFileName);
|
|
|
|
- dataList.add(mydata);
|
|
|
|
- idMax++;
|
|
|
|
- }
|
|
|
|
|
|
|
|
- if ("true".equals(labeled)) {
|
|
|
|
|
|
+ if (dataInfo.getLabeled()) {
|
|
List<Data> unmarkedData = dataList.stream()
|
|
List<Data> unmarkedData = dataList.stream()
|
|
.filter(data -> Boolean.FALSE.equals(data.getLabeled()))
|
|
.filter(data -> Boolean.FALSE.equals(data.getLabeled()))
|
|
.toList();
|
|
.toList();
|
|
@@ -259,15 +215,42 @@ public class DataController extends BaseController {
|
|
if (isTrue.get()) {
|
|
if (isTrue.get()) {
|
|
return CommonResult.fail("文件检测出重复批次号,请仔细检查后重新导入!");
|
|
return CommonResult.fail("文件检测出重复批次号,请仔细检查后重新导入!");
|
|
}
|
|
}
|
|
-
|
|
|
|
dataService.saveBatch(dataList);
|
|
dataService.saveBatch(dataList);
|
|
//删除压缩文件
|
|
//删除压缩文件
|
|
FileUtils.deleteFile(destZip);
|
|
FileUtils.deleteFile(destZip);
|
|
-
|
|
|
|
-
|
|
|
|
return CommonResult.success("数据集上传成功!");
|
|
return CommonResult.success("数据集上传成功!");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private boolean initFileInfo(String dest, Integer idMax, boolean directory, String fileName, List<Data> dataList, Data dataInfo) throws IOException {
|
|
|
|
+ if (!directory) {
|
|
|
|
+ String fileHeaderSuffix = StringUtils.substring(fileName, fileName.lastIndexOf("."), fileName.length());
|
|
|
|
+ if (!fileHeaderSuffix.equals(".jpg") && !fileHeaderSuffix.equals(".jpeg") && !fileHeaderSuffix.equals(".png")) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ File file = new File(dest, idMax + fileHeaderSuffix);
|
|
|
|
+ File odlFile = new File(dest, fileName);
|
|
|
|
+ log.info("更改用户上传文件名称:{}", odlFile.renameTo(file));
|
|
|
|
+ String pathFileName = FileUploadUtils.getPathFileName(dest, idMax + fileHeaderSuffix);
|
|
|
|
+ // 检查是否已标准抓住
|
|
|
|
+ Data data = new Data();
|
|
|
|
+ BeanUtils.copyProperties(dataInfo, data);
|
|
|
|
+ if (checkLabeled(pathFileName)) {
|
|
|
|
+ data.setLabeled(Boolean.TRUE);
|
|
|
|
+ } else {
|
|
|
|
+ data.setLabeled(Boolean.FALSE);
|
|
|
|
+ }
|
|
|
|
+ int lastSlashIndex = fileName.lastIndexOf('/');
|
|
|
|
+ if (lastSlashIndex != -1) {
|
|
|
|
+ fileName = fileName.substring(lastSlashIndex + 1);
|
|
|
|
+ }
|
|
|
|
+ data.setName(fileName);
|
|
|
|
+ data.setUrl(pathFileName);
|
|
|
|
+ dataList.add(data);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
// 检查有没有标注信息有的话把标注数据置为已标注
|
|
// 检查有没有标注信息有的话把标注数据置为已标注
|
|
private boolean checkLabeled(String pathFileName) {
|
|
private boolean checkLabeled(String pathFileName) {
|
|
Path newPath = FileUtils.getTxtPath(pathFileName);
|
|
Path newPath = FileUtils.getTxtPath(pathFileName);
|