优化文件OSS
This commit is contained in:
@@ -1,36 +1,36 @@
|
||||
package org.jiayunet.constant;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Oss桶
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum OssBucketEnum {
|
||||
|
||||
|
||||
HuiDongNi("oss-cn-guangzhou.aliyuncs.com", "jiayupet", 1,"https://jiayupet.oss-cn-guangzhou.aliyuncs.com");
|
||||
|
||||
/**
|
||||
* 服务端口
|
||||
*/
|
||||
private final String endpoint;
|
||||
|
||||
/**
|
||||
* 桶名
|
||||
*/
|
||||
private final String bucketName;
|
||||
|
||||
/**
|
||||
* 权限 0 私有 1公共读 2 公共读写
|
||||
*/
|
||||
private final int power;
|
||||
|
||||
/**
|
||||
*
|
||||
* 域名
|
||||
*/
|
||||
private final String domain;
|
||||
|
||||
OssBucketEnum(String endpoint, String bucketName, int power, String domain) {
|
||||
this.endpoint = endpoint;
|
||||
this.bucketName = bucketName;
|
||||
this.power = power;
|
||||
this.domain = domain;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.jiayunet.constant;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -9,27 +10,31 @@ import java.util.UUID;
|
||||
/**
|
||||
* oss 文件路径
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum OssPathEnum {
|
||||
UserImage("image/user", "用户图片"),
|
||||
ResumeImage("image/resume", "简历图片"),
|
||||
SystemImage("image/system", "系统图片"),
|
||||
ResumeFile("file/resume", "简历文件"),
|
||||
|
||||
;
|
||||
UserImage("image/user", "用户图片",OssBucketEnum.HuiDongNi),
|
||||
ResumeImage("image/resume", "简历图片",OssBucketEnum.HuiDongNi),
|
||||
SystemImage("image/system", "系统图片",OssBucketEnum.HuiDongNi),
|
||||
ResumeFile("file/resume", "简历文件",OssBucketEnum.HuiDongNi),
|
||||
OtherFile("other","其他文件",OssBucketEnum.HuiDongNi);
|
||||
|
||||
|
||||
/**
|
||||
* 文件存放目录
|
||||
*/
|
||||
private final String catalogue;
|
||||
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
private final String comment;
|
||||
|
||||
OssPathEnum(String catalogue, String comment) {
|
||||
this.catalogue = catalogue;
|
||||
this.comment = comment;
|
||||
}
|
||||
/**
|
||||
* 文件桶
|
||||
*/
|
||||
private final OssBucketEnum ossBucketEnum;
|
||||
|
||||
/**
|
||||
* 获取文件路径
|
||||
|
||||
@@ -4,13 +4,17 @@ package org.jiayunet.controller;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.jiayunet.constant.OssPathEnum;
|
||||
import org.jiayunet.pojo.vo.OssUrlVo;
|
||||
import org.jiayunet.server.OssServer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* oss控制类
|
||||
*/
|
||||
@@ -21,10 +25,33 @@ import org.jiayunet.server.OssServer;
|
||||
public class OssController {
|
||||
private OssServer ossServer;
|
||||
|
||||
|
||||
/**
|
||||
* 获取授权上传地址
|
||||
* @param fileName 文件名
|
||||
* @param pathEnum 文件地址枚举
|
||||
* @return 地址相关
|
||||
*/
|
||||
@GetMapping("/requestUploadUrl")
|
||||
public OssUrlVo requestUploadUrl(@RequestParam String fileName, @RequestParam OssPathEnum pathEnum){
|
||||
return ossServer.requestUploadUrl(fileName,pathEnum);
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单上传
|
||||
* @param file 文件
|
||||
* @param pathEnum 地址枚举
|
||||
* @return 下载地址
|
||||
*/
|
||||
@PostMapping("/simpleUpload")
|
||||
public OssUrlVo simpleUpload(@RequestParam("file") MultipartFile file, @RequestParam OssPathEnum pathEnum) {
|
||||
|
||||
try {
|
||||
return ossServer.simpleUpload(file.getOriginalFilename(), pathEnum, file.getInputStream());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,27 +40,31 @@ public class OssServer {
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public OssUrlVo requestUploadUrl(String fileName, OssPathEnum pathEnum) {
|
||||
|
||||
Assert.notNull(pathEnum, " 上传路径不能为空");
|
||||
Assert.hasText(fileName, "文件不能为空");
|
||||
Assert.isTrue(fileName.lastIndexOf(".") >= 0, "文件名必须包含类型");
|
||||
|
||||
OssBucketEnum jiaYuPet = OssBucketEnum.HuiDongNi;
|
||||
OssBucketEnum bucketEnum = pathEnum.getOssBucketEnum();
|
||||
|
||||
Long userId = UserSecurityTool.getUserId();
|
||||
String type = fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||
|
||||
String path = pathEnum.getPath(fileName);
|
||||
|
||||
|
||||
OssUrlVo ossUrlVo = new OssUrlVo();
|
||||
|
||||
try {
|
||||
|
||||
//保存上传信息
|
||||
ossFileMapper.insert(new OssFile(null, jiaYuPet.getBucketName(), type, path, fileName, userId, 0, Instant.now(), null));
|
||||
ossFileMapper.insert(new OssFile(null, bucketEnum.getBucketName(), type, path, fileName, userId, 0, Instant.now(), null));
|
||||
|
||||
//获取上传Url
|
||||
String uploadUrl = aliOssAbility.signatureUrl(jiaYuPet.getEndpoint(), jiaYuPet.getBucketName(), path, 60, HttpMethod.PUT);
|
||||
String uploadUrl = aliOssAbility.signatureUrl(bucketEnum.getEndpoint(), bucketEnum.getBucketName(), path, 60, HttpMethod.PUT);
|
||||
|
||||
//组装下载地址
|
||||
String downloadUrl = jiaYuPet.getDomain() + "/" + path;
|
||||
String downloadUrl = bucketEnum.getDomain() + "/" + path;
|
||||
|
||||
ossUrlVo.setUploadUrl(uploadUrl);
|
||||
ossUrlVo.setDownloadUrl(downloadUrl);
|
||||
@@ -73,6 +77,9 @@ public class OssServer {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public OssUrlVo simpleUpload(String fileName, OssPathEnum pathEnum, InputStream inputStream) {
|
||||
Assert.notNull(pathEnum, " 上传路径不能为空");
|
||||
Assert.hasText(fileName, "文件不能为空");
|
||||
@@ -108,4 +115,8 @@ public class OssServer {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user