Skip to content

Commit

Permalink
feat: upload file
Browse files Browse the repository at this point in the history
  • Loading branch information
lichong-a committed Aug 20, 2024
1 parent 50329e7 commit af2bfc7
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.funcode.portal.server.common.core.base.http.handler;

import io.jsonwebtoken.JwtException;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.ValidationException;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -84,6 +85,19 @@ public ResponseResult<BusinessException> processBusinessException(BusinessExcept
return ResponseResult.fail(businessException.getLocalizedMessage());
}

/**
* handle Jwt exception.
*
* @param jwtException business exception
* @return ResponseResult
*/
@ResponseBody
@ExceptionHandler(JwtException.class)
public ResponseResult<JwtException> processBusinessException(JwtException jwtException) {
log.error(jwtException.getLocalizedMessage(), jwtException);
return ResponseResult.fail(jwtException.getLocalizedMessage());
}

/**
* handle other exception.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class Storage extends BaseEntity {
@Comment("唯一标识")
private String key;

@Column(length = 200, nullable = false)
@Column(length = 200)
@Comment("版本ID")
private String versionId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
import org.funcode.portal.server.module.ielts.storage.domain.vo.StorageQueryVo;
import org.funcode.portal.server.module.ielts.storage.service.IStorageService;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
* @author 李冲
Expand All @@ -33,9 +39,9 @@ public class StorageController {
private final IStorageService storageService;

@Operation(summary = "上传文件")
@PostMapping("upload")
@PostMapping(value = "upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@PreAuthorize(value = "hasAuthority('ielts:storage:upload')")
public ResponseResult<Boolean> upload(@Valid @RequestBody StorageAddOrEditVo storageAddOrEditVo) {
public ResponseResult<Storage> upload(@Valid StorageAddOrEditVo storageAddOrEditVo) {
return ResponseResult.success(storageService.upload(storageAddOrEditVo));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class StorageAddOrEditVo {
@Schema(description = "存储标题")
private String title;

@Size(max = 10, message = "{ielts.domain.vo.StorageAddOrEditVo.fileType.Size}")
@Schema(description = "文件类型(0:图片;1:音频;2:视频;3:markdown)")
private int fileType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface IStorageService extends IBaseService<Storage, Long> {
* @param storageAddOrEditVo 参数
* @return 上传结果
*/
Boolean upload(StorageAddOrEditVo storageAddOrEditVo);
Storage upload(StorageAddOrEditVo storageAddOrEditVo);

/**
* 删除文件
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ public IStorageRepository getBaseRepository() {

@Override
@Transactional
public Boolean upload(StorageAddOrEditVo storageAddOrEditVo) {
public Storage upload(StorageAddOrEditVo storageAddOrEditVo) {
File localFile = null;
try {
File localFile = FileUtils.multipartFileToFile(storageAddOrEditVo.getFile());
localFile = FileUtils.multipartFileToFile(storageAddOrEditVo.getFile());
// 设置高级接口的配置项
// 分块上传阈值和分块大小分别设置为 5MB 和 1MB(若不特殊设置,分块上传阈值和分块大小的默认值均为5MB)
TransferManagerConfiguration transferManagerConfiguration = new TransferManagerConfiguration();
Expand Down Expand Up @@ -100,12 +101,15 @@ public Boolean upload(StorageAddOrEditVo storageAddOrEditVo) {
.fileType(storageAddOrEditVo.getFileType())
.title(storageAddOrEditVo.getTitle())
.build();
getBaseRepository().save(storage);
return getBaseRepository().save(storage);
} catch (Exception e) {
log.error("上传文件失败,IO异常", e);
throw new BusinessException("上传文件失败");
} finally {
if (localFile != null) {
FileUtils.delteTempFile(localFile);
}
}
return true;
}

@Override
Expand Down

0 comments on commit af2bfc7

Please sign in to comment.