first commit

This commit is contained in:
cys841515238
2023-03-02 16:13:28 +08:00
commit 2733a60b97
741 changed files with 76931 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
/**
* Copyright (c) 2016-2019 人人开源 All rights reserved.
*
* https://www.renren.io
*
* 版权所有,侵权必究!
*/
package com.peanut.modules.oss.cloud;
import com.aliyun.oss.OSSClient;
import com.peanut.common.exception.RRException;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
/**
* 阿里云存储
*
* @author Mark sunlightcs@gmail.com
*/
public class AliyunCloudStorageService extends CloudStorageService {
private OSSClient client;
public AliyunCloudStorageService(CloudStorageConfig config){
this.config = config;
//初始化
init();
}
private void init(){
client = new OSSClient(config.getAliyunEndPoint(), config.getAliyunAccessKeyId(),
config.getAliyunAccessKeySecret());
}
@Override
public String upload(byte[] data, String path) {
return upload(new ByteArrayInputStream(data), path);
}
@Override
public String upload(InputStream inputStream, String path) {
try {
client.putObject(config.getAliyunBucketName(), path, inputStream);
} catch (Exception e){
throw new RRException("上传文件失败,请检查配置信息", e);
}
return config.getAliyunDomain() + "/" + path;
}
@Override
public String uploadSuffix(byte[] data, String suffix) {
return upload(data, getPath(config.getAliyunPrefix(), suffix));
}
@Override
public String uploadSuffix(InputStream inputStream, String suffix) {
return upload(inputStream, getPath(config.getAliyunPrefix(), suffix));
}
}

View File

@@ -0,0 +1,94 @@
/**
* Copyright (c) 2016-2019 人人开源 All rights reserved.
*
* https://www.renren.io
*
* 版权所有,侵权必究!
*/
package com.peanut.modules.oss.cloud;
import com.peanut.common.validator.group.AliyunGroup;
import com.peanut.common.validator.group.QcloudGroup;
import com.peanut.common.validator.group.QiniuGroup;
import lombok.Data;
import org.hibernate.validator.constraints.Range;
import org.hibernate.validator.constraints.URL;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* 云存储配置信息
*
* @author Mark sunlightcs@gmail.com
*/
@Data
public class CloudStorageConfig implements Serializable {
private static final long serialVersionUID = 1L;
//类型 1七牛 2阿里云 3腾讯云
@Range(min=1, max=3, message = "类型错误")
private Integer type;
//七牛绑定的域名
@NotBlank(message="七牛绑定的域名不能为空", groups = QiniuGroup.class)
@URL(message = "七牛绑定的域名格式不正确", groups = QiniuGroup.class)
private String qiniuDomain;
//七牛路径前缀
private String qiniuPrefix;
//七牛ACCESS_KEY
@NotBlank(message="七牛AccessKey不能为空", groups = QiniuGroup.class)
private String qiniuAccessKey;
//七牛SECRET_KEY
@NotBlank(message="七牛SecretKey不能为空", groups = QiniuGroup.class)
private String qiniuSecretKey;
//七牛存储空间名
@NotBlank(message="七牛空间名不能为空", groups = QiniuGroup.class)
private String qiniuBucketName;
//阿里云绑定的域名
@NotBlank(message="阿里云绑定的域名不能为空", groups = AliyunGroup.class)
@URL(message = "阿里云绑定的域名格式不正确", groups = AliyunGroup.class)
private String aliyunDomain;
//阿里云路径前缀
private String aliyunPrefix;
//阿里云EndPoint
@NotBlank(message="阿里云EndPoint不能为空", groups = AliyunGroup.class)
private String aliyunEndPoint;
//阿里云AccessKeyId
@NotBlank(message="阿里云AccessKeyId不能为空", groups = AliyunGroup.class)
private String aliyunAccessKeyId;
//阿里云AccessKeySecret
@NotBlank(message="阿里云AccessKeySecret不能为空", groups = AliyunGroup.class)
private String aliyunAccessKeySecret;
//阿里云BucketName
@NotBlank(message="阿里云BucketName不能为空", groups = AliyunGroup.class)
private String aliyunBucketName;
//腾讯云绑定的域名
@NotBlank(message="腾讯云绑定的域名不能为空", groups = QcloudGroup.class)
@URL(message = "腾讯云绑定的域名格式不正确", groups = QcloudGroup.class)
private String qcloudDomain;
//腾讯云路径前缀
private String qcloudPrefix;
//腾讯云AppId
@NotNull(message="腾讯云AppId不能为空", groups = QcloudGroup.class)
private Integer qcloudAppId;
//腾讯云SecretId
@NotBlank(message="腾讯云SecretId不能为空", groups = QcloudGroup.class)
private String qcloudSecretId;
//腾讯云SecretKey
@NotBlank(message="腾讯云SecretKey不能为空", groups = QcloudGroup.class)
private String qcloudSecretKey;
//腾讯云BucketName
@NotBlank(message="腾讯云BucketName不能为空", groups = QcloudGroup.class)
private String qcloudBucketName;
//腾讯云COS所属地区
@NotBlank(message="所属地区不能为空", groups = QcloudGroup.class)
private String qcloudRegion;
}

View File

@@ -0,0 +1,78 @@
/**
* Copyright (c) 2016-2019 人人开源 All rights reserved.
*
* https://www.renren.io
*
* 版权所有,侵权必究!
*/
package com.peanut.modules.oss.cloud;
import com.peanut.common.utils.DateUtils;
import org.apache.commons.lang.StringUtils;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;
/**
* 云存储(支持七牛、阿里云、腾讯云、又拍云)
*
* @author Mark sunlightcs@gmail.com
*/
public abstract class CloudStorageService {
/** 云存储配置信息 */
CloudStorageConfig config;
/**
* 文件路径
* @param prefix 前缀
* @param suffix 后缀
* @return 返回上传路径
*/
public String getPath(String prefix, String suffix) {
//生成uuid
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
//文件路径
String path = DateUtils.format(new Date(), "yyyyMMdd") + "/" + uuid;
if(StringUtils.isNotBlank(prefix)){
path = prefix + "/" + path;
}
return path + suffix;
}
/**
* 文件上传
* @param data 文件字节数组
* @param path 文件路径,包含文件名
* @return 返回http地址
*/
public abstract String upload(byte[] data, String path);
/**
* 文件上传
* @param data 文件字节数组
* @param suffix 后缀
* @return 返回http地址
*/
public abstract String uploadSuffix(byte[] data, String suffix);
/**
* 文件上传
* @param inputStream 字节流
* @param path 文件路径,包含文件名
* @return 返回http地址
*/
public abstract String upload(InputStream inputStream, String path);
/**
* 文件上传
* @param inputStream 字节流
* @param suffix 后缀
* @return 返回http地址
*/
public abstract String uploadSuffix(InputStream inputStream, String suffix);
}

View File

@@ -0,0 +1,44 @@
/**
* Copyright (c) 2016-2019 人人开源 All rights reserved.
*
* https://www.renren.io
*
* 版权所有,侵权必究!
*/
package com.peanut.modules.oss.cloud;
import com.peanut.common.utils.ConfigConstant;
import com.peanut.common.utils.Constant;
import com.peanut.common.utils.SpringContextUtils;
import com.peanut.modules.sys.service.SysConfigService;
/**
* 文件上传Factory
*
* @author Mark sunlightcs@gmail.com
*/
public final class OSSFactory {
private static SysConfigService sysConfigService;
static {
OSSFactory.sysConfigService = (SysConfigService) SpringContextUtils.getBean("sysConfigService");
}
public static CloudStorageService build(){
//获取云存储配置信息
CloudStorageConfig config = sysConfigService.getConfigObject(ConfigConstant.CLOUD_STORAGE_CONFIG_KEY, CloudStorageConfig.class);
if(config.getType() == Constant.CloudService.QINIU.getValue()){
return new QiniuCloudStorageService(config);
}else if(config.getType() == Constant.CloudService.ALIYUN.getValue()){
return new AliyunCloudStorageService(config);
}else if(config.getType() == Constant.CloudService.QCLOUD.getValue()){
return new QcloudCloudStorageService(config);
}
return null;
}
}

View File

@@ -0,0 +1,88 @@
/**
* Copyright (c) 2016-2019 人人开源 All rights reserved.
*
* https://www.renren.io
*
* 版权所有,侵权必究!
*/
package com.peanut.modules.oss.cloud;
import com.alibaba.fastjson.JSONObject;
import com.peanut.common.exception.RRException;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.request.UploadFileRequest;
import com.qcloud.cos.sign.Credentials;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
/**
* 腾讯云存储
*
* @author Mark sunlightcs@gmail.com
*/
public class QcloudCloudStorageService extends CloudStorageService {
private COSClient client;
public QcloudCloudStorageService(CloudStorageConfig config){
this.config = config;
//初始化
init();
}
private void init(){
Credentials credentials = new Credentials(config.getQcloudAppId(), config.getQcloudSecretId(),
config.getQcloudSecretKey());
//初始化客户端配置
ClientConfig clientConfig = new ClientConfig();
//设置bucket所在的区域华南gz 华北tj 华东sh
clientConfig.setRegion(config.getQcloudRegion());
client = new COSClient(clientConfig, credentials);
}
@Override
public String upload(byte[] data, String path) {
//腾讯云必需要以"/"开头
if(!path.startsWith("/")) {
path = "/" + path;
}
//上传到腾讯云
UploadFileRequest request = new UploadFileRequest(config.getQcloudBucketName(), path, data);
String response = client.uploadFile(request);
JSONObject jsonObject = JSONObject.parseObject(response);
if(jsonObject.getInteger("code") != 0) {
throw new RRException("文件上传失败," + jsonObject.getString("message"));
}
return config.getQcloudDomain() + path;
}
@Override
public String upload(InputStream inputStream, String path) {
try {
byte[] data = IOUtils.toByteArray(inputStream);
return this.upload(data, path);
} catch (IOException e) {
throw new RRException("上传文件失败", e);
}
}
@Override
public String uploadSuffix(byte[] data, String suffix) {
return upload(data, getPath(config.getQcloudPrefix(), suffix));
}
@Override
public String uploadSuffix(InputStream inputStream, String suffix) {
return upload(inputStream, getPath(config.getQcloudPrefix(), suffix));
}
}

View File

@@ -0,0 +1,77 @@
/**
* Copyright (c) 2016-2019 人人开源 All rights reserved.
*
* https://www.renren.io
*
* 版权所有,侵权必究!
*/
package com.peanut.modules.oss.cloud;
import com.peanut.common.exception.RRException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
/**
* 七牛云存储
*
* @author Mark sunlightcs@gmail.com
*/
public class QiniuCloudStorageService extends CloudStorageService {
private UploadManager uploadManager;
private String token;
public QiniuCloudStorageService(CloudStorageConfig config){
this.config = config;
//初始化
init();
}
private void init(){
uploadManager = new UploadManager(new Configuration(Zone.autoZone()));
token = Auth.create(config.getQiniuAccessKey(), config.getQiniuSecretKey()).
uploadToken(config.getQiniuBucketName());
}
@Override
public String upload(byte[] data, String path) {
try {
Response res = uploadManager.put(data, path, token);
if (!res.isOK()) {
throw new RuntimeException("上传七牛出错:" + res.toString());
}
} catch (Exception e) {
throw new RRException("上传文件失败,请核对七牛配置信息", e);
}
return config.getQiniuDomain() + "/" + path;
}
@Override
public String upload(InputStream inputStream, String path) {
try {
byte[] data = IOUtils.toByteArray(inputStream);
return this.upload(data, path);
} catch (IOException e) {
throw new RRException("上传文件失败", e);
}
}
@Override
public String uploadSuffix(byte[] data, String suffix) {
return upload(data, getPath(config.getQiniuPrefix(), suffix));
}
@Override
public String uploadSuffix(InputStream inputStream, String suffix) {
return upload(inputStream, getPath(config.getQiniuPrefix(), suffix));
}
}