1
0

fix(web): 优化文件上传适配

This commit is contained in:
2024-11-27 18:05:15 +08:00
parent f15bcc2f53
commit aba9a1716c
16 changed files with 318 additions and 114 deletions

View File

@@ -19,7 +19,7 @@ public abstract class SimpleController<ENTITY extends SimpleEntity, SAVE_ITEM, L
protected static final String DETAIL = "/detail/{id}";
protected static final String REMOVE = "/remove/{id}";
protected final SimpleService<ENTITY> service;
private final SimpleService<ENTITY> service;
public SimpleController(SimpleService<ENTITY> service) {
this.service = service;

View File

@@ -1,6 +1,13 @@
package com.eshore.gringotts.web.domain.base.entity;
import com.eshore.gringotts.web.domain.upload.entity.DataFile;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@@ -8,18 +15,37 @@ import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonDeserialize(using = FileInfo.FileInfoDeserializer.class)
public final class FileInfo {
private Long id;
private String id;
private String name;
private String filename;
private Long value;
private String state;
public FileInfo(DataFile dataFile) {
this.id = dataFile.getId();
this.id = dataFile.getId().toString();
this.name = dataFile.getFilename();
this.filename = dataFile.getFilename();
this.value = dataFile.getId();
this.state = "uploaded";
}
public static final class FileInfoDeserializer extends JsonDeserializer<FileInfo> {
@Override
public FileInfo deserialize(JsonParser parser, DeserializationContext context) throws IOException {
TreeNode root = parser.readValueAsTree();
if (root instanceof ObjectNode) {
ObjectNode node = (ObjectNode) root;
return new FileInfo(
node.get("id").asText(),
node.get("name").asText(),
node.get("filename").asText(),
node.get("value").asLong(),
node.get("state").asText()
);
}
return null;
}
}
}

View File

@@ -20,8 +20,8 @@ import org.eclipse.collections.api.set.ImmutableSet;
*/
@Slf4j
public abstract class SimpleService<ENTITY extends SimpleEntity> {
protected final SimpleRepository<ENTITY, Long> repository;
protected final UserService userService;
private final SimpleRepository<ENTITY, Long> repository;
private final UserService userService;
public SimpleService(SimpleRepository<ENTITY, Long> repository, UserService userService) {
this.repository = repository;

View File

@@ -1,8 +1,8 @@
package com.eshore.gringotts.web.domain.confirmation.controller;
import com.eshore.gringotts.web.configuration.amis.AmisResponse;
import com.eshore.gringotts.web.domain.base.controller.SimpleController;
import com.eshore.gringotts.web.domain.base.entity.FileInfo;
import com.eshore.gringotts.web.domain.base.entity.SimpleDetailItem;
import com.eshore.gringotts.web.domain.base.entity.SimpleListItem;
import com.eshore.gringotts.web.domain.base.entity.SimpleSaveItem;
import com.eshore.gringotts.web.domain.confirmation.entity.Confirmation;
@@ -14,6 +14,8 @@ import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.factory.Sets;
import org.eclipse.collections.api.set.ImmutableSet;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -25,22 +27,36 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("confirmation")
public class ConfirmationController extends SimpleController<Confirmation, ConfirmationController.SaveItem, ConfirmationController.ListItem, ConfirmationController.DetailItem> {
private final ConfirmationService confirmationService;
private final DataResourceService dataResourceService;
private final DataFileService dataFileService;
public ConfirmationController(ConfirmationService service, DataResourceService dataResourceService, DataFileService dataFileService) {
public ConfirmationController(ConfirmationService service, ConfirmationService confirmationService, DataResourceService dataResourceService, DataFileService dataFileService) {
super(service);
this.confirmationService = confirmationService;
this.dataResourceService = dataResourceService;
this.dataFileService = dataFileService;
}
@GetMapping("/submit/{id}")
public AmisResponse<Object> submit(@PathVariable Long id) {
confirmationService.submit(id);
return AmisResponse.responseSuccess();
}
@GetMapping("/retract/{id}")
public AmisResponse<Object> retract(@PathVariable Long id) {
confirmationService.retract(id);
return AmisResponse.responseSuccess();
}
@Override
protected Confirmation fromSaveItem(SaveItem item) {
Confirmation confirmation = new Confirmation();
confirmation.setId(item.getId());
confirmation.setDescription(item.getDescription());
confirmation.setTarget(dataResourceService.detailOrThrow(item.getTargetId()));
confirmation.setEvidences(dataFileService.list(item.getEvidenceIds()).toSet());
confirmation.setEvidences(dataFileService.list(item.getEvidenceFiles().collect(FileInfo::getValue)).toSet());
return confirmation;
}
@@ -50,13 +66,14 @@ public class ConfirmationController extends SimpleController<Confirmation, Confi
item.setId(entity.getId());
item.setName(entity.getTarget().getName());
item.setDescription(entity.getDescription());
item.setState(entity.getState().name());
item.setCreatedUsername(entity.getCreatedUser().getUsername());
item.setCreatedTime(entity.getCreatedTime());
return item;
}
@Override
protected DetailItem toDetailItem(Confirmation entity) throws Exception {
protected DetailItem toDetailItem(Confirmation entity) {
return new DetailItem(entity);
}
@@ -65,7 +82,7 @@ public class ConfirmationController extends SimpleController<Confirmation, Confi
public static class SaveItem extends SimpleSaveItem<Confirmation> {
private Long targetId;
private String description;
private ImmutableSet<Long> evidenceIds;
private ImmutableSet<FileInfo> evidenceFiles;
}
@Data
@@ -73,31 +90,20 @@ public class ConfirmationController extends SimpleController<Confirmation, Confi
public static class ListItem extends SimpleListItem<Confirmation> {
private String name;
private String description;
private String state;
}
@Data
@EqualsAndHashCode(callSuper = true)
public static final class DetailItem extends SimpleDetailItem<Confirmation> {
private Long targetId;
public static final class DetailItem extends SaveItem {
private String targetName;
private String description;
private ImmutableSet<FileInfo> evidenceIds;
public DetailItem(Confirmation confirmation) {
this.setId(confirmation.getId());
this.setTargetId(confirmation.getTarget().getId());
this.setTargetName(confirmation.getTarget().getName());
this.setDescription(confirmation.getDescription());
this.setEvidenceIds(
Sets.immutable.ofAll(confirmation.getEvidences())
.collect(file -> new FileInfo(
file.getId(),
file.getFilename(),
file.getFilename(),
file.getId(),
"uploaded"
))
);
this.setEvidenceFiles(Sets.immutable.ofAll(confirmation.getEvidences()).collect(FileInfo::new));
}
}
}

View File

@@ -57,9 +57,13 @@ public class Confirmation extends SimpleEntity {
private Set<DataFile> evidences;
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private State state = State.CHECKING;
private State state = State.DRAFT;
public enum State {
/**
* 草稿
*/
DRAFT,
/**
* 审查中
*/
@@ -69,8 +73,8 @@ public class Confirmation extends SimpleEntity {
*/
NORMAL,
/**
* 禁用
* 驳回
*/
DISABLED,
REJECT,
}
}

View File

@@ -4,7 +4,10 @@ import com.eshore.gringotts.web.domain.base.repository.SimpleRepository;
import com.eshore.gringotts.web.domain.confirmation.entity.Confirmation;
import java.util.List;
import java.util.Optional;
import javax.transaction.Transactional;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
/**
@@ -21,4 +24,11 @@ public interface ConfirmationRepository extends SimpleRepository<Confirmation, L
@Override
@EntityGraph(value = "confirmation.detail", type = EntityGraph.EntityGraphType.FETCH)
Optional<Confirmation> findById(Long id);
Boolean existsByTarget_Id(Long id);
@Transactional
@Modifying
@Query("update Confirmation confirmation set confirmation.state = ?2 where confirmation.id = ?1")
void updateStateById(Long id, Confirmation.State state);
}

View File

@@ -14,7 +14,32 @@ import org.springframework.stereotype.Service;
@Slf4j
@Service
public class ConfirmationService extends SimpleService<Confirmation> {
public ConfirmationService(ConfirmationRepository repository, UserService userService) {
super(repository, userService);
private final ConfirmationRepository confirmationRepository;
public ConfirmationService(ConfirmationRepository confirmationRepository, UserService userService) {
super(confirmationRepository, userService);
this.confirmationRepository = confirmationRepository;
}
@Override
public Long save(Confirmation entity) {
if (confirmationRepository.existsByTarget_Id(entity.getTarget().getId())) {
throw new ConfirmationDuplicatedException();
}
return super.save(entity);
}
public void submit(Long id) {
confirmationRepository.updateStateById(id, Confirmation.State.CHECKING);
}
public void retract(Long id) {
confirmationRepository.updateStateById(id, Confirmation.State.DRAFT);
}
public static final class ConfirmationDuplicatedException extends RuntimeException {
public ConfirmationDuplicatedException() {
super("数据资源已绑定确权申请,无法再次申请");
}
}
}

View File

@@ -0,0 +1,32 @@
package com.eshore.gringotts.web.domain.order.entity;
import com.eshore.gringotts.core.Constants;
import com.eshore.gringotts.web.domain.base.entity.SimpleEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
/**
* 工单
*
* @author lanyuanxiaoyao
* @date 2024-11-27
*/
@Getter
@Setter
@ToString
@Entity
@EntityListeners(AuditingEntityListener.class)
@DynamicUpdate
@Table(name = Constants.TABLE_PREFIX + "work_order")
public class WorkOrder extends SimpleEntity {
@Column(nullable = false)
private String name;
private String description;
}

View File

@@ -29,8 +29,6 @@ import java.util.Map;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -64,7 +62,9 @@ public class DataResourceController extends SimpleController<DataResource, DataR
dataResource.setDescription(item.getDescription());
dataResource.setType(type);
dataResource.setFormat(format);
dataResource.setExample(dataFileService.detail(item.getExampleFileId()));
if (ObjectUtil.isNotNull(item.getExampleFile())) {
dataResource.setExample(dataFileService.detail(item.getExampleFile().getValue()));
}
return dataResource;
}
@@ -102,8 +102,7 @@ public class DataResourceController extends SimpleController<DataResource, DataR
break;
case FILE:
FileResourceType fileType = (FileResourceType) dataResource.getType();
item.setFileId(fileType.getFile().getId());
item.setFile(Lists.immutable.of(new FileInfo(fileType.getFile())));
item.setFile(new FileInfo(fileType.getFile()));
break;
case DATABASE:
DatabaseResourceType databaseType = (DatabaseResourceType) dataResource.getType();
@@ -114,10 +113,8 @@ public class DataResourceController extends SimpleController<DataResource, DataR
break;
case HDFS:
HDFSResourceType hdfsType = (HDFSResourceType) dataResource.getType();
item.setCoreSiteFileId(hdfsType.getCoreSite().getId());
item.setCoreSiteFile(Lists.immutable.of(new FileInfo(hdfsType.getCoreSite())));
item.setHdfsSiteFileId(hdfsType.getHdfsSite().getId());
item.setHdfsSiteFile(Lists.immutable.of(new FileInfo(hdfsType.getHdfsSite())));
item.setCoreSiteFile(new FileInfo(hdfsType.getCoreSite()));
item.setHdfsSiteFile(new FileInfo(hdfsType.getHdfsSite()));
break;
case FTP:
FtpResourceType ftpType = (FtpResourceType) dataResource.getType();
@@ -150,8 +147,7 @@ public class DataResourceController extends SimpleController<DataResource, DataR
break;
}
if (ObjectUtil.isNotNull(dataResource.getExample())) {
item.setExampleFileId(dataResource.getExample().getId());
item.setExampleFile(Lists.immutable.of(new FileInfo(dataResource.getExample())));
item.setExampleFile(new FileInfo(dataResource.getExample()));
}
item.setCreatedUsername(dataResource.getCreatedUser().getUsername());
item.setCreatedTime(dataResource.getCreatedTime());
@@ -167,7 +163,7 @@ public class DataResourceController extends SimpleController<DataResource, DataR
type = new ApiResourceType(item.getApiUrl(), item.getApiUsername(), item.getApiPassword());
break;
case FILE:
DataFile dataFile = dataFileService.detail(item.getFileId());
DataFile dataFile = dataFileService.detail(item.getFile().getValue());
type = new FileResourceType(dataFile);
break;
case DATABASE:
@@ -179,7 +175,7 @@ public class DataResourceController extends SimpleController<DataResource, DataR
);
break;
case HDFS:
type = new HDFSResourceType(dataFileService.detail(item.getCoreSiteFileId()), dataFileService.detail(item.getHdfsSiteFileId()));
type = new HDFSResourceType(dataFileService.detail(item.getCoreSiteFile().getValue()), dataFileService.detail(item.getHdfsSiteFile().getValue()));
break;
case FTP:
type = new FtpResourceType(item.getFtpUrl(), item.getFtpUsername(), item.getFtpPassword(), item.getFtpPath(), item.getFtpRegexFilter());
@@ -222,13 +218,13 @@ public class DataResourceController extends SimpleController<DataResource, DataR
private String apiUrl;
private String apiUsername;
private String apiPassword;
private Long fileId;
private FileInfo file;
private String databaseType;
private String databaseJdbc;
private String databaseUsername;
private String databasePassword;
private Long coreSiteFileId;
private Long hdfsSiteFileId;
private FileInfo coreSiteFile;
private FileInfo hdfsSiteFile;
private String ftpUrl;
private String ftpUsername;
private String ftpPassword;
@@ -242,7 +238,7 @@ public class DataResourceController extends SimpleController<DataResource, DataR
private String jsonLineSchemaText;
private Map<?, ?> csvSchema;
private String csvSchemaText;
private Long exampleFileId;
private FileInfo exampleFile;
}
@Data
@@ -261,11 +257,6 @@ public class DataResourceController extends SimpleController<DataResource, DataR
@Data
@EqualsAndHashCode(callSuper = true)
public static final class DetailItem extends SaveItem {
private Long id;
private ImmutableList<FileInfo> file;
private ImmutableList<FileInfo> coreSiteFile;
private ImmutableList<FileInfo> hdfsSiteFile;
private ImmutableList<FileInfo> exampleFile;
private LocalDateTime createdTime;
private String createdUsername;
private LocalDateTime modifiedTime;

View File

@@ -34,15 +34,15 @@ import org.springframework.web.multipart.MultipartFile;
*/
@RestController
@RequestMapping("/upload")
public class UploadController {
private static final Logger logger = LoggerFactory.getLogger(UploadController.class);
public class DataFileController {
private static final Logger logger = LoggerFactory.getLogger(DataFileController.class);
private final DataFileService dataFileService;
private final String uploadFolderPath;
private final String cacheFolderPath;
private final String sliceFolderPath;
public UploadController(UploadConfiguration uploadConfiguration, DataFileService dataFileService) {
public DataFileController(UploadConfiguration uploadConfiguration, DataFileService dataFileService) {
this.dataFileService = dataFileService;
this.uploadFolderPath = uploadConfiguration.getUploadPath();

View File

@@ -17,8 +17,13 @@ import org.springframework.stereotype.Service;
@Slf4j
@Service
public class DataFileService extends SimpleService<DataFile> {
public DataFileService(DataFileRepository repository, UserService userService) {
super(repository, userService);
private final DataFileRepository dataFileRepository;
private final UserService userService;
public DataFileService(DataFileRepository dataFileRepository, UserService userService) {
super(dataFileRepository, userService);
this.dataFileRepository = dataFileRepository;
this.userService = userService;
}
public DataFile detail(String id) {
@@ -34,17 +39,17 @@ public class DataFileService extends SimpleService<DataFile> {
User loginUser = userService.currentLoginUser();
dataFile.setCreatedUser(loginUser);
dataFile.setModifiedUser(loginUser);
return repository.save(dataFile).getId();
return dataFileRepository.save(dataFile).getId();
}
public void updateDataFile(Long id, String path, Long size, String md5) {
DataFile dataFile = repository.findById(id).orElseThrow(UpdateDataFileFailedException::new);
DataFile dataFile = dataFileRepository.findById(id).orElseThrow(UpdateDataFileFailedException::new);
dataFile.setSize(size);
dataFile.setMd5(md5);
dataFile.setPath(path);
User loginUser = userService.currentLoginUser();
dataFile.setModifiedUser(loginUser);
repository.save(dataFile);
dataFileRepository.save(dataFile);
}
public static final class DataFileNotFoundException extends RuntimeException {