mirror of
https://github.com/BgaSol/sol-cloud
synced 2026-05-24 01:28:51 +00:00
feat(file): Optimize file upload logic
This commit is contained in:
parent
a5e2932b6a
commit
8489b9e8c0
4 changed files with 114 additions and 24 deletions
|
|
@ -27,7 +27,8 @@ public class FileCreateDto extends BaseCreateDto<FileEntity> {
|
|||
|
||||
@Override
|
||||
public FileEntity toEntity() {
|
||||
throw new UnsupportedOperationException("方法未实现");
|
||||
FileEntity fileEntity = new FileEntity();
|
||||
return super.toEntity(fileEntity);
|
||||
}
|
||||
|
||||
public FileCreateDto(String fileName, byte[] bytes, String mediaType) {
|
||||
|
|
|
|||
|
|
@ -3,8 +3,17 @@ package com.bgasol.model.file.file.dto;
|
|||
import com.bgasol.common.core.base.dto.BaseUpdateDto;
|
||||
import com.bgasol.model.file.file.entity.FileEntity;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.*;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
|
|
@ -12,8 +21,59 @@ import lombok.experimental.SuperBuilder;
|
|||
@NoArgsConstructor
|
||||
@Schema(description = "更新文件")
|
||||
public class FileUpdateDto extends BaseUpdateDto<FileEntity> {
|
||||
@Schema(description = "要上传的文件块")
|
||||
@NotNull(message = "上传文件不能为空")
|
||||
private MultipartFile uploadFile;
|
||||
|
||||
@Override
|
||||
public FileEntity toEntity() {
|
||||
throw new UnsupportedOperationException("Not implemented yet");
|
||||
FileEntity fileEntity = new FileEntity();
|
||||
return super.toEntity(fileEntity);
|
||||
}
|
||||
|
||||
public FileUpdateDto(String fileName, byte[] bytes, String mediaType) {
|
||||
super();
|
||||
this.uploadFile = new MultipartFile() {
|
||||
@Override
|
||||
public String getName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getOriginalFilename() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentType() {
|
||||
return mediaType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return bytes.length == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getSize() {
|
||||
return bytes.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getBytes() throws IOException {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return new ByteArrayResource(bytes).getInputStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transferTo(File dest) throws IllegalStateException {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ package com.bgasol.web.file.file.controller;
|
|||
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import com.bgasol.common.core.base.controller.BaseController;
|
||||
import com.bgasol.common.core.base.exception.BaseException;
|
||||
import com.bgasol.common.core.base.vo.BaseVo;
|
||||
import com.bgasol.common.core.base.vo.PageVo;
|
||||
import com.bgasol.model.file.file.dto.FileCreateDto;
|
||||
|
|
@ -22,7 +21,6 @@ import org.springframework.http.MediaType;
|
|||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
|
|
@ -58,12 +56,16 @@ public class FileController extends BaseController<
|
|||
@Operation(summary = "保存|上传文件", operationId = "saveFile")
|
||||
@SaCheckPermission("file:save")
|
||||
public BaseVo<FileEntity> save(@Valid FileCreateDto fileCreateDto) {
|
||||
FileEntity save = null;
|
||||
try {
|
||||
save = fileService.save(fileCreateDto.getUploadFile());
|
||||
} catch (IOException e) {
|
||||
throw new BaseException("文件上传失败");
|
||||
}
|
||||
FileEntity save = fileService.save(fileCreateDto.getUploadFile(), fileCreateDto.toEntity());
|
||||
return BaseVo.success(save, "文件上传成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
@PostMapping
|
||||
@Operation(summary = "更新|上传文件", operationId = "updateFile")
|
||||
@SaCheckPermission("file:update")
|
||||
public BaseVo<FileEntity> update(FileUpdateDto fileUpdateDto) {
|
||||
FileEntity save = fileService.update(fileUpdateDto.getUploadFile(), fileUpdateDto.toEntity());
|
||||
return BaseVo.success(save, "文件上传成功");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -75,13 +75,42 @@ public class FileService extends BaseService<FileEntity, FilePageDto> {
|
|||
|
||||
/**
|
||||
* 通过文件流保存文件
|
||||
*
|
||||
* @param multipartFile 文件对象
|
||||
* @return 文件实体
|
||||
*/
|
||||
public FileEntity save(MultipartFile multipartFile) throws IOException {
|
||||
// 初始化文件实体
|
||||
FileEntity fileEntity = new FileEntity();
|
||||
public FileEntity save(MultipartFile multipartFile, FileEntity fileEntity) {
|
||||
if (ObjectUtils.isEmpty(multipartFile)) {
|
||||
return this.save(fileEntity);
|
||||
}
|
||||
// 获取文件元数据
|
||||
getFileMateDate(multipartFile, fileEntity);
|
||||
// 保存文件实体
|
||||
fileEntity = this.save(fileEntity);
|
||||
// 上传文件
|
||||
try (InputStream inputStream = multipartFile.getInputStream()) {
|
||||
ossService.writeFileStream(fileEntity.getBucket(), fileEntity.getId(), fileEntity.getName(), inputStream, fileEntity.getSize(), fileEntity.getType());
|
||||
} catch (IOException e) {
|
||||
throw new BaseException("上传文件失败");
|
||||
}
|
||||
return fileEntity;
|
||||
}
|
||||
|
||||
public FileEntity update(MultipartFile multipartFile, FileEntity fileEntity) {
|
||||
if (ObjectUtils.isEmpty(multipartFile)) {
|
||||
return this.update(fileEntity);
|
||||
}
|
||||
// 获取文件元数据
|
||||
getFileMateDate(multipartFile, fileEntity);
|
||||
// 更新文件实体
|
||||
fileEntity = this.update(fileEntity);
|
||||
// 上传文件
|
||||
try (InputStream inputStream = multipartFile.getInputStream()) {
|
||||
ossService.writeFileStream(fileEntity.getBucket(), fileEntity.getId(), fileEntity.getName(), inputStream, fileEntity.getSize(), fileEntity.getType());
|
||||
} catch (IOException e) {
|
||||
throw new BaseException("上传文件失败");
|
||||
}
|
||||
return fileEntity;
|
||||
}
|
||||
|
||||
public void getFileMateDate(MultipartFile multipartFile, FileEntity fileEntity) {
|
||||
fileEntity.setName(multipartFile.getOriginalFilename());
|
||||
fileEntity.setSize(multipartFile.getSize());
|
||||
fileEntity.setType(multipartFile.getContentType());
|
||||
|
|
@ -89,13 +118,11 @@ public class FileService extends BaseService<FileEntity, FilePageDto> {
|
|||
// 获取文件后缀
|
||||
fileEntity.setSuffix(this.getSuffix(fileEntity.getName()));
|
||||
// 获取文件HASH
|
||||
fileEntity.setHash(this.getFileHash(multipartFile.getInputStream()));
|
||||
|
||||
// 保存文件实体
|
||||
fileEntity = this.save(fileEntity);
|
||||
// 上传文件
|
||||
ossService.writeFileStream(fileEntity.getBucket(), fileEntity.getId(), fileEntity.getName(), multipartFile.getInputStream(), fileEntity.getSize(), fileEntity.getType());
|
||||
return fileEntity;
|
||||
try (InputStream inputStream = multipartFile.getInputStream()) {
|
||||
fileEntity.setHash(this.getFileHash(inputStream));
|
||||
} catch (IOException e) {
|
||||
throw new BaseException("获取文件HASH失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in a new issue