|
|
@@ -1,24 +1,19 @@
|
|
|
package cn.reghao.tnb.file.app.controller;
|
|
|
|
|
|
import cn.reghao.jutil.jdk.result.WebBody;
|
|
|
-import cn.reghao.tnb.file.app.db.repository.FileRepository;
|
|
|
-import cn.reghao.tnb.file.app.model.dto.UploadFile;
|
|
|
+import cn.reghao.tnb.file.app.db.mapper.FileUserMapper;
|
|
|
import cn.reghao.tnb.file.app.model.dto.UploadedFile;
|
|
|
import cn.reghao.tnb.file.app.model.vo.UploadFileRet;
|
|
|
import cn.reghao.tnb.file.app.service.FileUploadService;
|
|
|
-import cn.reghao.tnb.file.app.service.SpiderFileService;
|
|
|
-import cn.reghao.tnb.file.app.service.media.ImageFileService;
|
|
|
-import cn.reghao.tnb.file.app.service.media.VideoFileService;
|
|
|
-import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.http.MediaType;
|
|
|
-import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.validation.constraints.NotNull;
|
|
|
import java.io.InputStream;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
@@ -30,36 +25,31 @@ import java.io.InputStream;
|
|
|
@Deprecated
|
|
|
public class SpiderFileController {
|
|
|
private final FileUploadService fileUploadService;
|
|
|
- private ImageFileService imageService;
|
|
|
- private VideoFileService videoService;
|
|
|
+ private final FileUserMapper fileUserMapper;
|
|
|
|
|
|
- public SpiderFileController(FileUploadService fileUploadService, ImageFileService imageService,
|
|
|
- VideoFileService videoService) {
|
|
|
+ public SpiderFileController(FileUploadService fileUploadService, FileUserMapper fileUserMapper) {
|
|
|
this.fileUploadService = fileUploadService;
|
|
|
- this.imageService = imageService;
|
|
|
- this.videoService = videoService;
|
|
|
+ this.fileUserMapper = fileUserMapper;
|
|
|
}
|
|
|
|
|
|
- @ApiOperation(value = "上传图片文件")
|
|
|
- @PostMapping(value = "/image", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
- public String uploadImageFile(@NotNull MultipartFile file, String userId) throws Exception {
|
|
|
+ @ApiOperation(value = "上传文件")
|
|
|
+ @PostMapping(value = "/file", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
+ public String uploadFile(@NotNull MultipartFile file, String username) throws Exception {
|
|
|
String filename = file.getOriginalFilename();
|
|
|
long size = file.getSize();
|
|
|
- String contentType = file.getContentType();
|
|
|
+ String contentType = Objects.requireNonNull(file.getContentType());
|
|
|
InputStream inputStream = file.getInputStream();
|
|
|
- if (contentType == null || !contentType.startsWith("image")) {
|
|
|
- return WebBody.failWithMsg("content-type 错误");
|
|
|
- }
|
|
|
|
|
|
- UploadedFile uploadedFile = new UploadedFile(filename, size, contentType, inputStream);
|
|
|
- UploadFileRet uploadFileRet = fileUploadService.put(uploadedFile);
|
|
|
- return WebBody.success(uploadFileRet);
|
|
|
- }
|
|
|
+ if (contentType.startsWith("image") || contentType.startsWith("video")) {
|
|
|
+ String userId = fileUserMapper.findUserIdByUsername(username);
|
|
|
+ if (userId == null) {
|
|
|
+ return WebBody.failWithMsg(username + " 不存在");
|
|
|
+ }
|
|
|
|
|
|
- @ApiOperation(value = "上传视频文件")
|
|
|
- @PostMapping(value = "/video", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
- public String uploadVideoFile(@Validated UploadFile uploadFile) throws Exception {
|
|
|
- log.info("视频文件");
|
|
|
- return WebBody.success();
|
|
|
+ UploadedFile uploadedFile = new UploadedFile(filename, size, contentType, inputStream);
|
|
|
+ UploadFileRet uploadFileRet = fileUploadService.put(uploadedFile, userId);
|
|
|
+ return WebBody.success(uploadFileRet);
|
|
|
+ }
|
|
|
+ return WebBody.failWithMsg("content-type 错误");
|
|
|
}
|
|
|
}
|