Browse Source

增加fastdfs支持

zhouhao 7 years ago
parent
commit
4987893ba0

+ 6 - 0
hsweb-system/hsweb-system-file/hsweb-system-file-service/hsweb-system-file-service-simple/pom.xml

@@ -22,5 +22,11 @@
             <artifactId>hsweb-system-file-service-api</artifactId>
             <artifactId>hsweb-system-file-service-api</artifactId>
             <version>${project.version}</version>
             <version>${project.version}</version>
         </dependency>
         </dependency>
+        <dependency>
+            <groupId>com.luhuiguo</groupId>
+            <artifactId>fastdfs-spring-boot-starter</artifactId>
+            <version>0.2.0</version>
+            <optional>true</optional>
+        </dependency>
     </dependencies>
     </dependencies>
 </project>
 </project>

+ 133 - 0
hsweb-system/hsweb-system-file/hsweb-system-file-service/hsweb-system-file-service-simple/src/main/java/org/hswebframework/web/service/file/fastdfs/FdfsFileService.java

@@ -0,0 +1,133 @@
+package org.hswebframework.web.service.file.fastdfs;
+
+import com.luhuiguo.fastdfs.domain.MetaData;
+import com.luhuiguo.fastdfs.domain.StorePath;
+import com.luhuiguo.fastdfs.service.FastFileStorageClient;
+import org.apache.commons.codec.binary.Hex;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.hswebframework.utils.time.DateFormatter;
+import org.hswebframework.web.commons.entity.DataStatus;
+import org.hswebframework.web.entity.file.FileInfoEntity;
+import org.hswebframework.web.service.file.FileInfoService;
+import org.hswebframework.web.service.file.FileService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.StreamUtils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.security.MessageDigest;
+import java.util.Arrays;
+import java.util.HashSet;
+
+/**
+ * TODO 完成注释
+ *
+ * @author zhouhao
+ * @since
+ */
+public class FdfsFileService implements FileService {
+    private FastFileStorageClient fastFileStorageClient;
+
+    private FileInfoService fileInfoService;
+
+    private String staticLocation = "/";
+
+    @Autowired
+    public void setFastFileStorageClient(FastFileStorageClient fastFileStorageClient) {
+        this.fastFileStorageClient = fastFileStorageClient;
+    }
+
+    @Autowired
+    public void setFileInfoService(FileInfoService fileInfoService) {
+        this.fileInfoService = fileInfoService;
+    }
+
+    public void setStaticLocation(String staticLocation) {
+        this.staticLocation = staticLocation;
+    }
+
+    public String getStaticLocation() {
+        return staticLocation;
+    }
+
+    @Override
+    public InputStream readFile(String fileIdOrMd5) {
+        FileInfoEntity entity = fileInfoService.selectByIdOrMd5(fileIdOrMd5);
+        StorePath path = StorePath.praseFromUrl(entity.getLocation());
+        return fastFileStorageClient.downloadFile(path.getGroup(), path.getPath(), ins -> ins);
+    }
+
+    @Override
+    public FileInfoEntity saveFile(InputStream fileStream, String fileName, String type, String creatorId) throws IOException {
+//        MetaData createIdMeta = new MetaData("creatorId", creatorId);
+        MessageDigest digest = DigestUtils.getMd5Digest();
+        String suffix = fileName.contains(".") ?
+                fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : "";
+
+        StorePath path;
+        int fileSize;
+        try (InputStream tmp = new InputStream() {
+
+            @Override
+            public int read(byte[] b, int off, int len) throws IOException {
+                int r = super.read(b, off, len);
+                digest.update(b, off, len);
+                return r;
+            }
+
+            @Override
+            public int read() throws IOException {
+                return fileStream.read();
+            }
+
+            @Override
+            public void close() throws IOException {
+                fileStream.close();
+                super.close();
+            }
+
+            @Override
+            public int available() throws IOException {
+                return fileStream.available();
+            }
+        }) {
+            path = fastFileStorageClient.uploadFile(tmp, fileSize = tmp.available(), suffix, new HashSet<>());
+        }
+        String md5 = Hex.encodeHexString(digest.digest());
+        FileInfoEntity fileInfo = fileInfoService.createEntity();
+        fileInfo.setLocation(path.getFullPath());
+        fileInfo.setMd5(md5);
+        fileInfo.setStatus(DataStatus.STATUS_ENABLED);
+        fileInfo.setSize((long) fileSize);
+        fileInfo.setName(fileName);
+        fileInfo.setType(type);
+        fileInfo.setCreatorId(creatorId);
+        fileInfo.setCreateTimeNow();
+        fileInfoService.insert(fileInfo);
+
+        return fileInfo;
+    }
+
+    @Override
+    public String saveStaticFile(InputStream fileStream, String fileName) throws IOException {
+        //文件后缀
+        String suffix = fileName.contains(".") ?
+                fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : "";
+
+        StorePath path = fastFileStorageClient.uploadFile(fileStream, fileStream.available(), suffix, new HashSet<>());
+
+        return staticLocation.concat(path.getFullPath());
+    }
+
+    @Override
+    public void writeFile(String fileId, OutputStream out, long skip) throws IOException {
+        try (InputStream inputStream = readFile(fileId)) {
+            if (skip > 0) {
+                inputStream.skip(skip);
+            }
+            StreamUtils.copy(inputStream, out);
+        }
+    }
+
+}

+ 1 - 1
hsweb-system/hsweb-system-file/hsweb-system-file-service/hsweb-system-file-service-simple/src/main/java/org/hswebframework/web/service/file/simple/LocalFileService.java

@@ -21,7 +21,7 @@ import java.util.Date;
  * @author zhouhao
  * @author zhouhao
  * @since 3.0
  * @since 3.0
  */
  */
-@Service("fileService")
+//@Service("fileService")
 public class LocalFileService implements FileService {
 public class LocalFileService implements FileService {
     private FileInfoService fileInfoService;
     private FileInfoService fileInfoService;
 
 

+ 7 - 0
hsweb-system/hsweb-system-file/hsweb-system-file-starter/pom.xml

@@ -22,6 +22,13 @@
     </build>
     </build>
 
 
     <dependencies>
     <dependencies>
+        <dependency>
+            <groupId>com.luhuiguo</groupId>
+            <artifactId>fastdfs-spring-boot-starter</artifactId>
+            <version>0.2.0</version>
+            <optional>true</optional>
+        </dependency>
+
         <dependency>
         <dependency>
             <groupId>org.hswebframework.web</groupId>
             <groupId>org.hswebframework.web</groupId>
             <artifactId>hsweb-system-file-service-simple</artifactId>
             <artifactId>hsweb-system-file-service-simple</artifactId>

+ 22 - 0
hsweb-system/hsweb-system-file/hsweb-system-file-starter/src/main/java/org/hswebframework/web/file/starter/FastdfsServiceAutoConfiguration.java

@@ -0,0 +1,22 @@
+package org.hswebframework.web.file.starter;
+
+import org.hswebframework.web.service.file.fastdfs.FdfsFileService;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @author zhouhao
+ * @since
+ */
+@Configuration
+@ConditionalOnProperty(prefix = "hsweb.web.upload.fdfs", name = "enable", havingValue = "true")
+public class FastdfsServiceAutoConfiguration {
+
+    @Bean
+    @ConfigurationProperties(prefix = "hsweb.web.upload")
+    public FdfsFileService fdfsFileService() {
+        return new FdfsFileService();
+    }
+}

+ 13 - 3
hsweb-system/hsweb-system-file/hsweb-system-file-starter/src/main/java/org/hswebframework/web/file/starter/FileAutoConfiguration.java

@@ -1,16 +1,26 @@
 package org.hswebframework.web.file.starter;
 package org.hswebframework.web.file.starter;
 
 
+import org.hswebframework.web.service.file.FileService;
+import org.hswebframework.web.service.file.simple.LocalFileService;
+import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Configuration;
 
 
 /**
 /**
- * TODO 完成注释
- *
  * @author zhouhao
  * @author zhouhao
- * @since
+ * @since 3.0
  */
  */
 @Configuration
 @Configuration
 @ComponentScan({"org.hswebframework.web.service.file.simple"
 @ComponentScan({"org.hswebframework.web.service.file.simple"
         , "org.hswebframework.web.controller.file"})
         , "org.hswebframework.web.controller.file"})
+@ImportAutoConfiguration(FastdfsServiceAutoConfiguration.class)
 public class FileAutoConfiguration {
 public class FileAutoConfiguration {
+
+    @ConditionalOnMissingBean(FileService.class)
+    @Bean
+    public LocalFileService localFileService() {
+        return new LocalFileService();
+    }
 }
 }

+ 6 - 12
hsweb-system/hsweb-system-file/hsweb-system-file-starter/src/test/java/org/hswebframework/web/service/file/FileUploadTests.java

@@ -8,11 +8,6 @@ import org.springframework.http.MediaType;
 import org.springframework.mock.web.MockMultipartFile;
 import org.springframework.mock.web.MockMultipartFile;
 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
 
 
-import static org.junit.Assert.*;
-
-/**
- * Created by zhouhao on 2017/7/25.
- */
 public class FileUploadTests extends SimpleWebApplicationTests {
 public class FileUploadTests extends SimpleWebApplicationTests {
 
 
     @Test
     @Test
@@ -26,26 +21,26 @@ public class FileUploadTests extends SimpleWebApplicationTests {
                                 MediaType.TEXT_PLAIN_VALUE, "test".getBytes()))
                                 MediaType.TEXT_PLAIN_VALUE, "test".getBytes()))
                         .file(new MockMultipartFile("files", "test2.txt",
                         .file(new MockMultipartFile("files", "test2.txt",
                                 MediaType.TEXT_PLAIN_VALUE, "test2".getBytes()))
                                 MediaType.TEXT_PLAIN_VALUE, "test2".getBytes()))
-                ).andReturn()
+        ).andReturn()
                 .getResponse()
                 .getResponse()
                 .getContentAsString();
                 .getContentAsString();
 
 
 
 
-        Assert.assertEquals(JSON.parseObject(result).getJSONArray("result").size(),2);
+        Assert.assertEquals(JSON.parseObject(result).getJSONArray("result").size(), 2);
         System.out.println(result);
         System.out.println(result);
-        String fileId=JSON.parseObject(result)
+        String fileId = JSON.parseObject(result)
                 .getJSONArray("result")
                 .getJSONArray("result")
                 .getJSONObject(0)
                 .getJSONObject(0)
                 .getString("id");
                 .getString("id");
-        String fileMd5 =JSON.parseObject(result)
+        String fileMd5 = JSON.parseObject(result)
                 .getJSONArray("result")
                 .getJSONArray("result")
                 .getJSONObject(0)
                 .getJSONObject(0)
                 .getString("md5");
                 .getString("md5");
 
 
-        result=testGet("/file/md5/"+fileMd5).exec().resultAsString();
+        result = testGet("/file/md5/" + fileMd5).exec().resultAsString();
         System.out.println(result);
         System.out.println(result);
 
 
-        result=  mvc.perform(MockMvcRequestBuilders.get("/file/download/"+fileId))
+        result = mvc.perform(MockMvcRequestBuilders.get("/file/download/" + fileId))
                 .andReturn()
                 .andReturn()
                 .getResponse()
                 .getResponse()
                 .getContentAsString();
                 .getContentAsString();
@@ -64,7 +59,6 @@ public class FileUploadTests extends SimpleWebApplicationTests {
         Assert.assertNotNull(JSON.parseObject(result).getString("result"));
         Assert.assertNotNull(JSON.parseObject(result).getString("result"));
 
 
 
 
-
     }
     }
 
 
 }
 }

+ 7 - 0
hsweb-system/hsweb-system-file/hsweb-system-file-starter/src/test/resources/application.yml

@@ -13,6 +13,13 @@ hsweb:
       version: 3.0.0
       version: 3.0.0
     web:
     web:
       upload:
       upload:
+#        fdfs:
+#          enable: true
         static-file-path: target/static
         static-file-path: target/static
         static-location: http://localhost:8080/
         static-location: http://localhost:8080/
         file-path: target/file
         file-path: target/file
+fdfs:
+  connect-timeout: 2000
+  so-timeout: 3000
+  tracker-list:
+    - localhost:22122