|
@@ -0,0 +1,174 @@
|
|
|
+package com.slibra.business.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import com.qiniu.common.QiniuException;
|
|
|
+import com.qiniu.common.Zone;
|
|
|
+import com.qiniu.http.Response;
|
|
|
+import com.qiniu.storage.BucketManager;
|
|
|
+import com.qiniu.storage.Configuration;
|
|
|
+import com.qiniu.storage.UploadManager;
|
|
|
+import com.qiniu.util.Auth;
|
|
|
+import com.slibra.common.config.QiNiuYunProperties;
|
|
|
+import com.slibra.common.constant.MyConstants;
|
|
|
+import com.slibra.common.core.redis.RedisCache;
|
|
|
+import com.slibra.common.utils.StringUtils;
|
|
|
+import com.slibra.common.utils.third.StringUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+import static com.slibra.common.constant.CacheConstants.QINIUYUN_UPLOAD_KEY;
|
|
|
+import static com.slibra.common.constant.CacheConstants.QINIUYUN_UPLOAD_KEY_TIME;
|
|
|
+
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+@Component("qiNiuYunServiceNew")
|
|
|
+//@DependsOn("redisCache")
|
|
|
+public class QiNiuYunServiceImpl {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private QiNiuYunProperties qiNiuYunProperties;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisCache redisCache;
|
|
|
+
|
|
|
+ // 七牛文件上传管理器
|
|
|
+ private UploadManager uploadManager;
|
|
|
+ //上传的token
|
|
|
+ private String token;
|
|
|
+ // 七牛认证管理
|
|
|
+ private Auth auth;
|
|
|
+
|
|
|
+ private BucketManager bucketManager;
|
|
|
+
|
|
|
+ public QiNiuYunServiceImpl(QiNiuYunProperties qiNiuYunConfig) {
|
|
|
+ log.info("七牛云上传服务层初始化开始~~~");
|
|
|
+ this.qiNiuYunProperties = qiNiuYunConfig;
|
|
|
+ init();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void init() {
|
|
|
+ // 构造一个带指定Zone对象的配置类, 河北
|
|
|
+ uploadManager = new UploadManager(new Configuration(Zone.zone1()));
|
|
|
+ auth = Auth.create(qiNiuYunProperties.getAccessKey(), qiNiuYunProperties.getSecretKey());
|
|
|
+ // 根据命名空间生成的上传token
|
|
|
+ bucketManager = new BucketManager(auth, new Configuration(Zone.zone1()));
|
|
|
+// token = auth.uploadToken(qiNiuYunProperties.getBucketName());
|
|
|
+// redisCache.setCacheObject(QINIUYUN_UPLOAD_KEY, token, QINIUYUN_UPLOAD_KEY_TIME, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 上传文件
|
|
|
+ * @Param [file, key]
|
|
|
+ * @return java.lang.String
|
|
|
+ **/
|
|
|
+ public String uploadQNImg(File file) {
|
|
|
+ FileInputStream inputStream = null;
|
|
|
+ try {
|
|
|
+ // 获取文件的名称
|
|
|
+ String fileName = file.getName();
|
|
|
+ log.info("获取到的文件的名字是{}", fileName);
|
|
|
+ // 使用工具类根据上传文件生成唯一图片名称
|
|
|
+ String imgName = MyConstants.QI_NIU_YUN_UPLOAD_URL + StringUtil.getRandomImgName(fileName);
|
|
|
+// log.info("处理以后到的文件的名字是{}", imgName);
|
|
|
+ inputStream = new FileInputStream(file);
|
|
|
+ // 上传图片文件
|
|
|
+ //token默认是3600s Redis缓存处理
|
|
|
+ token = redisCache.getCacheObject(QINIUYUN_UPLOAD_KEY);
|
|
|
+ if(StringUtils.isBlank(token)){
|
|
|
+ log.info("七牛云缓存的token过期了或者是第一次初始化");
|
|
|
+ token = auth.uploadToken(qiNiuYunProperties.getBucketName());
|
|
|
+ redisCache.setCacheObject(QINIUYUN_UPLOAD_KEY, token, QINIUYUN_UPLOAD_KEY_TIME, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+ Response res = uploadManager.put(inputStream, imgName, token, null, null);
|
|
|
+// log.info("上传接口返回的结果是{}", JSON.toJSONString(res));
|
|
|
+ if (!res.isOK()) {
|
|
|
+ throw new RuntimeException("上传七牛出错,返回结果是:" + JSON.toJSONString(res));
|
|
|
+ }
|
|
|
+ // 解析上传成功的结果
|
|
|
+// DefaultPutRet putRet = new Gson().fromJson(res.bodyString(), DefaultPutRet.class);
|
|
|
+
|
|
|
+ // 直接返回外链地址
|
|
|
+ return getPublicName(imgName);
|
|
|
+ } catch (QiniuException e) {
|
|
|
+ //这里有个bug e.printStackTrace() 不打印异常信息,假如遇到token过期了,但是不会抛出异常
|
|
|
+ log.error("上次异常,异常信息为{}", e.getMessage());
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("上次异常,异常信息为{}", e.getMessage());
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getPublicName(String imgName) {
|
|
|
+ return String.format("%s/%s", this.qiNiuYunProperties.getUrl(), imgName);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取私有空间文件
|
|
|
+ *
|
|
|
+ * @param fileKey
|
|
|
+ * @return
|
|
|
+ * 2023年12月8日13:50:33 做为公共链接返回
|
|
|
+ */
|
|
|
+ public String getPrivateFile(String fileKey) {
|
|
|
+ String encodedFileName = null;
|
|
|
+// String finalUrl = null;
|
|
|
+ try {
|
|
|
+ encodedFileName = URLEncoder.encode(fileKey, "utf-8").replace("+", "%20");
|
|
|
+ return String.format("%s/%s", this.qiNiuYunProperties.getUrl(), encodedFileName);
|
|
|
+// String publicUrl = String.format("%s/%s", this.qiNiuYunProperties.getUrl(), encodedFileName);
|
|
|
+// long expireInSeconds = 3600;//1小时,可以自定义链接过期时间
|
|
|
+// finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+// return finalUrl;
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 根据空间名、文件名删除文件
|
|
|
+ * @Param [bucketName, fileKey]
|
|
|
+ * @return boolean
|
|
|
+ **/
|
|
|
+ public boolean removeFile(String bucketName, String fileKey) {
|
|
|
+ try {
|
|
|
+ bucketManager.delete(bucketName, fileKey);
|
|
|
+ } catch (QiniuException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) throws UnsupportedEncodingException {
|
|
|
+ String str = URLEncoder.encode("abc/ddd", "utf-8").replace("+", "%20");
|
|
|
+ System.out.println(String.format("%s/%s", "https://static.fuxicarbon.com", str));
|
|
|
+ System.out.println(String.format("%s/%s", "https://static.fuxicarbon.com", "abc/ddd"));
|
|
|
+
|
|
|
+ try {
|
|
|
+ int i = 5/0;
|
|
|
+ } catch (Exception e) {
|
|
|
+// throw new RuntimeException(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|