Преглед на файлове

扩展core-fss,为FileStore增加delete方法,以及两种存储模式的实现

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi преди 2 години
родител
ревизия
c954fb3602

+ 6 - 0
core-fss/src/main/java/com/qmth/boot/core/fss/store/FileStore.java

@@ -122,4 +122,10 @@ public interface FileStore {
      */
     boolean exist(String path);
 
+    /**
+     * 删除已存在的文件
+     *
+     * @param path
+     */
+    boolean delete(String path);
 }

+ 12 - 0
core-fss/src/main/java/com/qmth/boot/core/fss/store/impl/DiskStore.java

@@ -1,5 +1,6 @@
 package com.qmth.boot.core.fss.store.impl;
 
+import com.qmth.boot.core.exception.StatusException;
 import com.qmth.boot.core.fss.store.FileStore;
 import com.qmth.boot.tools.io.IOUtils;
 import com.qmth.boot.tools.models.ByteArray;
@@ -97,4 +98,15 @@ public class DiskStore implements FileStore {
         return file.exists() && file.isFile();
     }
 
+    @Override
+    public boolean delete(String path) {
+        path = formatPath(path);
+        File file = new File(rootDir, path);
+        if (file.exists() && file.isFile()) {
+            return file.delete();
+        } else {
+            throw new StatusException(path + " not exist");
+        }
+    }
+
 }

+ 9 - 2
core-fss/src/main/java/com/qmth/boot/core/fss/store/impl/OssStore.java

@@ -77,14 +77,14 @@ public class OssStore implements FileStore {
     }
 
     @Override
-    public void write(String path, InputStream ins, String md5) throws Exception {
+    public void write(String path, InputStream ins, String md5) {
         ObjectMetadata metadata = new ObjectMetadata();
         metadata.setContentMD5(toBase64(md5));
         ossClient.putObject(bucket, formatPath(path), ins, metadata);
     }
 
     @Override
-    public InputStream read(String path) throws Exception {
+    public InputStream read(String path) {
         OSSObject ossObject = ossClient.getObject(bucket, formatPath(path));
         return ossObject.getObjectContent();
     }
@@ -98,6 +98,12 @@ public class OssStore implements FileStore {
         }
     }
 
+    @Override
+    public boolean delete(String path) {
+        ossClient.deleteObject(bucket, formatPath(path));
+        return true;
+    }
+
     @Override
     public void close() {
         if (ossClient != null) {
@@ -107,4 +113,5 @@ public class OssStore implements FileStore {
             temporaryUrlClient.shutdown();
         }
     }
+
 }