OSS 本地服务代码优化

v1.4.1
hiparker 4 years ago
parent 40f03ba1d0
commit 534b538f6e

@ -0,0 +1,18 @@
package org.opsli.common.annotation;
import java.lang.annotation.*;
/**
*
* @author Parker
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OptionDict {
/** 参数编码 */
String value();
}

@ -31,14 +31,17 @@ public enum OptionsType {
/** 非对称加密 私钥 */
CRYPTO_ASYMMETRIC_PRIVATE_KEY("crypto_asymmetric_private_key", "加解密-非对称-私钥"),
/** 存储服务类型 */
STORAGE_TYPE("storage_type", "存储服务类型"),
;
private final String code;
private final String desc;
public static OptionsType getType(String cacheType) {
OptionsType[] var1 = values();
for (OptionsType type : var1) {
OptionsType[] types = values();
for (OptionsType type : types) {
if (type.code.equalsIgnoreCase(cacheType)) {
return type;
}

@ -66,7 +66,10 @@ public enum CoreMsg implements BaseMsg {
OTHER_EXCEPTION_LIMITER(10700,"当前系统繁忙,请稍后再试"),
OTHER_EXCEPTION_CRYPTO_EN(10702,"加密失败"),
OTHER_EXCEPTION_CRYPTO_DE(10703,"解密失败"),
OTHER_EXCEPTION_CRYPTO_REFLEX(10703,"解密反射失败"),
OTHER_EXCEPTION_CRYPTO_REFLEX(10704,"解密反射失败"),
OTHER_EXCEPTION_UTILS_INIT(10705,"系统工具类暂未初始化"),
;
private final int code;

@ -0,0 +1,72 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.opsli.core.utils;
import lombok.extern.slf4j.Slf4j;
import org.opsli.core.autoconfigure.properties.GlobalProperties;
import org.opsli.core.msg.CoreMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import static org.opsli.common.constants.OrderConstants.UTIL_ORDER;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.core.utils
* @Author: Parker
* @CreateTime: 2020-09-19 20:03
* @Description:
*/
@Slf4j
@Order(UTIL_ORDER)
@Component
@Lazy(false)
public class GlobalPropertiesUtil {
/** 系统信息 */
private static GlobalProperties GLOBAL_PROPERTIES;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/**
*
* @return GlobalProperties
*/
public static GlobalProperties getGlobalProperties(){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
return GLOBAL_PROPERTIES;
}
// ==================================
/**
*
*/
@Autowired
public void init(GlobalProperties globalProperties){
// 获得 配置信息
GlobalPropertiesUtil.GLOBAL_PROPERTIES = globalProperties;
IS_INIT = true;
}
}

@ -15,8 +15,10 @@
*/
package org.opsli.core.utils;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ReflectUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
@ -24,6 +26,7 @@ import org.apache.commons.lang3.StringUtils;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.web.system.options.OptionsApi;
import org.opsli.api.wrapper.system.options.OptionsModel;
import org.opsli.common.annotation.OptionDict;
import org.opsli.common.enums.OptionsType;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.CoreMsg;
@ -32,6 +35,7 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
@ -58,19 +62,71 @@ public class OptionsUtil {
/** 参数 Api */
private static OptionsApi optionsApi;
/** 实体类字段 */
private static final Map<Class<?>, Field[]> ENTITY_FIELD_MAP = Maps.newHashMap();
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/**
* optionsType
* @param optionsType
* @return
* @param optionsType
* @return model
*/
public static OptionsModel getOptionByCode(OptionsType optionsType){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(optionsType == null){
return null;
}
return OptionsUtil.getOptionByCode(optionsType.getCode());
}
/**
* bean
* @param beanObj
* @return T
*/
public static <T> T getOptionByBean(T beanObj){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(beanObj == null){
return null;
}
// 判断是否是Bean对象
boolean isBean = BeanUtil.isBean(beanObj.getClass());
if(!isBean){
return null;
}
// 字段缓存 减少每次更新 反射
Field[] fields = ENTITY_FIELD_MAP.get(beanObj.getClass());
if(fields == null){
fields = ReflectUtil.getFields(beanObj.getClass());
ENTITY_FIELD_MAP.put(beanObj.getClass(), fields);
}
for (Field f : fields) {
// 处理注解字段
OptionDict optionField = f.getAnnotation(OptionDict.class);
if (optionField != null) {
String optionCode = optionField.value();
// 获得配置
OptionsModel option = getOptionByCode(optionCode);
if(option != null){
BeanUtil.setProperty(beanObj, f.getName(), option.getOptionValue());
}
}
}
return beanObj;
}
/**
* optionCode
@ -78,6 +134,10 @@ public class OptionsUtil {
* @return OptionsModel
*/
public static OptionsModel getOptionByCode(String optionCode){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key
String cacheKey = PREFIX_CODE;
// 缓存Key + VALUE
@ -141,6 +201,10 @@ public class OptionsUtil {
* @return boolean
*/
public static boolean refreshOption(OptionsModel option){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(option == null || StringUtils.isEmpty(option.getOptionCode())){
return true;
}
@ -184,6 +248,10 @@ public class OptionsUtil {
* @return List
*/
public static List<OptionsModel> handleOptionsList(Map<String, Object> optionsMap){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(CollUtil.isEmpty(optionsMap)){
return null;
}
@ -204,6 +272,10 @@ public class OptionsUtil {
* @return Map
*/
public static Map<String, OptionsModel> convertOptionsMap(List<OptionsModel> optionsModels){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 这里不管有没有 都返回一个集合 防止多做一步null处理
Map<String, OptionsModel> optionsModelMap = Maps.newHashMap();
@ -218,8 +290,13 @@ public class OptionsUtil {
// =====================================
/**
*
*/
@Autowired
public void setOptionsApi(OptionsApi optionsApi) {
public void init(OptionsApi optionsApi) {
OptionsUtil.optionsApi = optionsApi;
IS_INIT = true;
}
}

@ -0,0 +1,33 @@
package org.opsli.core.utils;
import org.opsli.common.base.msg.BaseMsg;
import org.opsli.common.exception.ServiceException;
/**
*
*
* @author Parker
* @date 20215718:32:32
*/
public final class ThrowExceptionUtil {
/**
*
* @param msg
*/
public static void throwException(BaseMsg msg){
throw new ServiceException(msg);
}
/**
*
* @param msg
*/
public static void isThrowException(boolean isError, BaseMsg msg){
if(isError){
throw new ServiceException(msg);
}
}
private ThrowExceptionUtil(){}
}

@ -13,20 +13,16 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.opsli.plugins.oss.factory;
package org.opsli.plugins.oss;
import cn.hutool.core.util.ClassUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.opsli.api.wrapper.system.options.OptionsModel;
import org.opsli.common.enums.OptionsType;
import org.opsli.core.utils.OptionsUtil;
import org.opsli.plugins.oss.enums.OssConfType;
import org.opsli.plugins.oss.enums.OssStorageType;
import org.opsli.plugins.oss.service.OssStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.stereotype.Component;
import java.lang.reflect.Modifier;
import java.util.Map;
@ -40,40 +36,39 @@ import java.util.Set;
*
*/
@Slf4j
@Component
public class OssStorageFactory {
public enum OssStorageFactory {
/** Spring Bean 前缀 */
public static final String SPRING_PREFIX = "ossStorage_";
/** 实例 */
INSTANCE;
/** OSS 服务容器 */
private static final Map<OssStorageType, OssStorageService> OSS_HANDLE_MAP = Maps.newHashMap();
private final Map<OssStorageType, OssStorageService> ossStorageServiceMap = Maps.newHashMap();
/**
* OSS
* @return OssStorageService
*/
public static OssStorageService getHandle(){
public OssStorageService getHandle(){
OssStorageType storageType = null;
OptionsModel storageTypeOption = OptionsUtil.getOptionByCode(OssConfType.STORAGE_TYPE.getCode());
OptionsModel storageTypeOption = OptionsUtil.getOptionByCode(OptionsType.STORAGE_TYPE.getCode());
if(storageTypeOption != null){
storageType = OssStorageType.getType(storageTypeOption.getOptionValue());
}
return OssStorageFactory.getHandle(storageType);
return this.getHandle(storageType);
}
/**
* OSS
* @return OssStorageService
*/
public static OssStorageService getHandle(OssStorageType storageType){
OssStorageService ossStorageService = OSS_HANDLE_MAP.get(storageType);
public OssStorageService getHandle(OssStorageType storageType){
OssStorageService ossStorageService = ossStorageServiceMap.get(storageType);
if(ossStorageService == null){
return OSS_HANDLE_MAP.get(OssStorageType.LOCAL);
return ossStorageServiceMap.get(OssStorageType.LOCAL);
}
return OSS_HANDLE_MAP.get(storageType);
return ossStorageServiceMap.get(storageType);
}
@ -83,32 +78,26 @@ public class OssStorageFactory {
/**
*
*/
@Autowired
public void init(AutowireCapableBeanFactory beanFactory,
DefaultListableBeanFactory defaultListableBeanFactory){
OssStorageFactory(){
// 清空执行器集合
OSS_HANDLE_MAP.clear();
ossStorageServiceMap.clear();
// 拿到实现了 OssStorageService 接口的,所有子类
Set<Class<?>> clazzSet = ClassUtil.scanPackageBySuper(OssStorageService.class.getPackage().getName()+".impl"
, OssStorageService.class
Set<Class<?>> clazzSet = ClassUtil.scanPackageBySuper(
OssStorageService.class.getPackage().getName()+".impl",
OssStorageService.class
);
// 入参处理类
this.handleInit(beanFactory, defaultListableBeanFactory, clazzSet);
this.handleInit(clazzSet);
}
/**
*
* @param beanFactory Bean
* @param defaultListableBeanFactory BeanList
* @param clazzSet
*/
private void handleInit(AutowireCapableBeanFactory beanFactory,
DefaultListableBeanFactory defaultListableBeanFactory,
Set<Class<?>> clazzSet){
private void handleInit(Set<Class<?>> clazzSet){
for (Class<?> aClass : clazzSet) {
// 位运算 去除抽象类
if((aClass.getModifiers() & Modifier.ABSTRACT) != 0){
@ -117,18 +106,10 @@ public class OssStorageFactory {
try {
Object obj = aClass.newInstance();
OssStorageService handler = (OssStorageService) obj;
// 加入集合
OssStorageFactory.OSS_HANDLE_MAP.put(handler.getType(), handler);
// 将new出的对象放入Spring容器中
defaultListableBeanFactory.registerSingleton(SPRING_PREFIX + aClass.getSimpleName(), obj);
// 自动注入依赖
beanFactory.autowireBean(obj);
ossStorageServiceMap.put(handler.getType(), handler);
} catch (Exception e){
log.error("Oss 服务注入失败");
}

@ -1,58 +0,0 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.opsli.plugins.oss.enums;
/**
*
*
* @author Parker
*/
public enum LocalStorageType {
/** 本地存储服务 */
DOMAIN("storage_local_domain", "域名"),
PATH_PREFIX("storage_local_path_prefix", "路径前缀"),
;
private final String code;
private final String desc;
public static LocalStorageType getType(String cacheType) {
LocalStorageType[] var1 = values();
for (LocalStorageType type : var1) {
if (type.code.equalsIgnoreCase(cacheType)) {
return type;
}
}
return null;
}
public String getCode() {
return this.code;
}
public String getDesc() {
return this.desc;
}
// =================
LocalStorageType(final String code, final String desc) {
this.code = code;
this.desc = desc;
}
}

@ -1,57 +0,0 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.opsli.plugins.oss.enums;
/**
*
*
* @author Parker
*/
public enum OssConfType {
/** 存储服务类型 */
STORAGE_TYPE("storage_type", "存储服务类型"),
;
private final String code;
private final String desc;
public static OssConfType getType(String cacheType) {
OssConfType[] var1 = values();
for (OssConfType type : var1) {
if (type.code.equalsIgnoreCase(cacheType)) {
return type;
}
}
return null;
}
public String getCode() {
return this.code;
}
public String getDesc() {
return this.desc;
}
// =================
OssConfType(final String code, final String desc) {
this.code = code;
this.desc = desc;
}
}

@ -31,8 +31,8 @@ public enum OssStorageType {
private final String desc;
public static OssStorageType getType(String cacheType) {
OssStorageType[] var1 = values();
for (OssStorageType type : var1) {
OssStorageType[] types = values();
for (OssStorageType type : types) {
if (type.code.equalsIgnoreCase(cacheType)) {
return type;
}

@ -0,0 +1,18 @@
package org.opsli.plugins.oss.factory;
/**
*
*
* @author Parker
* @date 20215717:39:32
*/
public interface ConfigFactory<Conf> {
/**
*
* @return T
*/
Conf getConfig();
}

@ -0,0 +1,79 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.opsli.plugins.oss.factory;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.opsli.common.annotation.OptionDict;
import org.opsli.common.annotation.validation.ValidationArgs;
import org.opsli.common.annotation.validation.ValidationArgsLenMax;
import org.opsli.common.enums.ValiArgsType;
import org.opsli.core.utils.OptionsUtil;
import java.io.Serializable;
/**
*
*
* @author Parker
*/
public enum LocalConfigFactory implements ConfigFactory<LocalConfigFactory.LocalConfig> {
/** 实例对象 */
INSTANCE;
/**
*
* @return LocalConfig
*/
@Override
public LocalConfig getConfig() {
LocalConfig config = new LocalConfig();
// 获得缓存参数配置
OptionsUtil.getOptionByBean(config);
return config;
}
// =======================
/**
*
*
* @author Parker
*/
@Data
public static class LocalConfig implements Serializable {
private static final long serialVersionUID = 1L;
/** 域名 */
@ApiModelProperty(value = "域名")
@ValidationArgs({ValiArgsType.IS_NOT_NULL})
@ValidationArgsLenMax(100)
@OptionDict("storage_local_domain")
private String domain;
/** 前缀 */
@ApiModelProperty(value = "前缀")
@ValidationArgsLenMax(100)
@OptionDict("storage_local_path_prefix")
private String pathPrefix;
}
}

@ -20,15 +20,12 @@ import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.wrapper.system.options.OptionsModel;
import org.opsli.core.autoconfigure.properties.GlobalProperties;
import org.opsli.core.utils.OptionsUtil;
import org.opsli.core.utils.GlobalPropertiesUtil;
import org.opsli.core.utils.ValidationUtil;
import org.opsli.plugins.oss.conf.LocalConfig;
import org.opsli.plugins.oss.enums.LocalStorageType;
import org.opsli.plugins.oss.enums.OssStorageType;
import org.opsli.plugins.oss.factory.LocalConfigFactory;
import org.opsli.plugins.oss.service.BaseOssStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.File;
import java.io.IOException;
@ -47,9 +44,6 @@ public class LocalStorageServiceImpl extends BaseOssStorageService {
/** 固定路径 */
private static final String FIXED_PATH = "/static/files";
@Autowired
private GlobalProperties globalProperties;
@Override
public OssStorageType getType() {
return OssStorageType.LOCAL;
@ -57,7 +51,9 @@ public class LocalStorageServiceImpl extends BaseOssStorageService {
@Override
public String getDomain() {
return this.getConfig().getDomain();
// 获得配置信息
LocalConfigFactory.LocalConfig config = LocalConfigFactory.INSTANCE.getConfig();
return config.getDomain();
}
@Override
@ -67,8 +63,12 @@ public class LocalStorageServiceImpl extends BaseOssStorageService {
return new FileAttr();
}
// 获得系统配置信息
GlobalProperties globalProperties = GlobalPropertiesUtil.getGlobalProperties();
// 获得配置信息
LocalConfig config = this.getConfig();
LocalConfigFactory.LocalConfig config = LocalConfigFactory.INSTANCE.getConfig();
// 验证对象
ValidationUtil.verify(config);
@ -104,7 +104,6 @@ public class LocalStorageServiceImpl extends BaseOssStorageService {
fileAttr.setFileStoragePath(
config.getDomain() + packageNameByHalf + fileAttr.getName() + fileAttr.getSuffix());
// 创建文件夹
FileUtil.mkdir(packageName);
@ -123,8 +122,11 @@ public class LocalStorageServiceImpl extends BaseOssStorageService {
@Override
public FileAttr upload(InputStream inputStream, String suffix) {
// 获得系统配置信息
GlobalProperties globalProperties = GlobalPropertiesUtil.getGlobalProperties();
// 获得配置信息
LocalConfig config = this.getConfig();
LocalConfigFactory.LocalConfig config = LocalConfigFactory.INSTANCE.getConfig();
// 验证对象
ValidationUtil.verify(config);
@ -178,28 +180,4 @@ public class LocalStorageServiceImpl extends BaseOssStorageService {
return fileAttr;
}
// ===================
/**
*
* @return
*/
private LocalConfig getConfig(){
LocalConfig localConfig = new LocalConfig();
// 获得配置数据
OptionsModel domain = OptionsUtil.getOptionByCode(LocalStorageType.DOMAIN.getCode());
OptionsModel pathPrefix = OptionsUtil.getOptionByCode(LocalStorageType.PATH_PREFIX.getCode());
if(domain != null){
localConfig.setDomain(domain.getOptionValue());
}
if(pathPrefix != null){
localConfig.setPathPrefix(pathPrefix.getOptionValue());
}
return localConfig;
}
}

@ -58,7 +58,7 @@ import org.opsli.modulars.system.org.web.SysOrgRestController;
import org.opsli.modulars.system.user.entity.SysUser;
import org.opsli.modulars.system.user.entity.SysUserAndOrg;
import org.opsli.modulars.system.user.service.IUserService;
import org.opsli.plugins.oss.factory.OssStorageFactory;
import org.opsli.plugins.oss.OssStorageFactory;
import org.opsli.plugins.oss.service.BaseOssStorageService;
import org.opsli.plugins.oss.service.OssStorageService;
import org.springframework.beans.factory.annotation.Autowired;
@ -205,7 +205,7 @@ public class UserRestController extends BaseRestController<SysUser, UserModel, I
try {
// 调用OSS 服务保存头像
OssStorageService ossStorageService = OssStorageFactory.getHandle();
OssStorageService ossStorageService = OssStorageFactory.INSTANCE.getHandle();
BaseOssStorageService.FileAttr fileAttr = ossStorageService.upload(
files.get(0).getInputStream(), "jpg");

Loading…
Cancel
Save