Browse Source

文件接口增加访问路径字段

yingp 6 years ago
parent
commit
075cbd95f3

+ 0 - 4
base-servers/file/file-dto/pom.xml

@@ -17,10 +17,6 @@
             <artifactId>springfox-swagger2</artifactId>
             <scope>compile</scope>
         </dependency>
-      <dependency>
-        <groupId>org.springframework</groupId>
-        <artifactId>spring-web</artifactId>
-      </dependency>
     </dependencies>
 
 </project>

+ 0 - 126
base-servers/file/file-dto/src/main/java/com/usoftchina/smartschool/file/dto/MyMultipartFile.java

@@ -1,126 +0,0 @@
-package com.usoftchina.smartschool.file.dto;
-
-import org.springframework.lang.Nullable;
-import org.springframework.util.Assert;
-import org.springframework.util.FileCopyUtils;
-import org.springframework.web.multipart.MultipartFile;
-
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-
-/**
- * @Author chenwei
- * @Date 2019-03-14
- */
-public class MyMultipartFile implements MultipartFile {
-
-    private final String name;
-
-    private String originalFilename;
-
-    @Nullable
-    private String contentType;
-
-    private final byte[] content;
-
-
-    /**
-     * Create a new MockMultipartFile with the given content.
-     * @param name the name of the file
-     * @param content the content of the file
-     */
-    public MyMultipartFile(String name, @Nullable byte[] content) {
-        this(name, "", null, content);
-    }
-
-    public MyMultipartFile(String name, String originalName, @Nullable byte[] content) {
-        this(name, originalName, null, content);
-    }
-
-    /**
-     * Create a new MockMultipartFile with the given content.
-     * @param name the name of the file
-     * @param contentStream the content of the file as stream
-     * @throws IOException if reading from the stream failed
-     */
-    public MyMultipartFile(String name, InputStream contentStream) throws IOException {
-        this(name, "", null, FileCopyUtils.copyToByteArray(contentStream));
-    }
-
-    /**
-     * Create a new MockMultipartFile with the given content.
-     * @param name the name of the file
-     * @param originalFilename the original filename (as on the client's machine)
-     * @param contentType the content type (if known)
-     * @param content the content of the file
-     */
-    public MyMultipartFile(
-            String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable byte[] content) {
-
-        Assert.hasLength(name, "Name must not be null");
-        this.name = name;
-        this.originalFilename = (originalFilename != null ? originalFilename : "");
-        this.contentType = contentType;
-        this.content = (content != null ? content : new byte[0]);
-    }
-
-    /**
-     * Create a new MockMultipartFile with the given content.
-     * @param name the name of the file
-     * @param originalFilename the original filename (as on the client's machine)
-     * @param contentType the content type (if known)
-     * @param contentStream the content of the file as stream
-     * @throws IOException if reading from the stream failed
-     */
-    public MyMultipartFile(
-            String name, @Nullable String originalFilename, @Nullable String contentType, InputStream contentStream)
-            throws IOException {
-
-        this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
-    }
-
-
-    @Override
-    public String getName() {
-        return this.name;
-    }
-
-    @Override
-    public String getOriginalFilename() {
-        return this.originalFilename;
-    }
-
-    @Override
-    @Nullable
-    public String getContentType() {
-        return this.contentType;
-    }
-
-    @Override
-    public boolean isEmpty() {
-        return (this.content.length == 0);
-    }
-
-    @Override
-    public long getSize() {
-        return this.content.length;
-    }
-
-    @Override
-    public byte[] getBytes() throws IOException {
-        return this.content;
-    }
-
-    @Override
-    public InputStream getInputStream() throws IOException {
-        return new ByteArrayInputStream(this.content);
-    }
-
-    @Override
-    public void transferTo(File dest) throws IOException, IllegalStateException {
-        FileCopyUtils.copy(this.content, dest);
-    }
-
-}

+ 1 - 13
base-servers/file/file-server/src/main/java/com/usoftchina/smartschool/file/controller/FileController.java

@@ -94,19 +94,7 @@ public class FileController {
         // 检查父文件夹
         FileInfo parent = checkFolder(folderId);
         FileInfo info = FileInfo.newFile(file).folder(parent.getId())
-                .storeBy(storageClient).build();
-        fileService.save(info);
-        return Result.success(BeanMapper.map(info, FileInfoDTO.class));
-    }
-
-    @ApiOperation(value = "上传文件")
-    @PostMapping(value = "/imageUpload")
-    public Result<FileInfoDTO> imageUpload(@RequestBody ImageFile imageFile) throws Exception {
-        // 检查父文件夹
-        FileInfo parent = checkFolder(0L);
-        MultipartFile file = new MyMultipartFile(imageFile.getName(), imageFile.getName()+ ".jpg", imageFile.getContent()) ;
-        FileInfo info = FileInfo.newFile(file).folder(parent.getId())
-                .storeBy(storageClient).build();
+                .storeBy(storageClient).baseUrl(fileBaseUrl).build();
         fileService.save(info);
         return Result.success(BeanMapper.map(info, FileInfoDTO.class));
     }

+ 18 - 5
base-servers/file/file-server/src/main/java/com/usoftchina/smartschool/file/po/FileInfo.java

@@ -33,6 +33,10 @@ public class FileInfo extends CommonBaseEntity implements Serializable {
      * 存储路径
      */
     private String fullPath;
+    /**
+     * 访问路径
+     */
+    private String accessPath;
     /**
      * Content-Type
      */
@@ -77,10 +81,11 @@ public class FileInfo extends CommonBaseEntity implements Serializable {
         this.name = name;
     }
 
-    public FileInfo(Long folderId, String name, String fullPath, String mime, String ext, String type, long size) {
+    public FileInfo(Long folderId, String name, String fullPath, String accessPath, String mime, String ext, String type, long size) {
         this.folderId = folderId;
         this.name = name;
         this.fullPath = fullPath;
+        this.accessPath = accessPath;
         this.mime = mime;
         this.ext = ext;
         this.type = type;
@@ -143,6 +148,14 @@ public class FileInfo extends CommonBaseEntity implements Serializable {
         this.type = type;
     }
 
+    public String getAccessPath() {
+        return accessPath;
+    }
+
+    public void setAccessPath(String accessPath) {
+        this.accessPath = accessPath;
+    }
+
     public boolean isDeleted() {
         return deleted;
     }
@@ -287,12 +300,12 @@ public class FileInfo extends CommonBaseEntity implements Serializable {
 
         public FileInfo build() throws IOException {
             if (null != storageClient) {
-                this.fullPath = StringUtils.nullIf(baseUrl) +
-                        storageClient.uploadFile(multipartFile.getInputStream(),
-                                new FileMetadata(mime, ext, size));
+                this.fullPath = storageClient.uploadFile(multipartFile.getInputStream(),
+                        new FileMetadata(mime, ext, size));
             }
 
-            return new FileInfo(folderId, name, fullPath, mime, ext, type, size);
+            String accessPath = StringUtils.nullIf(baseUrl) + fullPath;
+            return new FileInfo(folderId, name, fullPath, accessPath, mime, ext, type, size);
         }
     }
 }