parent
cd71749aa2
commit
0c2096e36b
@ -0,0 +1,53 @@
|
||||
/**
|
||||
* 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.modulars.tools.oss;
|
||||
|
||||
|
||||
import org.opsli.common.base.msg.BaseMsg;
|
||||
|
||||
/**
|
||||
* @Author: 周鹏程
|
||||
* @CreateTime: 2020-11-04 17:34
|
||||
* @Description: OSS服务异常 - 消息
|
||||
*/
|
||||
public enum OssMsg implements BaseMsg {
|
||||
|
||||
/**
|
||||
* OSS
|
||||
*/
|
||||
|
||||
|
||||
|
||||
;
|
||||
|
||||
private final int code;
|
||||
private final String message;
|
||||
|
||||
OssMsg(int code, String message){
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getCode() {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return this.message;
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/**
|
||||
* 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.modulars.tools.oss.conf;
|
||||
|
||||
import lombok.Data;
|
||||
import org.opsli.common.annotation.validation.ValidationArgs;
|
||||
import org.opsli.common.annotation.validation.ValidationArgsLenMax;
|
||||
import org.opsli.common.enums.ValiArgsType;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 本地存储配置
|
||||
*
|
||||
* @author Parker
|
||||
*/
|
||||
@Data
|
||||
public class LocalConfig implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 域名 */
|
||||
@ValidationArgs({ValiArgsType.IS_NOT_NULL})
|
||||
@ValidationArgsLenMax(100)
|
||||
private String domain;
|
||||
|
||||
/** 前缀 */
|
||||
@ValidationArgsLenMax(100)
|
||||
private String pathPrefix;
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/**
|
||||
* 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.modulars.tools.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;
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
/**
|
||||
* 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.modulars.tools.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;
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
/**
|
||||
* 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.modulars.tools.oss.enums;
|
||||
|
||||
/**
|
||||
* 存储服务类型
|
||||
*
|
||||
* @author Parker
|
||||
*/
|
||||
public enum OssStorageType {
|
||||
|
||||
/** 存储服务类型 */
|
||||
LOCAL("local", "本地"),
|
||||
|
||||
;
|
||||
|
||||
private final String code;
|
||||
private final String desc;
|
||||
|
||||
public static OssStorageType getType(String cacheType) {
|
||||
OssStorageType[] var1 = values();
|
||||
for (OssStorageType type : var1) {
|
||||
if (type.code.equalsIgnoreCase(cacheType)) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return this.desc;
|
||||
}
|
||||
|
||||
// =================
|
||||
|
||||
OssStorageType(final String code, final String desc) {
|
||||
this.code = code;
|
||||
this.desc = desc;
|
||||
}
|
||||
}
|
@ -0,0 +1,138 @@
|
||||
/**
|
||||
* 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.modulars.tools.oss.factory;
|
||||
|
||||
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.core.utils.OptionsUtil;
|
||||
import org.opsli.modulars.tools.oss.enums.OssConfType;
|
||||
import org.opsli.modulars.tools.oss.enums.OssStorageType;
|
||||
import org.opsli.modulars.tools.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;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* OSS服务 工厂
|
||||
*
|
||||
* @author Parker
|
||||
* @date 2021年3月16日10:16:29
|
||||
*
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class OssStorageFactory {
|
||||
|
||||
/** Spring Bean 前缀 */
|
||||
public static final String SPRING_PREFIX = "ossStorage_";
|
||||
|
||||
/** OSS 服务容器 */
|
||||
private static final Map<OssStorageType, OssStorageService> OSS_HANDLE_MAP = Maps.newHashMap();
|
||||
|
||||
/**
|
||||
* 获得OSS 执行类
|
||||
* @return OssStorageService
|
||||
*/
|
||||
public static OssStorageService getHandle(){
|
||||
OssStorageType storageType = null;
|
||||
|
||||
OptionsModel storageTypeOption = OptionsUtil.getOptionByCode(OssConfType.STORAGE_TYPE.getCode());
|
||||
if(storageTypeOption != null){
|
||||
storageType = OssStorageType.getType(storageTypeOption.getOptionValue());
|
||||
}
|
||||
|
||||
return OssStorageFactory.getHandle(storageType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得OSS 执行类
|
||||
* @return OssStorageService
|
||||
*/
|
||||
public static OssStorageService getHandle(OssStorageType storageType){
|
||||
OssStorageService ossStorageService = OSS_HANDLE_MAP.get(storageType);
|
||||
if(ossStorageService == null){
|
||||
return OSS_HANDLE_MAP.get(OssStorageType.LOCAL);
|
||||
}
|
||||
return OSS_HANDLE_MAP.get(storageType);
|
||||
}
|
||||
|
||||
|
||||
// ============================
|
||||
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
@Autowired
|
||||
public void init(AutowireCapableBeanFactory beanFactory,
|
||||
DefaultListableBeanFactory defaultListableBeanFactory){
|
||||
|
||||
// 清空执行器集合
|
||||
OSS_HANDLE_MAP.clear();
|
||||
|
||||
// 拿到实现了 OssStorageService 接口的,所有子类
|
||||
Set<Class<?>> clazzSet = ClassUtil.scanPackageBySuper(OssStorageService.class.getPackage().getName()+".impl"
|
||||
, OssStorageService.class
|
||||
);
|
||||
|
||||
|
||||
// 入参处理类
|
||||
this.handleInit(beanFactory, defaultListableBeanFactory, clazzSet);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理类
|
||||
* @param beanFactory Bean工程
|
||||
* @param defaultListableBeanFactory 默认BeanList工厂
|
||||
* @param clazzSet 扫描处理类
|
||||
*/
|
||||
private void handleInit(AutowireCapableBeanFactory beanFactory,
|
||||
DefaultListableBeanFactory defaultListableBeanFactory,
|
||||
Set<Class<?>> clazzSet){
|
||||
for (Class<?> aClass : clazzSet) {
|
||||
// 位运算 去除抽象类
|
||||
if((aClass.getModifiers() & Modifier.ABSTRACT) != 0){
|
||||
continue;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
} catch (Exception e){
|
||||
log.error("Oss 服务注入失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,170 @@
|
||||
/**
|
||||
* 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.modulars.tools.oss.service;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import lombok.Data;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 云存储 (支持本地、七牛、阿里云、腾讯云、又拍云)
|
||||
*
|
||||
* @author Parker
|
||||
* @date 2021年4月30日16:12:45
|
||||
*/
|
||||
public abstract class BaseOssStorageService implements OssStorageService {
|
||||
|
||||
/** 文件夹前缀 */
|
||||
private static final String FOLDER_PREFIX = "/";
|
||||
private static final String FOLDER_WINDOWS_PREFIX = "\\";
|
||||
|
||||
/**
|
||||
* 获得文件信息
|
||||
* @param file 文件
|
||||
* @return FileAttr
|
||||
*/
|
||||
protected FileAttr getFileAttr(File file){
|
||||
FileAttr fileAttr = new FileAttr();
|
||||
fileAttr.setName(FileUtil.getName(file));
|
||||
fileAttr.setSize(FileUtil.size(file));
|
||||
fileAttr.setPrefix(FileUtil.getPrefix(file));
|
||||
fileAttr.setSuffix(FileUtil.getSuffix(file));
|
||||
fileAttr.setNameAndSuffix(fileAttr.getName()+"."+fileAttr.getSuffix());
|
||||
// 生成随机 文件名称
|
||||
// 当前时间戳 + 文件名 hash + 文件大小 + 5位随机码
|
||||
|
||||
// 当前时间戳
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
// 文件名 HashCode
|
||||
int fileNameHashCode = fileAttr.getName().hashCode();
|
||||
// 随机字符串
|
||||
String randomString = RandomUtil.randomStringUpper(5);
|
||||
// 随机文件名
|
||||
String randomFileName = currentTimeMillis + fileNameHashCode + fileAttr.getSize() + randomString;
|
||||
|
||||
fileAttr.setRandomFileName(randomFileName);
|
||||
fileAttr.setRandomFileNameAndSuffix(randomFileName+"."+fileAttr.getSuffix());
|
||||
|
||||
return fileAttr;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获得文件信息
|
||||
* @param inputStream 文件
|
||||
* @param suffix 后缀
|
||||
* @return FileAttr
|
||||
*/
|
||||
protected FileAttr getFileAttr(InputStream inputStream, String suffix){
|
||||
FileAttr fileAttr = new FileAttr();
|
||||
fileAttr.setSuffix(suffix);
|
||||
|
||||
// 生成随机 文件名称
|
||||
// 当前时间戳 + 文件名 hash + 5位随机数字码 + 5位随机码
|
||||
|
||||
// 当前时间戳
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
// 文件名 HashCode
|
||||
int fileNameHashCode = inputStream.hashCode();
|
||||
// 随机字符串
|
||||
String randomString = RandomUtil.randomStringUpper(5);
|
||||
// 随机数
|
||||
String randomNumbers = RandomUtil.randomNumbers(5);
|
||||
// 随机文件名
|
||||
String randomFileName = currentTimeMillis + fileNameHashCode + randomNumbers + randomString;
|
||||
|
||||
fileAttr.setRandomFileName(randomFileName);
|
||||
fileAttr.setRandomFileNameAndSuffix(randomFileName+"."+fileAttr.getSuffix());
|
||||
|
||||
return fileAttr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理 Path 路径
|
||||
* @param path 路径
|
||||
* @return String
|
||||
*/
|
||||
protected String handlePath(String path){
|
||||
return this.handlePath(path, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理 Path 路径
|
||||
* @param path 路径
|
||||
* @return String
|
||||
*/
|
||||
protected String handlePath(String path, boolean isHandleFirst){
|
||||
if(StringUtils.isEmpty(path)){
|
||||
return path;
|
||||
}
|
||||
|
||||
if(isHandleFirst){
|
||||
// 如果 第一位不是 / 则加 /
|
||||
char firstChar = path.charAt(0);
|
||||
if(!StringUtils.equals(FOLDER_PREFIX, Convert.toStr(firstChar))){
|
||||
path = FOLDER_PREFIX + path;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果最后一位 是 / 则减 /
|
||||
char lastChar = path.charAt(path.length()-1);
|
||||
if(path.length() > 1 &&
|
||||
StringUtils.equals(FOLDER_PREFIX, Convert.toStr(lastChar))){
|
||||
path = StringUtils.substring(path, 0, path.length()-1);
|
||||
}
|
||||
|
||||
// 针对 windows 特殊路径 进行统一翻转
|
||||
return StringUtils.replace(path, FOLDER_WINDOWS_PREFIX, FOLDER_PREFIX);
|
||||
}
|
||||
|
||||
|
||||
// =============================
|
||||
|
||||
@Data
|
||||
public static class FileAttr{
|
||||
|
||||
/** 大小 */
|
||||
private long size;
|
||||
|
||||
/** 文件名 */
|
||||
private String name;
|
||||
|
||||
/** 文件名 包含后缀 */
|
||||
private String nameAndSuffix;
|
||||
|
||||
/** 前缀 */
|
||||
private String prefix;
|
||||
|
||||
/** 后缀 */
|
||||
private String suffix;
|
||||
|
||||
/** 随机唯一文件名 */
|
||||
private String randomFileName;
|
||||
|
||||
/** 随机唯一文件名 包含后缀 */
|
||||
private String randomFileNameAndSuffix;
|
||||
|
||||
/** 文件存储 */
|
||||
private String fileStoragePath;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
/**
|
||||
* 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.modulars.tools.oss.service;
|
||||
|
||||
|
||||
import org.opsli.modulars.tools.oss.enums.OssStorageType;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 云存储 (支持本地、七牛、阿里云、腾讯云、又拍云)
|
||||
*
|
||||
* @author Parker
|
||||
* @date 2021年4月30日16:12:45
|
||||
*/
|
||||
public interface OssStorageService {
|
||||
|
||||
/**
|
||||
* 获得存储服务类型
|
||||
* @return type
|
||||
*/
|
||||
OssStorageType getType();
|
||||
|
||||
/**
|
||||
* 获得域名
|
||||
* @return String
|
||||
*/
|
||||
String getDomain();
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param file 文件
|
||||
* @return 返回文件信息
|
||||
*/
|
||||
BaseOssStorageService.FileAttr upload(File file);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param inputStream 输入流
|
||||
* @param suffix 后缀
|
||||
* @return 返回文件信息
|
||||
*/
|
||||
BaseOssStorageService.FileAttr upload(InputStream inputStream, String suffix);
|
||||
|
||||
}
|
@ -0,0 +1,205 @@
|
||||
/**
|
||||
* 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.modulars.tools.oss.service.impl;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
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.ValidationUtil;
|
||||
import org.opsli.modulars.tools.oss.conf.LocalConfig;
|
||||
import org.opsli.modulars.tools.oss.enums.LocalStorageType;
|
||||
import org.opsli.modulars.tools.oss.enums.OssStorageType;
|
||||
import org.opsli.modulars.tools.oss.service.BaseOssStorageService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 本地文件上传
|
||||
*
|
||||
* @author Parker
|
||||
* @date 2021年4月30日14:09:08
|
||||
*/
|
||||
@Slf4j
|
||||
public class LocalStorageServiceImpl extends BaseOssStorageService {
|
||||
|
||||
/** 固定路径 */
|
||||
private static final String FIXED_PATH = "/static/files";
|
||||
|
||||
@Autowired
|
||||
private GlobalProperties globalProperties;
|
||||
|
||||
@Override
|
||||
public OssStorageType getType() {
|
||||
return OssStorageType.LOCAL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDomain() {
|
||||
return this.getConfig().getDomain();
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileAttr upload(File file) {
|
||||
// 非空判断
|
||||
if(FileUtil.isEmpty(file)){
|
||||
return new FileAttr();
|
||||
}
|
||||
|
||||
// 获得配置信息
|
||||
LocalConfig config = this.getConfig();
|
||||
// 验证对象
|
||||
ValidationUtil.verify(config);
|
||||
|
||||
// 当前时间戳
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
|
||||
// 当前时间
|
||||
Date currDate = DateUtil.date(currentTimeMillis);
|
||||
|
||||
// 静态路径
|
||||
String genPath = globalProperties != null && globalProperties.getWeb() != null &&
|
||||
StringUtils.isNotEmpty(globalProperties.getWeb().getUploadPath())
|
||||
? globalProperties.getWeb().getUploadPath()
|
||||
: FileUtil.getUserHomePath();
|
||||
|
||||
// 静态路径前缀 默认为空
|
||||
String pathPrefix = StringUtils.isNotEmpty(config.getPathPrefix())?config.getPathPrefix():"";
|
||||
|
||||
// 文件夹名称
|
||||
String folderName = DateUtil.format(currDate, "yyyyMMdd");
|
||||
|
||||
// 包 全名称
|
||||
String packageName = super.handlePath(genPath, false)
|
||||
+ super.handlePath(FIXED_PATH) + super.handlePath(pathPrefix)
|
||||
+ super.handlePath(folderName);
|
||||
// 包 半名称
|
||||
String packageNameByHalf = super.handlePath(FIXED_PATH) + super.handlePath(pathPrefix)
|
||||
+ super.handlePath(folderName);
|
||||
|
||||
// 文件属性
|
||||
FileAttr fileAttr = super.getFileAttr(file);
|
||||
// 设置文件路径
|
||||
fileAttr.setFileStoragePath(
|
||||
config.getDomain() + packageNameByHalf + fileAttr.getName() + fileAttr.getSuffix());
|
||||
|
||||
|
||||
// 创建文件夹
|
||||
FileUtil.mkdir(packageName);
|
||||
|
||||
// 创建文件
|
||||
File tmpFile = new File(
|
||||
packageName + super.handlePath(fileAttr.getRandomFileNameAndSuffix()));
|
||||
try {
|
||||
FileUtils.copyFile(file, tmpFile);
|
||||
}catch (IOException e){
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
FileUtil.touch(tmpFile);
|
||||
|
||||
return fileAttr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileAttr upload(InputStream inputStream, String suffix) {
|
||||
// 获得配置信息
|
||||
LocalConfig config = this.getConfig();
|
||||
// 验证对象
|
||||
ValidationUtil.verify(config);
|
||||
|
||||
// 当前时间戳
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
|
||||
// 当前时间
|
||||
Date currDate = DateUtil.date(currentTimeMillis);
|
||||
|
||||
// 静态路径
|
||||
String genPath = globalProperties != null && globalProperties.getWeb() != null &&
|
||||
StringUtils.isNotEmpty(globalProperties.getWeb().getUploadPath())
|
||||
? globalProperties.getWeb().getUploadPath()
|
||||
: FileUtil.getUserHomePath();
|
||||
|
||||
// 静态路径前缀 默认为空
|
||||
String pathPrefix = StringUtils.isNotEmpty(config.getPathPrefix())?config.getPathPrefix():"";
|
||||
|
||||
// 文件夹名称
|
||||
String folderName = DateUtil.format(currDate, "yyyyMMdd");
|
||||
|
||||
// 包 全名称
|
||||
String packageName = super.handlePath(genPath, false)
|
||||
+ super.handlePath(FIXED_PATH) + super.handlePath(pathPrefix)
|
||||
+ super.handlePath(folderName);
|
||||
// 包 半名称
|
||||
String packageNameByHalf = super.handlePath(FIXED_PATH) + super.handlePath(pathPrefix)
|
||||
+ super.handlePath(folderName);
|
||||
|
||||
// 文件属性
|
||||
FileAttr fileAttr = super.getFileAttr(inputStream, suffix);
|
||||
|
||||
// 设置文件路径
|
||||
fileAttr.setFileStoragePath(
|
||||
config.getDomain() + packageNameByHalf
|
||||
+ super.handlePath(fileAttr.getRandomFileNameAndSuffix()));
|
||||
|
||||
// 创建文件夹
|
||||
FileUtil.mkdir(packageName);
|
||||
|
||||
// 创建文件
|
||||
File tmpFile = new File(
|
||||
packageName + super.handlePath(fileAttr.getRandomFileNameAndSuffix()));
|
||||
try {
|
||||
FileUtils.copyInputStreamToFile(inputStream, tmpFile);
|
||||
}catch (IOException e){
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
FileUtil.touch(tmpFile);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue