|
@@ -1,17 +1,27 @@
|
|
|
package com.fdkankan.modeldemo.service.impl;
|
|
package com.fdkankan.modeldemo.service.impl;
|
|
|
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
|
import cn.hutool.core.lang.UUID;
|
|
import cn.hutool.core.lang.UUID;
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
|
+import com.fdkankan.modeldemo.config.MinioProperties;
|
|
|
|
|
+import com.fdkankan.modeldemo.constant.StorageType;
|
|
|
|
|
+import com.fdkankan.modeldemo.constant.UploadFilePath;
|
|
|
import com.fdkankan.modeldemo.entity.SceneFileMapping;
|
|
import com.fdkankan.modeldemo.entity.SceneFileMapping;
|
|
|
-import com.fdkankan.modeldemo.httpclient.HttpClient;
|
|
|
|
|
|
|
+import com.fdkankan.modeldemo.httpclient.CustomHttpClient;
|
|
|
import com.fdkankan.modeldemo.service.FYunFileService;
|
|
import com.fdkankan.modeldemo.service.FYunFileService;
|
|
|
import com.fdkankan.modeldemo.service.SceneFileMappingService;
|
|
import com.fdkankan.modeldemo.service.SceneFileMappingService;
|
|
|
-import com.fdkankan.modeldemo.utils.FdfsUtil;
|
|
|
|
|
|
|
+import com.fdkankan.modeldemo.utils.ESAPIUtil;
|
|
|
|
|
+import com.fdkankan.modeldemo.utils.MinioUtil;
|
|
|
|
|
+import org.owasp.esapi.errors.EncodingException;
|
|
|
|
|
+import org.owasp.esapi.errors.ValidationException;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.io.InputStream;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
@@ -22,61 +32,130 @@ public class FYunFileServiceImpl implements FYunFileService {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private SceneFileMappingService sceneFileMappingService;
|
|
private SceneFileMappingService sceneFileMappingService;
|
|
|
@Resource
|
|
@Resource
|
|
|
- private HttpClient customHttpClient;
|
|
|
|
|
|
|
+ private CustomHttpClient customHttpClient;
|
|
|
|
|
|
|
|
@Resource
|
|
@Resource
|
|
|
- private FdfsUtil fdfsUtil;
|
|
|
|
|
|
|
+ private MinioProperties minioProperties;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private MinioUtil minioUtil;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String getFileContent(String key, Integer subgroup, String upTime) throws IOException {
|
|
|
|
|
+ SceneFileMapping sceneFileMapping = sceneFileMappingService.getByKey(key, subgroup, upTime);
|
|
|
|
|
+ if(Objects.isNull(sceneFileMapping)){
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ String content = null;
|
|
|
|
|
+ String url = null;
|
|
|
|
|
+ String storageType = sceneFileMapping.getStorageType();
|
|
|
|
|
+ if(StrUtil.isNotEmpty(storageType) && storageType.equals(StorageType.MINIO.code())){
|
|
|
|
|
+ url = minioProperties.getAddress().concat(minioProperties.getApi().getPreview().replace(UploadFilePath.BUCKET_NAME, sceneFileMapping.getBucketName()).replace(UploadFilePath.OBJECT_NAME, sceneFileMapping.getObjectName()));
|
|
|
|
|
+ }else{
|
|
|
|
|
+ url = sceneFileMapping.getUrl();
|
|
|
|
|
+ }
|
|
|
|
|
+ try (InputStream inputStream = customHttpClient.downloadFileToInputStream(url)){
|
|
|
|
|
+ content = IoUtil.read(inputStream, StandardCharsets.UTF_8);
|
|
|
|
|
+ }
|
|
|
|
|
+ return content;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public void uploadFile(String num, Integer subgroup, String upTime, byte[] data, String key) {
|
|
|
|
|
- // 获取文件后缀
|
|
|
|
|
|
|
+ public void uploadFile(String num, Integer subgroup, String upTime, byte[] data, String key) throws ValidationException, EncodingException {
|
|
|
String suffix = "." + FileUtil.getSuffix(key);
|
|
String suffix = "." + FileUtil.getSuffix(key);
|
|
|
- // 创建临时文件
|
|
|
|
|
- File tempFile = FileUtil.createTempFile(UUID.fastUUID().toString(), suffix, new File("/temp"), true);
|
|
|
|
|
- // 将数据写入临时文件
|
|
|
|
|
|
|
+ File tempFile = FileUtil.createTempFile(UUID.fastUUID().toString(), suffix, new File(ESAPIUtil.validFilePath(ESAPIUtil.getRealPath("temp"))), true);
|
|
|
FileUtil.writeBytes(data, tempFile);
|
|
FileUtil.writeBytes(data, tempFile);
|
|
|
- // 上传文件并获取映射信息
|
|
|
|
|
- Map<String, String> mapping = fdfsUtil.uploadFile(tempFile.getAbsolutePath());
|
|
|
|
|
- // 添加记录
|
|
|
|
|
- SceneFileMapping sceneFileMapping = sceneFileMappingService.getByKey(key, subgroup, upTime);
|
|
|
|
|
- if(Objects.isNull(sceneFileMapping)){
|
|
|
|
|
- sceneFileMapping = new SceneFileMapping();
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ Map<String, String> uploadRes = minioUtil.upload(tempFile.getAbsolutePath());
|
|
|
|
|
+
|
|
|
|
|
+ //添加记录
|
|
|
|
|
+ SceneFileMapping sceneFileMapping = sceneFileMappingService.getByKey(key, subgroup, upTime);
|
|
|
|
|
+ if(Objects.isNull(sceneFileMapping)){
|
|
|
|
|
+ sceneFileMapping = new SceneFileMapping();
|
|
|
|
|
+ }
|
|
|
|
|
+ sceneFileMapping.setNum(num);
|
|
|
|
|
+ sceneFileMapping.setKey(key);
|
|
|
|
|
+ sceneFileMapping.setSubgroup(subgroup);
|
|
|
|
|
+ sceneFileMapping.setUpTime(upTime);
|
|
|
|
|
+ sceneFileMapping.setStorageType(StorageType.MINIO.code());
|
|
|
|
|
+ sceneFileMapping.setBucketName(uploadRes.get(UploadFilePath.BUCKET_NAME));
|
|
|
|
|
+ sceneFileMapping.setObjectName(uploadRes.get(UploadFilePath.OBJECT_NAME));
|
|
|
|
|
+ sceneFileMappingService.saveOrUpdate(sceneFileMapping);
|
|
|
|
|
+ }catch (Exception e){
|
|
|
|
|
+ throw e;
|
|
|
|
|
+ }finally {
|
|
|
|
|
+ FileUtil.del(tempFile);
|
|
|
}
|
|
}
|
|
|
- sceneFileMapping.setNum(num);
|
|
|
|
|
- sceneFileMapping.setFileid(mapping.get("file_id"));
|
|
|
|
|
- sceneFileMapping.setUrl(mapping.get("http_url"));
|
|
|
|
|
- sceneFileMapping.setKey(key);
|
|
|
|
|
- sceneFileMapping.setSubgroup(subgroup);
|
|
|
|
|
- sceneFileMapping.setUpTime(upTime);
|
|
|
|
|
- // 保存或更新记录
|
|
|
|
|
- sceneFileMappingService.saveOrUpdate(sceneFileMapping);
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void uploadFile(String num, Integer subgroup, String upTime, String path, String key) {
|
|
public void uploadFile(String num, Integer subgroup, String upTime, String path, String key) {
|
|
|
- // 上传文件并获取映射信息
|
|
|
|
|
- Map<String, String> mapping = fdfsUtil.uploadFile(path);
|
|
|
|
|
- // 添加记录
|
|
|
|
|
|
|
+ Map<String, String> uploadRes = minioUtil.upload(path, UUID.fastUUID() + "." + FileUtil.getSuffix(path));
|
|
|
|
|
+ //添加记录
|
|
|
SceneFileMapping sceneFileMapping = sceneFileMappingService.getByKey(key,subgroup,upTime);
|
|
SceneFileMapping sceneFileMapping = sceneFileMappingService.getByKey(key,subgroup,upTime);
|
|
|
if(Objects.isNull(sceneFileMapping)){
|
|
if(Objects.isNull(sceneFileMapping)){
|
|
|
sceneFileMapping = new SceneFileMapping();
|
|
sceneFileMapping = new SceneFileMapping();
|
|
|
}
|
|
}
|
|
|
sceneFileMapping.setNum(num);
|
|
sceneFileMapping.setNum(num);
|
|
|
- sceneFileMapping.setFileid(mapping.get("file_id"));
|
|
|
|
|
- sceneFileMapping.setUrl(mapping.get("http_url"));
|
|
|
|
|
sceneFileMapping.setKey(key);
|
|
sceneFileMapping.setKey(key);
|
|
|
sceneFileMapping.setSubgroup(subgroup);
|
|
sceneFileMapping.setSubgroup(subgroup);
|
|
|
sceneFileMapping.setUpTime(upTime);
|
|
sceneFileMapping.setUpTime(upTime);
|
|
|
- // 保存或更新记录
|
|
|
|
|
|
|
+ sceneFileMapping.setStorageType(StorageType.MINIO.code());
|
|
|
|
|
+ sceneFileMapping.setBucketName(uploadRes.get(UploadFilePath.BUCKET_NAME));
|
|
|
|
|
+ sceneFileMapping.setObjectName(uploadRes.get(UploadFilePath.OBJECT_NAME));
|
|
|
sceneFileMappingService.saveOrUpdate(sceneFileMapping);
|
|
sceneFileMappingService.saveOrUpdate(sceneFileMapping);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void deleteFile(String num, Integer subgroup, String upTime, String key) {
|
|
|
|
|
+ SceneFileMapping sceneFileMapping = sceneFileMappingService.getByKey(key,subgroup,upTime);
|
|
|
|
|
+ if(sceneFileMapping == null){
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ if(StrUtil.isNotEmpty(sceneFileMapping.getStorageType())
|
|
|
|
|
+ && sceneFileMapping.getStorageType().equals(StorageType.MINIO.code())){
|
|
|
|
|
+ minioUtil.delete(sceneFileMapping.getBucketName(), sceneFileMapping.getObjectName());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ sceneFileMappingService.removeById(sceneFileMapping.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String downloadFile(String num, Integer subgroup, String upTime, String key, String dir, String fileName) {
|
|
|
|
|
+ SceneFileMapping sceneFileMapping = sceneFileMappingService.getByKey(key, subgroup,upTime);
|
|
|
|
|
+ if(Objects.isNull(sceneFileMapping)){
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ String url = null;
|
|
|
|
|
+ if(StrUtil.isNotEmpty(sceneFileMapping.getStorageType())
|
|
|
|
|
+ && sceneFileMapping.getStorageType().equals(StorageType.MINIO.code())){
|
|
|
|
|
+ url = minioProperties.getAddress().concat(minioProperties.getApi().getDownload().replace(UploadFilePath.BUCKET_NAME, sceneFileMapping.getBucketName()).replace(UploadFilePath.OBJECT_NAME, sceneFileMapping.getObjectName()));
|
|
|
|
|
+ }else{
|
|
|
|
|
+ url = sceneFileMapping.getUrl();
|
|
|
|
|
+ }
|
|
|
|
|
+ customHttpClient.downloadFile(url, dir, fileName);
|
|
|
|
|
+ if(dir.endsWith("/")){
|
|
|
|
|
+ dir += "/";
|
|
|
|
|
+ }
|
|
|
|
|
+ return dir + fileName;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public void uploadMulFiles(String num, Integer subgroup, String upTime, Map<String, String> map) {
|
|
|
|
|
+// if(CollUtil.isEmpty(map)){
|
|
|
|
|
+// return;
|
|
|
|
|
+// }
|
|
|
|
|
+// map.keySet().stream().forEach(localPath->{
|
|
|
|
|
+// String key = map.get(localPath);
|
|
|
|
|
+// this.uploadFile(num, subgroup, upTime, localPath, key);
|
|
|
|
|
+// });
|
|
|
|
|
+//
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
|
- // 获取文件后缀
|
|
|
|
|
- String suffix = "." + FileUtil.getSuffix("/sxx/ttt/adf.json");
|
|
|
|
|
- // 创建临时文件
|
|
|
|
|
- File tempFile = FileUtil.createTempFile(UUID.fastUUID().toString(), suffix, new File("D:\\test2"), true);
|
|
|
|
|
- // 将字符串写入临时文件
|
|
|
|
|
- FileUtil.writeBytes("nihsd灌灌灌灌".getBytes(StandardCharsets.UTF_8), tempFile);
|
|
|
|
|
|
|
+ String suffix = "." + FileUtil.getSuffix("sdf/sdfsdf.txt");
|
|
|
|
|
+ System.out.println(suffix);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|