111
This commit is contained in:
1 parent
81eca8933a
commit
345f906c35
540 files changed
+494222
-90
No files matched your search
@@ -0,0 +1,14 @@
|
||||
package com.luqj.qiniu.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.luqj.qiniu.entity.QiniuConfig;
|
||||
|
||||
/**
|
||||
* @Description: qiniu_config
|
||||
* @Author: jeecg-boot
|
||||
* @Date: 2020-08-15
|
||||
* @Version: V1.0
|
||||
*/
|
||||
public interface IQiniuConfigService extends IService<QiniuConfig> {
|
||||
QiniuConfig getFirstOne();
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.luqj.qiniu.service;
|
||||
|
||||
import com.luqj.qiniu.entity.QiNiu;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
public interface IQiniuService {
|
||||
QiNiu buildToken() throws Exception;
|
||||
|
||||
void refreshQiniuToken();
|
||||
|
||||
String upload(String localFilePath, String key);
|
||||
|
||||
String upload(MultipartFile file, String fileDir);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.luqj.qiniu.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.luqj.qiniu.entity.QiniuConfig;
|
||||
import com.luqj.qiniu.mapper.QiniuConfigMapper;
|
||||
import com.luqj.qiniu.service.IQiniuConfigService;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @Description: qiniu_config
|
||||
* @Author: jeecg-boot
|
||||
* @Date: 2020-08-15
|
||||
* @Version: V1.0
|
||||
*/
|
||||
@Service
|
||||
public class QiniuConfigServiceImpl extends ServiceImpl<QiniuConfigMapper, QiniuConfig> implements IQiniuConfigService {
|
||||
@Cacheable(value = "sys:qiniu:config", key = "'one'")
|
||||
@Override
|
||||
public QiniuConfig getFirstOne() {
|
||||
LambdaQueryWrapper queryWrapper = new QueryWrapper<QiniuConfig>().lambda();
|
||||
QiniuConfig qiniuConfig = getOne(queryWrapper);
|
||||
return qiniuConfig;
|
||||
}
|
||||
|
||||
@CacheEvict(value = "sys:qiniu:config", key = "'one'")
|
||||
@Override
|
||||
public boolean save(QiniuConfig entity) {
|
||||
return super.save(entity);
|
||||
}
|
||||
|
||||
@CacheEvict(value = "sys:qiniu:config", key = "'one'")
|
||||
@Override
|
||||
public boolean updateById(QiniuConfig entity) {
|
||||
return super.updateById(entity);
|
||||
}
|
||||
|
||||
@CacheEvict(value = "sys:qiniu:config", key = "'one'")
|
||||
@Override
|
||||
public boolean removeByIds(Collection<? extends Serializable> idList) {
|
||||
return super.removeByIds(idList);
|
||||
}
|
||||
|
||||
@CacheEvict(value = "sys:qiniu:config", key = "'one'")
|
||||
@Override
|
||||
public boolean removeById(Serializable id) {
|
||||
return super.removeById(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package com.luqj.qiniu.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.google.gson.Gson;
|
||||
import com.luqj.qiniu.entity.QiNiu;
|
||||
import com.luqj.qiniu.entity.QiniuConfig;
|
||||
import com.luqj.qiniu.service.IQiniuConfigService;
|
||||
import com.luqj.qiniu.service.IQiniuService;
|
||||
import com.qiniu.common.QiniuException;
|
||||
import com.qiniu.common.Zone;
|
||||
import com.qiniu.http.Response;
|
||||
import com.qiniu.storage.Configuration;
|
||||
import com.qiniu.storage.UploadManager;
|
||||
import com.qiniu.storage.model.DefaultPutRet;
|
||||
import com.qiniu.util.Auth;
|
||||
import com.qiniu.util.StringMap;
|
||||
import com.ruoyi.common.CacheConstant;
|
||||
import com.ruoyi.common.redis.RedisUtil;
|
||||
import com.ruoyi.common.utils.CommonUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Service
|
||||
public class QiniuServiceImpl implements IQiniuService {
|
||||
private final static Logger logger = LoggerFactory.getLogger(QiniuServiceImpl.class);
|
||||
@Autowired
|
||||
private IQiniuConfigService qiniuConfigService;
|
||||
@Autowired
|
||||
private RedisUtil redisUtil;
|
||||
@Value("${lu.admin.qiniu.addr:http://img.hy-vip.cn}")
|
||||
private String qiniuAddr;
|
||||
|
||||
@Override
|
||||
public QiNiu buildToken() throws Exception {
|
||||
LambdaQueryWrapper queryWrapper = new QueryWrapper<QiniuConfig>().lambda();
|
||||
QiniuConfig qiniuConfig = qiniuConfigService.getOne(queryWrapper);
|
||||
if (null == qiniuConfig) {
|
||||
throw new Exception("请配置七牛存储配置");
|
||||
}
|
||||
QiNiu qiNiu = new QiNiu();
|
||||
|
||||
qiNiu.setKey(UUID.randomUUID().toString().replaceAll("\\-", ""));
|
||||
String token = redisUtil.get(CacheConstant.QI_NIU_TOKEN, "");
|
||||
qiNiu.setToken(token);
|
||||
qiNiu.setZone(qiNiu.getZone());
|
||||
return qiNiu;
|
||||
}
|
||||
|
||||
public String upload(MultipartFile file, String fileDir) {
|
||||
Configuration cfg = new Configuration(new Zone.Builder().autoZone());
|
||||
UploadManager uploadManager = new UploadManager(cfg);
|
||||
StringBuilder fileUrl = new StringBuilder();
|
||||
// 获取文件名
|
||||
String orgName = file.getOriginalFilename();
|
||||
orgName = CommonUtils.getFileName(orgName);
|
||||
String fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.indexOf("."));
|
||||
if (!fileDir.endsWith("/")) {
|
||||
fileDir = fileDir.concat("/");
|
||||
}
|
||||
fileDir = fileDir.replace("/", "/");
|
||||
fileUrl = fileUrl.append(fileDir + fileName);
|
||||
|
||||
String upToken = redisUtil.get(CacheConstant.QI_NIU_TOKEN, "");
|
||||
|
||||
try {
|
||||
Response response = uploadManager.put(file.getBytes(), fileUrl.toString(), upToken);
|
||||
//解析上传成功的结果
|
||||
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
|
||||
System.out.println(putRet.key);
|
||||
System.out.println(putRet.hash);
|
||||
return qiniuAddr + File.separator + putRet.key;
|
||||
} catch (QiniuException ex) {
|
||||
ex.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
public void refreshQiniuToken() {
|
||||
QiniuConfig qiniuConfig = qiniuConfigService.getFirstOne();
|
||||
if (null == qiniuConfig) {
|
||||
logger.error("请配置七牛存储配置");
|
||||
return;
|
||||
}
|
||||
String accessKey = qiniuConfig.getAccessKey();
|
||||
String secretKey = qiniuConfig.getSecretKey();
|
||||
String bucket = qiniuConfig.getBucket();
|
||||
long expireSeconds = qiniuConfig.getTokenExpireSeconds(); //过期时间
|
||||
if (!redisUtil.hasKey(CacheConstant.QI_NIU_TOKEN, "")) {
|
||||
StringMap putPolicy = new StringMap();
|
||||
Auth auth = Auth.create(accessKey, secretKey);
|
||||
String upToken = auth.uploadToken(bucket, null, expireSeconds, putPolicy);
|
||||
redisUtil.set(CacheConstant.QI_NIU_TOKEN, "", upToken, expireSeconds, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String upload(String localFilePath, String key) {
|
||||
//构造一个带指定 Region
|
||||
Configuration cfg = new Configuration(new Zone.Builder().autoZone());
|
||||
UploadManager uploadManager = new UploadManager(cfg);
|
||||
try {
|
||||
String[] strings = localFilePath.split("\\.");
|
||||
Response response = uploadManager.put(localFilePath, key + "." + strings[strings.length - 1], redisUtil.get(CacheConstant.QI_NIU_TOKEN, ""));
|
||||
//解析上传成功的结果
|
||||
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
|
||||
return qiniuAddr + File.separator + putRet.key;
|
||||
} catch (QiniuException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user