|
@@ -0,0 +1,135 @@
|
|
|
+package com.fjhx.file.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fjhx.file.entity.*;
|
|
|
+import com.fjhx.file.mapper.FileInfoMapper;
|
|
|
+import com.fjhx.file.service.FileInfoService;
|
|
|
+import com.obs.services.ObsClient;
|
|
|
+import com.obs.services.model.PostSignatureRequest;
|
|
|
+import com.obs.services.model.PostSignatureResponse;
|
|
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
+import com.ruoyi.common.utils.wrapper.IWrapper;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+
|
|
|
+ * <p>
|
|
|
+ * 文件表 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author zlj
|
|
|
+ * @since 2023-03-14
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> implements FileInfoService {
|
|
|
+
|
|
|
+ @Value("${obs.ak}")
|
|
|
+ private String ak;
|
|
|
+
|
|
|
+ @Value("${obs.sk}")
|
|
|
+ private String sk;
|
|
|
+
|
|
|
+ @Value("${obs.endPoint}")
|
|
|
+ private String endPoint;
|
|
|
+
|
|
|
+ @Value("${obs.url}")
|
|
|
+ private String url;
|
|
|
+
|
|
|
+ @Value("${obs.bucketName}")
|
|
|
+ private String bucketName;
|
|
|
+
|
|
|
+ @Value("${spring.profiles.active}")
|
|
|
+ private String active;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public SingVo getSing(SingDto dto) {
|
|
|
+
|
|
|
+ SingVo singVo = new SingVo();
|
|
|
+
|
|
|
+
|
|
|
+ String suffix = FileUtil.getSuffix(dto.getFileName());
|
|
|
+
|
|
|
+
|
|
|
+ String objectKey = new StringJoiner("/")
|
|
|
+ .add("byteSailing")
|
|
|
+ .add(active)
|
|
|
+ .add(DateUtil.format(new Date(), "yyyy/MM/dd"))
|
|
|
+ .add(IdUtil.fastSimpleUUID() + (ObjectUtil.isEmpty(suffix) ? "" : "." + suffix))
|
|
|
+ .toString();
|
|
|
+
|
|
|
+ ObsClient obsClient = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ obsClient = getObsClient();
|
|
|
+
|
|
|
+ long currentTimeMillis = System.currentTimeMillis() + 1000 * 10;
|
|
|
+
|
|
|
+ PostSignatureRequest request = new PostSignatureRequest(currentTimeMillis, bucketName, objectKey);
|
|
|
+ PostSignatureResponse temporarySignature = obsClient.createPostSignature(request);
|
|
|
+
|
|
|
+ Map<String, String> body = new LinkedHashMap<>();
|
|
|
+ body.put("policy", temporarySignature.getPolicy());
|
|
|
+ body.put("AccessKeyId", sk);
|
|
|
+ body.put("signature", temporarySignature.getSignature());
|
|
|
+ body.put("key", objectKey);
|
|
|
+ singVo.setUploadBody(body);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ServiceException("获取签名失败");
|
|
|
+ } finally {
|
|
|
+ IoUtil.close(obsClient);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ FileInfo fileInfo = new FileInfo();
|
|
|
+ fileInfo.setFileUrl(objectKey);
|
|
|
+ fileInfo.setFileName(dto.getFileName());
|
|
|
+ save(fileInfo);
|
|
|
+
|
|
|
+
|
|
|
+ singVo.setUploadUrl("https://" + bucketName + "." + endPoint);
|
|
|
+ singVo.setId(fileInfo.getId());
|
|
|
+ singVo.setFileName(dto.getFileName());
|
|
|
+ singVo.setFileUrl(url + objectKey);
|
|
|
+
|
|
|
+ return singVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<FileInfoVo> getList(FileInfoSelectDto dto) {
|
|
|
+
|
|
|
+ if (ObjectUtil.isEmpty(dto.getFileIdList())) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ IWrapper<Object> wrapper = IWrapper.getWrapper();
|
|
|
+ wrapper.in("fi", FileInfo::getId, dto.getFileIdList());
|
|
|
+ wrapper.eq("fi", FileInfo::getBusinessType, dto.getFileType());
|
|
|
+ List<FileInfoVo> list = this.baseMapper.getList(wrapper);
|
|
|
+
|
|
|
+ if (list.size() == 0) {
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (FileInfoVo fileInfoVo : list) {
|
|
|
+ fileInfoVo.setUrl(url + fileInfoVo.getUrl());
|
|
|
+ }
|
|
|
+
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取oss链接客户端
|
|
|
+ */
|
|
|
+ private ObsClient getObsClient() {
|
|
|
+ return new ObsClient(ak, sk, endPoint);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|