mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
完善文件上传
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -170,6 +170,11 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-undertow</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.hypersistence</groupId>
|
||||
<artifactId>hypersistence-utils-hibernate-62</artifactId>
|
||||
<version>3.6.1</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-core -->
|
||||
<dependency>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
|
||||
@@ -4,6 +4,7 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@@ -21,4 +22,5 @@ public abstract class BaseDto {
|
||||
private String updateUserName;
|
||||
|
||||
private LocalDateTime updateTime;
|
||||
private List<String> fileIds;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package cn.lihongjie.coal.base.entity;
|
||||
|
||||
import cn.lihongjie.coal.common.Ctx;
|
||||
|
||||
import io.hypersistence.utils.hibernate.type.array.ListArrayType;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import lombok.Getter;
|
||||
@@ -11,6 +13,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.hibernate.annotations.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@MappedSuperclass
|
||||
@Getter
|
||||
@@ -44,8 +47,14 @@ public class BaseEntity {
|
||||
@UpdateTimestamp(source = SourceType.VM)
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
@Comment("关联附件")
|
||||
@Column(columnDefinition = "text[]")
|
||||
@Type(ListArrayType.class)
|
||||
private List<String> fileIds;
|
||||
|
||||
@PrePersist
|
||||
public void prePersist() {
|
||||
|
||||
if (StringUtils.isEmpty(this.createUserId)) {
|
||||
|
||||
this.createUserId = Ctx.isLoggedIn() ? Ctx.getUserId() : "";
|
||||
|
||||
@@ -33,8 +33,12 @@ public class FileController {
|
||||
public FileDto upload(
|
||||
@RequestParam("file") MultipartFile file,
|
||||
@RequestParam("name") String name,
|
||||
@RequestParam("dir") String dir) {
|
||||
return this.service.upload(file, name, dir);
|
||||
@RequestParam("dir") String dir,
|
||||
@RequestParam(value = "sortKey", required = false) String sortKey,
|
||||
@RequestParam(value = "code", required = false) String code,
|
||||
@RequestParam(value = "remarks", required = false) String remarks
|
||||
) {
|
||||
return this.service.upload(file, name, dir, sortKey, code, remarks);
|
||||
}
|
||||
|
||||
@PostMapping("/update")
|
||||
|
||||
@@ -25,6 +25,5 @@ public class CreateFileDto extends OrgCommonDto {
|
||||
|
||||
@Comment("局域网访问地址")
|
||||
private String privateUrl;
|
||||
@Comment("业务ID")
|
||||
private String businessId;
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,5 @@ public class FileDto extends OrgCommonDto {
|
||||
|
||||
@Comment("局域网访问地址")
|
||||
private String privateUrl;
|
||||
@Comment("业务ID")
|
||||
private String businessId;
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,5 @@ public class UpdateFileDto extends OrgCommonDto {
|
||||
|
||||
@Comment("局域网访问地址")
|
||||
private String privateUrl;
|
||||
@Comment("业务ID")
|
||||
private String businessId;
|
||||
|
||||
}
|
||||
|
||||
@@ -33,6 +33,5 @@ public class FileEntity extends OrgCommonEntity {
|
||||
@Comment("对象ID")
|
||||
private String objectId;
|
||||
|
||||
@Comment("业务ID")
|
||||
private String businessId;
|
||||
|
||||
}
|
||||
|
||||
@@ -38,14 +38,14 @@ import java.util.UUID;
|
||||
@Service
|
||||
@Slf4j
|
||||
@Transactional
|
||||
public
|
||||
class FileService extends BaseService<FileEntity, FileRepository> {
|
||||
public class FileService extends BaseService<FileEntity, FileRepository> {
|
||||
@Autowired ObsClient obsClient;
|
||||
@Autowired HwCloudProperty hwCloudProperty;
|
||||
@Autowired private FileRepository repository;
|
||||
@Autowired private FileMapper mapper;
|
||||
@Autowired private ConversionService conversionService;
|
||||
private final Tika tika = new Tika();
|
||||
|
||||
@Value("${spring.profiles.active}")
|
||||
private String activateProfile;
|
||||
|
||||
@@ -88,10 +88,19 @@ class FileService extends BaseService<FileEntity, FileRepository> {
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public FileDto upload(MultipartFile file, String name, String dir) {
|
||||
public FileDto upload(MultipartFile file, String name, String dir, String sortKey, String code, String remarks) {
|
||||
FileEntity fileEntity = new FileEntity();
|
||||
fileEntity.setFileName(name);
|
||||
fileEntity.setFileSize(file.getSize());
|
||||
fileEntity.setCode(code);
|
||||
fileEntity.setRemarks(remarks);
|
||||
try {
|
||||
|
||||
fileEntity.setSortKey(Integer.parseInt(sortKey));
|
||||
} catch (Exception e) {
|
||||
|
||||
fileEntity.setSortKey(1);
|
||||
}
|
||||
fileEntity.setDirectory(
|
||||
"%s/%s/%s"
|
||||
.formatted(
|
||||
@@ -99,8 +108,7 @@ class FileService extends BaseService<FileEntity, FileRepository> {
|
||||
Ctx.currentUser().getOrganizationId(),
|
||||
StringUtils.defaultIfBlank(dir, "public")));
|
||||
InputStream inputStream = file.getInputStream();
|
||||
fileEntity.setObjectId(
|
||||
UUID.randomUUID() + "." + FilenameUtils.getExtension(name));
|
||||
fileEntity.setObjectId(UUID.randomUUID() + "." + FilenameUtils.getExtension(name));
|
||||
try (inputStream) {
|
||||
fileEntity.setMimeType(tika.detect(inputStream));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user