parent
2e835ae853
commit
4d563a11cf
@ -1,22 +1,16 @@ |
|||||||
package cn.toesbieya.jxc.config; |
package cn.toesbieya.jxc.config; |
||||||
|
|
||||||
import lombok.Data; |
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
||||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
import org.springframework.context.annotation.Bean; |
||||||
import org.springframework.stereotype.Component; |
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.springframework.context.annotation.DependsOn; |
||||||
|
|
||||||
@Component |
@Configuration |
||||||
@ConfigurationProperties("qiniu") |
@EnableConfigurationProperties(QiniuProperty.class) |
||||||
@Data |
@DependsOn("redisUtil") |
||||||
public class QiniuConfig { |
public class QiniuConfig { |
||||||
private String accessKey; |
@Bean |
||||||
|
public QiniuTemplate qiniuTemplate(QiniuProperty property) { |
||||||
private String secretKey; |
return new QiniuTemplate(property); |
||||||
|
} |
||||||
private String bucket; |
|
||||||
|
|
||||||
//临时上传凭证的有效期,单位秒
|
|
||||||
private Integer tokenExpire = 3600; |
|
||||||
|
|
||||||
//临时上传凭证的redis键名
|
|
||||||
private String redisCacheKey = "qiniu-token"; |
|
||||||
} |
} |
||||||
|
|||||||
@ -0,0 +1,43 @@ |
|||||||
|
package cn.toesbieya.jxc.config; |
||||||
|
|
||||||
|
import com.qiniu.storage.Region; |
||||||
|
import lombok.Data; |
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||||
|
|
||||||
|
@Data |
||||||
|
@ConfigurationProperties("qiniu") |
||||||
|
public class QiniuProperty { |
||||||
|
private String accessKey; |
||||||
|
|
||||||
|
private String secretKey; |
||||||
|
|
||||||
|
private String bucket; |
||||||
|
|
||||||
|
//机房区域,默认为自动
|
||||||
|
private QiniuRegion region = QiniuRegion.auto; |
||||||
|
|
||||||
|
//临时上传凭证的有效期,单位秒
|
||||||
|
private Integer tokenExpire = 3600; |
||||||
|
|
||||||
|
//临时上传凭证的redis键名
|
||||||
|
private String redisCacheKey = "qiniu-token"; |
||||||
|
|
||||||
|
enum QiniuRegion { |
||||||
|
auto(Region.autoRegion()), |
||||||
|
huadong(Region.huadong()), |
||||||
|
huabei(Region.huabei()), |
||||||
|
huanan(Region.huanan()), |
||||||
|
beimei(Region.beimei()), |
||||||
|
xinjiapo(Region.xinjiapo()); |
||||||
|
|
||||||
|
private final Region region; |
||||||
|
|
||||||
|
QiniuRegion(Region region) { |
||||||
|
this.region = region; |
||||||
|
} |
||||||
|
|
||||||
|
public Region getRegion() { |
||||||
|
return this.region; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,54 @@ |
|||||||
|
package cn.toesbieya.jxc.config; |
||||||
|
|
||||||
|
import cn.toesbieya.jxc.util.RedisUtil; |
||||||
|
import com.qiniu.common.QiniuException; |
||||||
|
import com.qiniu.storage.BucketManager; |
||||||
|
import com.qiniu.storage.Configuration; |
||||||
|
import com.qiniu.util.Auth; |
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
|
||||||
|
@Slf4j |
||||||
|
public class QiniuTemplate { |
||||||
|
private final QiniuProperty property; |
||||||
|
private final Auth auth; |
||||||
|
private final BucketManager bucketManager; |
||||||
|
|
||||||
|
public QiniuTemplate(QiniuProperty property) { |
||||||
|
this.property = property; |
||||||
|
this.auth = Auth.create(property.getAccessKey(), property.getSecretKey()); |
||||||
|
this.bucketManager = new BucketManager(this.auth, new Configuration(property.getRegion().getRegion())); |
||||||
|
} |
||||||
|
|
||||||
|
public String getToken() { |
||||||
|
String key = property.getRedisCacheKey(); |
||||||
|
|
||||||
|
String token = (String) RedisUtil.get(key); |
||||||
|
|
||||||
|
if (token == null) { |
||||||
|
token = auth.uploadToken(property.getBucket()); |
||||||
|
RedisUtil.set(key, token, property.getTokenExpire()); |
||||||
|
} |
||||||
|
|
||||||
|
return token; |
||||||
|
} |
||||||
|
|
||||||
|
public void delete(String key) { |
||||||
|
try { |
||||||
|
bucketManager.delete(property.getBucket(), key); |
||||||
|
} |
||||||
|
catch (QiniuException e) { |
||||||
|
log.info("七牛云删除单个文件失败,{}", e.getMessage()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void deleteBatch(String... key) { |
||||||
|
BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations(); |
||||||
|
batchOperations.addDeleteOp(property.getBucket(), key); |
||||||
|
try { |
||||||
|
bucketManager.batch(batchOperations); |
||||||
|
} |
||||||
|
catch (QiniuException e) { |
||||||
|
log.info("七牛云批量删除文件失败,{}", e.getMessage()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,29 @@ |
|||||||
|
package cn.toesbieya.jxc.service; |
||||||
|
|
||||||
|
import cn.toesbieya.jxc.config.QiniuTemplate; |
||||||
|
import org.springframework.stereotype.Service; |
||||||
|
|
||||||
|
import javax.annotation.Resource; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
@Service |
||||||
|
public class FileService { |
||||||
|
@Resource |
||||||
|
private QiniuTemplate template; |
||||||
|
|
||||||
|
public String getToken() { |
||||||
|
return template.getToken(); |
||||||
|
} |
||||||
|
|
||||||
|
public void delete(String key) { |
||||||
|
template.delete(key); |
||||||
|
} |
||||||
|
|
||||||
|
public void deleteBatch(String... key) { |
||||||
|
template.deleteBatch(key); |
||||||
|
} |
||||||
|
|
||||||
|
public void deleteBatch(List<String> key) { |
||||||
|
deleteBatch(key.toArray(new String[0])); |
||||||
|
} |
||||||
|
} |
||||||
@ -1,4 +1,4 @@ |
|||||||
package cn.toesbieya.jxc.utils; |
package cn.toesbieya.jxc.util; |
||||||
|
|
||||||
import java.time.Instant; |
import java.time.Instant; |
||||||
import java.time.LocalDateTime; |
import java.time.LocalDateTime; |
||||||
@ -1,4 +1,4 @@ |
|||||||
package cn.toesbieya.jxc.utils; |
package cn.toesbieya.jxc.util; |
||||||
|
|
||||||
import cn.toesbieya.jxc.constant.DocConstant; |
import cn.toesbieya.jxc.constant.DocConstant; |
||||||
import cn.toesbieya.jxc.model.entity.BizDoc; |
import cn.toesbieya.jxc.model.entity.BizDoc; |
||||||
@ -1,4 +1,4 @@ |
|||||||
package cn.toesbieya.jxc.utils; |
package cn.toesbieya.jxc.util; |
||||||
|
|
||||||
import cn.toesbieya.jxc.model.vo.Result; |
import cn.toesbieya.jxc.model.vo.Result; |
||||||
import com.alibaba.excel.EasyExcel; |
import com.alibaba.excel.EasyExcel; |
||||||
@ -1,4 +1,4 @@ |
|||||||
package cn.toesbieya.jxc.utils; |
package cn.toesbieya.jxc.util; |
||||||
|
|
||||||
import com.alibaba.fastjson.JSON; |
import com.alibaba.fastjson.JSON; |
||||||
import com.alibaba.fastjson.JSONArray; |
import com.alibaba.fastjson.JSONArray; |
||||||
@ -1,4 +1,4 @@ |
|||||||
package cn.toesbieya.jxc.utils; |
package cn.toesbieya.jxc.util; |
||||||
|
|
||||||
import com.google.common.io.ByteStreams; |
import com.google.common.io.ByteStreams; |
||||||
import lombok.extern.slf4j.Slf4j; |
import lombok.extern.slf4j.Slf4j; |
||||||
@ -1,4 +1,4 @@ |
|||||||
package cn.toesbieya.jxc.utils; |
package cn.toesbieya.jxc.util; |
||||||
|
|
||||||
import cn.toesbieya.jxc.model.vo.UserVo; |
import cn.toesbieya.jxc.model.vo.UserVo; |
||||||
|
|
||||||
@ -1,4 +1,4 @@ |
|||||||
package cn.toesbieya.jxc.utils; |
package cn.toesbieya.jxc.util; |
||||||
|
|
||||||
import cn.toesbieya.jxc.model.entity.SysUser; |
import cn.toesbieya.jxc.model.entity.SysUser; |
||||||
import com.alibaba.fastjson.JSONObject; |
import com.alibaba.fastjson.JSONObject; |
||||||
@ -1,4 +1,4 @@ |
|||||||
package cn.toesbieya.jxc.utils; |
package cn.toesbieya.jxc.util; |
||||||
|
|
||||||
import org.springframework.beans.BeansException; |
import org.springframework.beans.BeansException; |
||||||
import org.springframework.context.ApplicationContext; |
import org.springframework.context.ApplicationContext; |
||||||
@ -1,4 +1,4 @@ |
|||||||
package cn.toesbieya.jxc.utils; |
package cn.toesbieya.jxc.util; |
||||||
|
|
||||||
import cn.toesbieya.jxc.model.entity.RecUserAction; |
import cn.toesbieya.jxc.model.entity.RecUserAction; |
||||||
import cn.toesbieya.jxc.model.vo.UserVo; |
import cn.toesbieya.jxc.model.vo.UserVo; |
||||||
@ -1,4 +1,4 @@ |
|||||||
package cn.toesbieya.jxc.utils; |
package cn.toesbieya.jxc.util; |
||||||
|
|
||||||
import org.springframework.util.StringUtils; |
import org.springframework.util.StringUtils; |
||||||
|
|
||||||
@ -1,4 +1,4 @@ |
|||||||
package cn.toesbieya.jxc.utils; |
package cn.toesbieya.jxc.util; |
||||||
|
|
||||||
import cn.toesbieya.jxc.constant.SocketConstant; |
import cn.toesbieya.jxc.constant.SocketConstant; |
||||||
import cn.toesbieya.jxc.model.vo.SocketEventVo; |
import cn.toesbieya.jxc.model.vo.SocketEventVo; |
||||||
@ -1,71 +0,0 @@ |
|||||||
package cn.toesbieya.jxc.utils; |
|
||||||
|
|
||||||
import cn.toesbieya.jxc.config.QiniuConfig; |
|
||||||
import com.qiniu.common.QiniuException; |
|
||||||
import com.qiniu.storage.BucketManager; |
|
||||||
import com.qiniu.storage.Configuration; |
|
||||||
import com.qiniu.storage.Region; |
|
||||||
import com.qiniu.util.Auth; |
|
||||||
import lombok.extern.slf4j.Slf4j; |
|
||||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||||
import org.springframework.context.annotation.DependsOn; |
|
||||||
import org.springframework.stereotype.Component; |
|
||||||
import org.springframework.util.StringUtils; |
|
||||||
|
|
||||||
import java.util.List; |
|
||||||
|
|
||||||
@Slf4j |
|
||||||
@Component |
|
||||||
@DependsOn("redisUtil") |
|
||||||
public class QiniuUtil { |
|
||||||
private static QiniuConfig config; |
|
||||||
private static Auth AUTH ; |
|
||||||
private static BucketManager BUCKET_MANAGER; |
|
||||||
|
|
||||||
@Autowired |
|
||||||
public QiniuUtil(QiniuConfig config) { |
|
||||||
QiniuUtil.config = config; |
|
||||||
|
|
||||||
if (StringUtils.isEmpty(config.getAccessKey())) { |
|
||||||
QiniuUtil.AUTH = null; |
|
||||||
QiniuUtil.BUCKET_MANAGER = null; |
|
||||||
return; |
|
||||||
} |
|
||||||
|
|
||||||
QiniuUtil.AUTH = Auth.create(config.getAccessKey(), config.getSecretKey()); |
|
||||||
QiniuUtil.BUCKET_MANAGER = new BucketManager(QiniuUtil.AUTH, new Configuration(Region.huadong())); |
|
||||||
} |
|
||||||
|
|
||||||
public static String getToken() { |
|
||||||
String token = (String) RedisUtil.get(config.getRedisCacheKey()); |
|
||||||
|
|
||||||
if (token == null) { |
|
||||||
token = AUTH.uploadToken(config.getBucket()); |
|
||||||
RedisUtil.set(config.getRedisCacheKey(), token, config.getTokenExpire()); |
|
||||||
} |
|
||||||
|
|
||||||
return token; |
|
||||||
} |
|
||||||
|
|
||||||
public static void delete(String key) { |
|
||||||
try { |
|
||||||
BUCKET_MANAGER.delete(config.getBucket(), key); |
|
||||||
} catch (QiniuException e) { |
|
||||||
log.info("七牛云删除单个文件失败,{}", e.getMessage()); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public static void deleteBatch(String... key) { |
|
||||||
BucketManager.BatchOperations batchOperations = new BucketManager.BatchOperations(); |
|
||||||
batchOperations.addDeleteOp(config.getBucket(), key); |
|
||||||
try { |
|
||||||
BUCKET_MANAGER.batch(batchOperations); |
|
||||||
} catch (QiniuException e) { |
|
||||||
log.info("七牛云批量删除文件失败,{}", e.getMessage()); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public static void deleteBatch(List<String> key) { |
|
||||||
deleteBatch(key.toArray(new String[0])); |
|
||||||
} |
|
||||||
} |
|
||||||
Loading…
Reference in new issue