Browse Source

修改文件上传逻辑

tianboguang 3 years ago
parent
commit
064b052043

+ 22 - 0
4dkankan-utils-fyun-local/pom.xml

@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <artifactId>4dkankan-utils</artifactId>
+        <groupId>com.fdkankan</groupId>
+        <version>3.0.0-SNAPSHOT</version>
+    </parent>
+    <artifactId>4dkankan-utils-fyun-local</artifactId>
+    <properties>
+        <java.version>1.8</java.version>
+    </properties>
+    <dependencies>
+        <dependency>
+            <groupId>com.fdkankan</groupId>
+            <artifactId>4dkankan-utils-fyun-parent</artifactId>
+            <version>3.0.0-SNAPSHOT</version>
+        </dependency>
+    </dependencies>
+
+</project>

+ 135 - 0
4dkankan-utils-fyun-local/src/main/java/com/fdkankan/fyun/local/LocalFileService.java

@@ -0,0 +1,135 @@
+package com.fdkankan.fyun.local;
+
+import cn.hutool.core.io.FileUtil;
+import com.fdkankan.fyun.constant.FYunConstants;
+import com.fdkankan.fyun.face.AbstractFYunFileService;
+import com.fdkankan.fyun.local.constant.LocalConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.stereotype.Component;
+import org.springframework.util.ObjectUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+@Component
+@ConditionalOnProperty(name = "fyun.type",havingValue = "local")
+public class LocalFileService extends AbstractFYunFileService {
+
+    private Logger log = LoggerFactory.getLogger(this.getClass().getName());
+
+    @Override
+    public String uploadFile(String bucket, byte[] data, String remoteFilePath) {
+        FileUtil.writeBytes(data, getOssPath(bucket,remoteFilePath));
+        return null;
+    }
+
+    @Override
+    public String uploadFile(String bucket, String filePath, String remoteFilePath) {
+        return uploadFile(bucket, filePath, remoteFilePath, true, null);
+    }
+
+    @Override
+    public String uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) {
+        return uploadFile(bucket, filePath, remoteFilePath, true, headers);
+    }
+
+    private String uploadFile(String bucket, String filePath, String remoteFilePath, Boolean shutdown,Map<String, String> headers) {
+        FileUtil.copy(filePath, getOssPath(bucket,remoteFilePath),true);
+        return null;
+    }
+
+    @Override
+    public String uploadFileByCommand(String bucket, String filePath, String remoteFilePath) {
+        String ossPath = bucket + "/" + remoteFilePath;
+        try {
+            String command = String.format(FYunConstants.UPLOAD_SH, ossPath, filePath);
+            log.info("开始上传文件, ossPath:{}, srcPath:{}", ossPath, filePath);
+            callshell(command);
+            return null;
+        } catch (Exception e) {
+            log.error("上传文件失败, ossPath:{}, srcPath:{}", ossPath, filePath);
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+    @Override
+    public void deleteFile(String bucket, String remoteFilePath) throws IOException {
+        FileUtil.del(getOssPath(bucket,remoteFilePath));
+    }
+
+    @Override
+    public void deleteFolder(String bucket, String remoteFolderPath) {
+        FileUtil.del(getOssPath(bucket,remoteFolderPath));
+    }
+
+    @Override
+    public void uploadMulFiles(String bucket, Map<String, String> filepaths) {
+        try {
+            for (Map.Entry<String, String> entry : filepaths.entrySet()) {
+                uploadFile(bucket, entry.getKey(), entry.getValue(), false,null);
+            }
+        } catch (Exception e) {
+            log.error("OSS批量上传文件失败!");
+        }
+    }
+
+
+    @Override
+    public List<String> listRemoteFiles(String bucket, String sourcePath) {
+        return listRemoteFiles(bucket, sourcePath, true);
+    }
+
+    private List<String> listRemoteFiles(String bucket, String sourcePath, Boolean shutdown) {
+        return FileUtil.loopFiles(getOssPath(bucket,sourcePath)).stream()
+                .map(f->f.getAbsolutePath().replace(LocalConstants.BASE_PATH.concat(bucket).concat(File.separator),""))
+                .collect(Collectors.toList());
+    }
+
+    @Override
+    public void copyFileBetweenBucket(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath) {
+        copyFileBetweenBucket(sourceBucketName, sourcePath, targetBucketName, targetPath, true);
+    }
+
+    private void copyFileBetweenBucket(String sourceBucketName, String sourcePath, String targetBucketName, String targetPath, Boolean shutdown) {
+        FileUtil.copyContent(new File(getOssPath(sourceBucketName, sourcePath)), new File(getOssPath(targetBucketName, targetPath)), true);
+    }
+
+    @Override
+    public void copyFilesBetweenBucket(String sourceBucketName, String targetBucketName, Map<String, String> pathMap) {
+        if (ObjectUtils.isEmpty(pathMap)) {
+            return;
+        }
+        try {
+            for (Map.Entry<String, String> entry : pathMap.entrySet()) {
+                copyFileBetweenBucket(sourceBucketName, entry.getKey(), targetBucketName, entry.getValue(), false);
+            }
+        } catch (Exception e) {
+            log.error("批量复制文件失败!");
+        }
+    }
+
+    @Override
+    public String getFileContent(String bucketName, String remoteFilePath) {
+       return FileUtil.readUtf8String(getOssPath(bucketName,remoteFilePath));
+    }
+
+    @Override
+    public boolean fileExist(String bucket, String objectName) {
+        return FileUtil.exist(getOssPath(bucket,objectName));
+    }
+
+    @Override
+    public void downloadFile(String bucket, String remoteFilePath, String localPath) {
+        FileUtil.copy(getOssPath(bucket, remoteFilePath), localPath, true);
+    }
+
+    private String getOssPath(String bucket, String filePath) {
+        return LocalConstants.BASE_PATH.concat(bucket).concat(File.separator).concat(filePath);
+    }
+}

+ 12 - 0
4dkankan-utils-fyun-local/src/main/java/com/fdkankan/fyun/local/config/LocalConfig.java

@@ -0,0 +1,12 @@
+package com.fdkankan.fyun.local.config;
+
+import com.fdkankan.fyun.config.FYunFileConfig;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+public class LocalConfig {
+
+    @Autowired
+    private FYunFileConfig fYunFileConfig;
+}

+ 10 - 0
4dkankan-utils-fyun-local/src/main/java/com/fdkankan/fyun/local/constant/LocalConstants.java

@@ -0,0 +1,10 @@
+package com.fdkankan.fyun.local.constant;
+
+public class LocalConstants {
+    /**
+     * oss文件上传命令
+     * 第一个参数是oss路径,要包含bucket名称
+     * 第二个参数是本地文件路径
+     */
+    public static final String BASE_PATH = "/oss/";
+}

+ 2 - 5
4dkankan-utils-fyun-oss/src/main/java/com/fdkankan/fyun/oss/OssFileService.java

@@ -31,7 +31,6 @@ public class OssFileService extends AbstractFYunFileService {
     public String uploadFile(String bucket, byte[] data, String remoteFilePath) {
         try {
             ossClient.putObject(bucket, remoteFilePath, new ByteArrayInputStream(data));
-            return fYunFileConfig.getHost().concat(remoteFilePath);
         } catch (Exception e) {
             log.error("oss上传文件失败", e);
             e.printStackTrace();
@@ -72,12 +71,11 @@ public class OssFileService extends AbstractFYunFileService {
                 }
             }
             ossClient.putObject(bucket, remoteFilePath, new File(filePath), metadata);
-            return fYunFileConfig.getHost().concat(remoteFilePath);
         } catch (Exception e) {
             log.error("oss上传文件失败", e);
             e.printStackTrace();
-            return null;
         }
+        return null;
     }
 
     @Override
@@ -87,12 +85,11 @@ public class OssFileService extends AbstractFYunFileService {
             String command = String.format(OssConstants.UPLOAD_SH, ossPath, filePath);
             log.info("开始上传文件, ossPath:{}, srcPath:{}", ossPath, filePath);
             callshell(command);
-            return fYunFileConfig.getHost().concat(remoteFilePath);
         } catch (Exception e) {
             log.error("上传文件失败, ossPath:{}, srcPath:{}", ossPath, filePath);
             e.printStackTrace();
-            return null;
         }
+        return null;
     }
 
     @Override

+ 8 - 4
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/face/AbstractFYunFileService.java

@@ -25,22 +25,26 @@ public abstract class AbstractFYunFileService implements FYunFileServiceInterfac
 
     @Override
     public String uploadFile(byte[] data, String remoteFilePath) {
-        return uploadFile(fYunFileConfig.getBucket(), data, remoteFilePath);
+        uploadFile(fYunFileConfig.getBucket(), data, remoteFilePath);
+        return fYunFileConfig.getHost().concat(remoteFilePath);
     }
 
     @Override
     public String uploadFile(String filePath, String remoteFilePath) {
-        return uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath);
+        uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath);
+        return fYunFileConfig.getHost().concat(remoteFilePath);
     }
 
     @Override
     public String uploadFile(String filePath, String remoteFilePath, Map<String, String> headers) {
-        return uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath, headers);
+        uploadFile(fYunFileConfig.getBucket(), filePath, remoteFilePath, headers);
+        return fYunFileConfig.getHost().concat(remoteFilePath);
     }
 
     @Override
     public String uploadFileByCommand(String filePath, String remoteFilePath) {
-        return uploadFileByCommand(fYunFileConfig.getBucket(), filePath, remoteFilePath);
+        uploadFileByCommand(fYunFileConfig.getBucket(), filePath, remoteFilePath);
+        return fYunFileConfig.getHost().concat(remoteFilePath);
     }
 
     @Override

+ 12 - 7
4dkankan-utils-fyun-parent/src/main/java/com/fdkankan/fyun/face/FYunFileServiceInterface.java

@@ -16,8 +16,9 @@ public interface FYunFileServiceInterface {
      *
      * @param bucket         目标bucket
      * @param data           上传的数据
-     * @param remoteFilePath 上传后的文件路径
-     * @
+     * @param remoteFilePath 上传后的文件路径,
+     *
+     * @return 因为目前一个bucket对应一个域名,因此暂时无法确定上传后的文件路径,因此返回null
      */
     String uploadFile(String bucket, byte[] data, String remoteFilePath) ;
 
@@ -26,7 +27,7 @@ public interface FYunFileServiceInterface {
      *
      * @param data           上传的数据
      * @param remoteFilePath 上传后的文件路径
-     * @
+     * @return 返回上传文件的链接
      */
     String uploadFile(byte[] data, String remoteFilePath) ;
 
@@ -36,7 +37,7 @@ public interface FYunFileServiceInterface {
      * @param bucket         目标bucket
      * @param filePath       本地路径
      * @param remoteFilePath 上传后的文件路径
-     * @
+     * @return 因为目前一个bucket对应一个域名,因此暂时无法确定上传后的文件路径,因此返回null
      */
     String uploadFile(String bucket, String filePath, String remoteFilePath) ;
 
@@ -45,7 +46,7 @@ public interface FYunFileServiceInterface {
      *
      * @param filePath       本地路径
      * @param remoteFilePath 上传后的文件路径
-     * @
+     * @return 返回上传文件的链接
      */
     String uploadFile(String filePath, String remoteFilePath) ;
 
@@ -55,7 +56,8 @@ public interface FYunFileServiceInterface {
      * @param bucket         目标bucket
      * @param filePath       本地路径
      * @param remoteFilePath 上传后的文件路径
-     * @
+     * @param headers 文件头
+     * @return 因为目前一个bucket对应一个域名,因此暂时无法确定上传后的文件路径,因此返回null
      */
     String uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) ;
 
@@ -64,13 +66,15 @@ public interface FYunFileServiceInterface {
      *
      * @param filePath       本地路径
      * @param remoteFilePath 上传后的文件路径
-     * @
+     * @param headers 文件头
+     * @return 返回上传文件的链接
      */
     String uploadFile(String filePath, String remoteFilePath, Map<String, String> headers) ;
 
     /**
      * 通过本地脚本上传
      *
+     * @param bucket
      * @param filePath
      * @param remoteFilePath
      */
@@ -81,6 +85,7 @@ public interface FYunFileServiceInterface {
      *
      * @param filePath
      * @param remoteFilePath
+     * @return 返回上传文件的链接
      */
     String uploadFileByCommand(String filePath, String remoteFilePath) ;
 

+ 0 - 3
4dkankan-utils-fyun-s3/src/main/java/com/fdkankan/fyun/s3/S3FileService.java

@@ -35,7 +35,6 @@ public class S3FileService extends AbstractFYunFileService {
             PutObjectRequest request = new PutObjectRequest(bucket, remoteFilePath, new ByteArrayInputStream(data), metadata);
             request.withCannedAcl(CannedAccessControlList.PublicRead);
             s3.putObject(request);
-            return fYunFileConfig.getHost().concat(remoteFilePath);
         } catch (Exception e) {
             log.error("s3上传文件失败", e);
         }
@@ -82,7 +81,6 @@ public class S3FileService extends AbstractFYunFileService {
             if (StringUtils.isNotEmpty(putObjectResult.getETag())) {
                 log.info("s3上传文件成功:" + remoteFilePath);
             }
-            return fYunFileConfig.getHost().concat(remoteFilePath);
         } catch (Exception e) {
             log.error("文件上传失败:{}",filePath);
             e.printStackTrace();
@@ -97,7 +95,6 @@ public class S3FileService extends AbstractFYunFileService {
             String command = String.format(FYunConstants.UPLOAD_SH, ossPath, filePath);
             log.info("开始上传文件, ossPath:{}, srcPath:{}", ossPath, filePath);
             callshell(command);
-            return fYunFileConfig.getHost().concat(remoteFilePath);
         } catch (Exception e) {
             log.error("上传文件失败, ossPath:{}, srcPath:{}", ossPath, filePath);
             e.printStackTrace();