完成以下第三方接口对接:
1.地图功能-地理/逆地理编码 2.发送短信验证码 3.COS云对象存储对接 其他: 完成文件上传/预览公共接口
This commit is contained in:
parent
39953cca84
commit
cf2295b85f
3 changed files with 159 additions and 0 deletions
108
src/main/java/com/xjhs/findmemerchant/file/FileController.java
Normal file
108
src/main/java/com/xjhs/findmemerchant/file/FileController.java
Normal file
|
|
@ -0,0 +1,108 @@
|
||||||
|
package com.xjhs.findmemerchant.file;
|
||||||
|
|
||||||
|
import com.xjhs.findmemerchant.common.ApiResult;
|
||||||
|
import com.xjhs.findmemerchant.file.dao.FileMetaInfo;
|
||||||
|
import com.xjhs.findmemerchant.file.dao.FileMetaInfoRepository;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.tika.mime.MimeTypes;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 统一文件管理控制器
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@RestController
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class FileController {
|
||||||
|
|
||||||
|
|
||||||
|
@Value("${appconfig.amapKey}")
|
||||||
|
private String fileSaveRoot="./file-data";
|
||||||
|
|
||||||
|
private final FileMetaInfoRepository fileMetaInfoRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取并检查创建文件存储目录
|
||||||
|
* @return 文件存储目录带日期
|
||||||
|
*/
|
||||||
|
public Path getFileSavePath() throws IOException {
|
||||||
|
var datePath = DateTimeFormatter.ofPattern("yyyy/MM-dd").format(LocalDate.now());
|
||||||
|
var path = Path.of(this.fileSaveRoot, datePath);
|
||||||
|
if (!Files.exists(path)){
|
||||||
|
Files.createDirectories(path);
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件
|
||||||
|
*
|
||||||
|
* @param file 文件数据内容
|
||||||
|
* @return 文件描信息
|
||||||
|
*/
|
||||||
|
|
||||||
|
@PostMapping("/platform/file/upload")
|
||||||
|
public ApiResult<FileMetaInfo> uploadFile(@RequestPart(name = "file") MultipartFile file) {
|
||||||
|
try {
|
||||||
|
var ext = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
|
||||||
|
if (StringUtils.isEmpty(ext)) {
|
||||||
|
try {
|
||||||
|
ext = MimeTypes.getDefaultMimeTypes().forName(file.getContentType()).getExtension();
|
||||||
|
} catch (Exception e) {
|
||||||
|
ext = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var filePath = Path.of(this.fileSaveRoot, UUID.randomUUID() + ext);
|
||||||
|
file.transferTo(filePath);
|
||||||
|
var fileMetaInfo = new FileMetaInfo();
|
||||||
|
fileMetaInfo.setName(file.getOriginalFilename());
|
||||||
|
fileMetaInfo.setContentType(file.getContentType());
|
||||||
|
fileMetaInfo.setExtension(ext);
|
||||||
|
fileMetaInfo.setSavePath(filePath.toFile().getAbsolutePath());
|
||||||
|
fileMetaInfoRepository.save(fileMetaInfo);
|
||||||
|
return ApiResult.data(fileMetaInfo);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(),e);
|
||||||
|
return ApiResult.fail(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 预览文件
|
||||||
|
* @param fileId 文件id
|
||||||
|
* @return 文件内容
|
||||||
|
*/
|
||||||
|
@GetMapping("/platform/file/view/{fileId}")
|
||||||
|
public ResponseEntity<?> uploadFile(@PathVariable("fileId") String fileId) {
|
||||||
|
return this.fileMetaInfoRepository.findById(fileId).map(item->{
|
||||||
|
try {
|
||||||
|
var bytes = Files.readAllBytes(Paths.get(item.getSavePath()));
|
||||||
|
return ResponseEntity.ok()
|
||||||
|
.contentType(MediaType.parseMediaType(item.getContentType()))
|
||||||
|
.body(bytes);
|
||||||
|
} catch (IOException e) {
|
||||||
|
return ResponseEntity.<byte[]>internalServerError().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}).orElse(ResponseEntity.<byte[]>internalServerError().build());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
package com.xjhs.findmemerchant.file.dao;
|
||||||
|
|
||||||
|
import com.xjhs.findmemerchant.common.jpa.id.SnowflakeGenerated;
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.hibernate.annotations.Comment;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件信息表
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Entity
|
||||||
|
@Comment("文件信息表")
|
||||||
|
public class FileMetaInfo {
|
||||||
|
@Id
|
||||||
|
@SnowflakeGenerated
|
||||||
|
@Comment("主键")
|
||||||
|
private Long id;
|
||||||
|
@Comment("文件原始名称")
|
||||||
|
private String name;
|
||||||
|
@Comment("文件媒体类型")
|
||||||
|
private String contentType;
|
||||||
|
@Comment("文件扩展名称")
|
||||||
|
@Column(length = 10)
|
||||||
|
private String extension;
|
||||||
|
@Comment("本地存储路径")
|
||||||
|
private String savePath;
|
||||||
|
@Comment("是否已迁移到云服务")
|
||||||
|
private boolean moveToCloud = false;
|
||||||
|
@Comment("云对象id")
|
||||||
|
@Column(length = 50)
|
||||||
|
private String cloudObjectId;
|
||||||
|
@Comment("创建时间")
|
||||||
|
private LocalDateTime createTime = LocalDateTime.now();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.xjhs.findmemerchant.file.dao;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface FileMetaInfoRepository extends JpaRepository<FileMetaInfo,String>, JpaSpecificationExecutor<FileMetaInfo> {
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue