|
|
@@ -0,0 +1,80 @@
|
|
|
+package com.ruoyi.file.service;
|
|
|
+
|
|
|
+import com.alibaba.nacos.common.utils.IoUtils;
|
|
|
+import com.obs.services.ObsClient;
|
|
|
+import com.obs.services.model.ObsObject;
|
|
|
+import com.obs.services.model.PutObjectResult;
|
|
|
+import com.ruoyi.common.core.exception.ServiceException;
|
|
|
+import com.ruoyi.common.redis.service.RedisService;
|
|
|
+import com.ruoyi.file.config.MinioConfig;
|
|
|
+import com.ruoyi.file.config.OBSConfig;
|
|
|
+import com.ruoyi.file.constant.RedisCacheConstants;
|
|
|
+import com.ruoyi.file.utils.FileUploadUtils;
|
|
|
+import io.minio.MinioClient;
|
|
|
+import io.minio.PutObjectArgs;
|
|
|
+import org.apache.poi.util.IOUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Primary;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.Base64;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Minio 文件存储
|
|
|
+ *
|
|
|
+ * @author lydgt
|
|
|
+ */
|
|
|
+@Primary
|
|
|
+@Service
|
|
|
+public class OBSSysFileServiceImpl implements ISysFileService
|
|
|
+{
|
|
|
+ @Autowired
|
|
|
+ private OBSConfig obsConfig;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisService redisService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Minio文件上传接口
|
|
|
+ *
|
|
|
+ * @param file 上传的文件
|
|
|
+ * @return 访问地址
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String uploadFile(MultipartFile file) throws Exception {
|
|
|
+ InputStream inputStream = null;
|
|
|
+ try {
|
|
|
+ String fileName = FileUploadUtils.extractFilename(file);
|
|
|
+ inputStream = file.getInputStream();
|
|
|
+
|
|
|
+ PutObjectResult putObjectResult = obsConfig.getObsClient().putObject(obsConfig.getBucketName(), fileName, inputStream);
|
|
|
+ return putObjectResult.getObjectUrl();
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("Obs Failed to upload file", e);
|
|
|
+ } finally {
|
|
|
+ IoUtils.closeQuietly(inputStream);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getObsFile(String fileName) {
|
|
|
+ String key = RedisCacheConstants.LOGISTICS_BANNER_CACHE + fileName;
|
|
|
+ if (redisService.hasKey(key)) {
|
|
|
+ return redisService.getCacheObject(key);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ ObsObject object = obsConfig.getObsClient().getObject(obsConfig.getBucketName(), fileName);
|
|
|
+ byte[] byteArray = IOUtils.toByteArray(object.getObjectContent());
|
|
|
+ String base64String = Base64.getEncoder().encodeToString(byteArray);
|
|
|
+ String contentType = object.getMetadata().getContentType();
|
|
|
+ String base64Data = "data:" + contentType + ";base64," + base64String;
|
|
|
+ redisService.setCacheObject(key, base64Data);
|
|
|
+ return base64Data;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("Obs Failed to get file", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|