Skip to content

Commit

Permalink
兼容MINIO
Browse files Browse the repository at this point in the history
  • Loading branch information
chopper711 committed Jun 7, 2022
1 parent 519a608 commit a22ccbd
Show file tree
Hide file tree
Showing 11 changed files with 320 additions and 60 deletions.
4 changes: 2 additions & 2 deletions buyer-api/src/test/java/cn/lili/buyer/test/cart/FileTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.lili.buyer.test.cart;


import cn.lili.modules.file.plugin.FileManagerPlugin;
import cn.lili.modules.file.plugin.FilePlugin;
import cn.lili.modules.goods.entity.dos.Brand;
import cn.lili.modules.goods.service.BrandService;
import com.xkcoding.http.util.StringUtil;
Expand All @@ -27,7 +27,7 @@ class FileTest {


@Autowired
private FileManagerPlugin fileManagerPlugin;
private FilePlugin fileManagerPlugin;

@Autowired
private BrandService brandService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import cn.lili.common.utils.CommonUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.file.entity.File;
import cn.lili.modules.file.plugin.FileManagerPlugin;
import cn.lili.modules.file.plugin.FilePlugin;
import cn.lili.modules.file.plugin.FilePluginFactory;
import cn.lili.modules.file.service.FileService;
import cn.lili.modules.system.entity.dos.Setting;
import cn.lili.modules.system.entity.enums.SettingEnum;
Expand Down Expand Up @@ -47,7 +48,7 @@ public class UploadController {
@Autowired
private SettingService settingService;
@Autowired
private FileManagerPlugin fileManagerPlugin;
private FilePluginFactory filePluginFactory;
@Autowired
private Cache cache;

Expand Down Expand Up @@ -86,7 +87,7 @@ public ResultMessage<Object> upload(MultipartFile file,
try {
InputStream inputStream = file.getInputStream();
//上传至第三方云服务或服务器
result = fileManagerPlugin.inputStreamUpload(inputStream, fileKey);
result = filePluginFactory.filePlugin().inputStreamUpload(inputStream, fileKey);
//保存数据信息至数据库
newFile.setName(file.getOriginalFilename());
newFile.setFileSize(file.getSize());
Expand Down
7 changes: 7 additions & 0 deletions framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,13 @@
</exclusions>
</dependency>

<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>${minio.version}</version>
</dependency>


</dependencies>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cn.lili.modules.file.entity.enums;

import com.aliyun.oss.OSS;

/**
* OssEnum
*
* @author Chopper
* @version v1.0
* 2022-06-06 11:23
*/
public enum OssEnum {
/**
*
*/
ALI_OSS, MINIO;
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package cn.lili.modules.file.plugin;

import cn.lili.modules.file.entity.enums.OssEnum;

import java.io.InputStream;
import java.util.List;

/**
* 文件管理插件
* 文件插件接口
*
* @author Chopper
*/
public interface FileManagerPlugin {
public interface FilePlugin {


/**
* 插件名称
*/
OssEnum pluginName();

/**
* 文件路径上传
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cn.lili.modules.file.plugin;

import cn.hutool.json.JSONUtil;
import cn.lili.common.exception.ServiceException;
import cn.lili.modules.file.entity.enums.OssEnum;
import cn.lili.modules.file.plugin.impl.AliFilePlugin;
import cn.lili.modules.file.plugin.impl.MinioFilePlugin;
import cn.lili.modules.system.entity.dos.Setting;
import cn.lili.modules.system.entity.dto.OssSetting;
import cn.lili.modules.system.entity.enums.SettingEnum;
import cn.lili.modules.system.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
* 文件服务抽象工厂 直接返回操作类
*
* @author Chopper
* @version v1.0
* 2022-06-06 11:35
*/
@Component
public class FilePluginFactory {


@Autowired
private SettingService settingService;


/**
* 获取oss client
*
* @return
*/
public FilePlugin filePlugin() {

OssSetting ossSetting = null;
try {
Setting setting = settingService.get(SettingEnum.OSS_SETTING.name());

ossSetting = JSONUtil.toBean(setting.getSettingValue(), OssSetting.class);


switch (OssEnum.valueOf(ossSetting.getType())) {

case MINIO:
return new MinioFilePlugin(ossSetting);
case ALI_OSS:
return new AliFilePlugin(ossSetting);
default:
throw new ServiceException();
}
} catch (Exception e) {
throw new ServiceException();
}
}


}
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package cn.lili.modules.file.plugin.impl;

import cn.hutool.core.util.StrUtil;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.modules.file.plugin.FileManagerPlugin;
import cn.lili.modules.system.entity.dos.Setting;
import cn.lili.modules.file.entity.enums.OssEnum;
import cn.lili.modules.file.plugin.FilePlugin;
import cn.lili.modules.system.entity.dto.OssSetting;
import cn.lili.modules.system.entity.enums.SettingEnum;
import cn.lili.modules.system.service.SettingService;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.ObjectMetadata;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;
Expand All @@ -29,69 +23,39 @@
* @author Chopper
*/

@Component
@Slf4j
public class AliFileManagerPlugin implements FileManagerPlugin {
public class AliFilePlugin implements FilePlugin {

@Autowired
private SettingService settingService;
private OssSetting ossSetting;

/**
* 下一个初始化配置参数的时间
* 这里为了防止多次调用redis,减少与redis的交互时间
*/
private static Long nextInitSetting;

/**
* 暂时设定3分账请求一次设置
*/
private static final Long INTERVAL = 60 * 3 * 1000L;
public AliFilePlugin(OssSetting ossSetting) {
this.ossSetting = ossSetting;
}

/**
* 静态设置,最快三分钟更新一次
*/
private static OssSetting ossSetting;
@Override
public OssEnum pluginName() {
return OssEnum.ALI_OSS;
}

/**
* 获取oss client
*
* @return
*/
private OSS getOssClient() {
OssSetting ossSetting = getSetting();

return new OSSClientBuilder().build(
ossSetting.getEndPoint(),
ossSetting.getAccessKeyId(),
ossSetting.getAccessKeySecret());
}

/**
* 获取配置
*
* @return
*/
private OssSetting getSetting() {
//如果没有配置,或者没有下次刷新时间,或者下次刷新时间小于当前时间,则从redis 更新一次
if (ossSetting == null || nextInitSetting == null || nextInitSetting < System.currentTimeMillis()) {
Setting setting = settingService.get(SettingEnum.OSS_SETTING.name());
if (setting == null || StrUtil.isBlank(setting.getSettingValue())) {
throw new ServiceException(ResultCode.OSS_NOT_EXIST);
}
nextInitSetting = System.currentTimeMillis() + INTERVAL;
ossSetting = new Gson().fromJson(setting.getSettingValue(), OssSetting.class);
return ossSetting;
}
return ossSetting;
}

/**
* 获取配置前缀
*
* @return
*/
private String getUrlPrefix() {
OssSetting ossSetting = getSetting();
return "https://" + ossSetting.getBucketName() + "." + ossSetting.getEndPoint() + "/";
}

Expand Down Expand Up @@ -130,7 +94,7 @@ public String inputStreamUpload(InputStream inputStream, String key) {
try {
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType("image/jpg");
ossClient.putObject(getSetting().getBucketName(), key, inputStream, meta);
ossClient.putObject(ossSetting.getBucketName(), key, inputStream, meta);
} catch (OSSException oe) {
log.error("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
Expand Down Expand Up @@ -161,7 +125,7 @@ public void deleteFile(List<String> key) {

try {
ossClient.deleteObjects(
new DeleteObjectsRequest(getSetting().getBucketName()).withKeys(key));
new DeleteObjectsRequest(ossSetting.getBucketName()).withKeys(key));
} catch (OSSException oe) {
log.error("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
Expand Down
Loading

0 comments on commit a22ccbd

Please sign in to comment.