|
@@ -2,6 +2,7 @@ package com.jeeplus.test.oss.service;
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.jeeplus.sys.service.dto.UserDTO;
|
|
import com.jeeplus.sys.service.dto.UserDTO;
|
|
import com.jeeplus.sys.utils.UserUtils;
|
|
import com.jeeplus.sys.utils.UserUtils;
|
|
@@ -11,15 +12,35 @@ import com.jeeplus.test.oss.service.dto.OssServiceDto;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
|
+import java.net.URL;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@Service
|
|
@Service
|
|
public class OssService {
|
|
public class OssService {
|
|
|
|
|
|
|
|
+ @Value("${config.accessory.aliyun.aliyunUrl}")
|
|
|
|
+ private String aliyunUrl;
|
|
|
|
+
|
|
|
|
+ @Value("${config.accessory.aliyun.aliyunDownloadUrl}")
|
|
|
|
+ private String aliyunDownloadUrl;
|
|
|
|
+
|
|
|
|
+ @Value("${config.accessory.aliyun.endpoint}")
|
|
|
|
+ private String endpoint;
|
|
|
|
+
|
|
|
|
+ @Value("${config.accessory.aliyun.accessKeyId}")
|
|
|
|
+ private String accessKeyId;
|
|
|
|
+
|
|
|
|
+ @Value("${config.accessory.aliyun.accessKeySecret}")
|
|
|
|
+ private String accessKeySecret;
|
|
|
|
+
|
|
|
|
+ @Value("${config.accessory.aliyun.bucketName}")
|
|
|
|
+ private String bucketName;
|
|
|
|
+
|
|
@Resource
|
|
@Resource
|
|
private OssServiceMapper ossServiceMapper;
|
|
private OssServiceMapper ossServiceMapper;
|
|
|
|
|
|
@@ -40,7 +61,7 @@ public class OssService {
|
|
log.info("删除操作完成,共删除{}条数据" , attachmentId);
|
|
log.info("删除操作完成,共删除{}条数据" , attachmentId);
|
|
}
|
|
}
|
|
log.info("开始执行保存操作,入参:{}" , JSONObject.toJSONString(dtos));
|
|
log.info("开始执行保存操作,入参:{}" , JSONObject.toJSONString(dtos));
|
|
- //TODO:获取当前登录人信息
|
|
|
|
|
|
+ //获取当前登录人信息
|
|
String id = UserUtils.getCurrentUserDTO().getId();
|
|
String id = UserUtils.getCurrentUserDTO().getId();
|
|
int i = 1;
|
|
int i = 1;
|
|
for (OssServiceDto dto : dtos) {
|
|
for (OssServiceDto dto : dtos) {
|
|
@@ -90,6 +111,7 @@ public class OssService {
|
|
List<WorkAttachment> list = ossServiceMapper.selectList(lambdaQueryWrapper);
|
|
List<WorkAttachment> list = ossServiceMapper.selectList(lambdaQueryWrapper);
|
|
//创建人和文件名称处理
|
|
//创建人和文件名称处理
|
|
if (CollectionUtil.isNotEmpty(list)) {
|
|
if (CollectionUtil.isNotEmpty(list)) {
|
|
|
|
+ temporaryUrl(list);
|
|
for (WorkAttachment workAttachment : list) {
|
|
for (WorkAttachment workAttachment : list) {
|
|
UserDTO userDTO = UserUtils.get(workAttachment.getCreateBy());
|
|
UserDTO userDTO = UserUtils.get(workAttachment.getCreateBy());
|
|
workAttachment.setCreateBy(userDTO.getName());
|
|
workAttachment.setCreateBy(userDTO.getName());
|
|
@@ -99,4 +121,39 @@ public class OssService {
|
|
log.info("文件查询结束,查询结果:{}" , JSONObject.toJSONString(list));
|
|
log.info("文件查询结束,查询结果:{}" , JSONObject.toJSONString(list));
|
|
return list;
|
|
return list;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 生成临时文件
|
|
|
|
+ * @param list
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public List<WorkAttachment> temporaryUrl(List<WorkAttachment> list) {
|
|
|
|
+ list.stream().forEach(work -> {
|
|
|
|
+ String url = aliyunUrl+work.getUrl();
|
|
|
|
+ work.setTemporaryUrl(getFileTemporaryLookUrl(url));
|
|
|
|
+ });
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 阿里云获取临时文件查看url
|
|
|
|
+ * @param url
|
|
|
|
+ */
|
|
|
|
+ public String getFileTemporaryLookUrl(String url){
|
|
|
|
+ url = url.replace("amp;","");
|
|
|
|
+ String cons = "";
|
|
|
|
+ if (url.contains(aliyunUrl)){
|
|
|
|
+ cons = aliyunUrl;
|
|
|
|
+ }else if (url.contains("http://gangwan-app.oss-cn-hangzhou.aliyuncs.com")){
|
|
|
|
+ cons = "http://gangwan-app.oss-cn-hangzhou.aliyuncs.com";
|
|
|
|
+ }else {
|
|
|
|
+ cons = aliyunDownloadUrl;
|
|
|
|
+ }
|
|
|
|
+ String key = url.split(cons+"/")[1];
|
|
|
|
+ // 指定过期时间为24小时。
|
|
|
|
+ Date expiration = new Date(new Date().getTime() + 1000 * 60 * 60 * 24 );
|
|
|
|
+ //初始化OSSClient
|
|
|
|
+ OSSClient ossClient = new OSSClient(endpoint,accessKeyId,accessKeySecret);
|
|
|
|
+ return ossClient.generatePresignedUrl(bucketName, key, expiration).toString();
|
|
|
|
+ }
|
|
}
|
|
}
|