Parcourir la source

account 模块和 myblog 项目对齐

reghao il y a 2 ans
Parent
commit
6ed208f844

+ 53 - 0
manager/src/main/java/cn/reghao/devops/manager/account/controller/FileController.java

@@ -0,0 +1,53 @@
+package cn.reghao.devops.manager.account.controller;
+
+import cn.reghao.devops.manager.account.service.FileService;
+import cn.reghao.jutil.jdk.result.WebResult;
+import cn.reghao.jutil.web.ServletUtil;
+import org.springframework.http.MediaType;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.servlet.http.HttpServletRequest;
+import java.io.IOException;
+import java.net.URLDecoder;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author reghao
+ * @date 2024-01-20 17:24:58
+ */
+@Controller
+@RequestMapping
+public class FileController {
+    private final FileService fileService;
+
+    public FileController(FileService fileService) {
+        this.fileService = fileService;
+    }
+
+    @PostMapping(value = "/api/file/upload", produces = MediaType.APPLICATION_JSON_VALUE)
+    @ResponseBody
+    public String imageUpload(MultipartFile file) throws Exception {
+        String url = fileService.putFile(file);
+        Map<String, String> map = new HashMap<>();
+        map.put("name", file.getOriginalFilename());
+        map.put("url", url);
+        return WebResult.success(map);
+    }
+
+    @GetMapping("/file/**")
+    @ResponseBody
+    public void getFile() throws IOException {
+        HttpServletRequest servletRequest = ServletUtil.getRequest();
+        String uri = servletRequest.getRequestURI();
+        String uri1 = URLDecoder.decode(uri, StandardCharsets.UTF_8);
+        String objectName =  uri1.replaceFirst("/", "");
+        fileService.getFile(objectName);
+    }
+}

+ 15 - 0
manager/src/main/java/cn/reghao/devops/manager/account/db/repository/DiskFileRepository.java

@@ -0,0 +1,15 @@
+package cn.reghao.devops.manager.account.db.repository;
+
+import cn.reghao.devops.manager.account.model.po.DiskFile;
+import org.springframework.data.jpa.repository.JpaRepository;
+
+import java.util.List;
+
+/**
+ * @author reghao
+ * @date 2024-01-29 09:24:29
+ */
+public interface DiskFileRepository extends JpaRepository<DiskFile, Integer> {
+    DiskFile findByObjectName(String objectName);
+    List<DiskFile> findBySha256sum(String sha256sum);
+}

+ 57 - 0
manager/src/main/java/cn/reghao/devops/manager/account/model/po/DiskFile.java

@@ -0,0 +1,57 @@
+package cn.reghao.devops.manager.account.model.po;
+
+import cn.reghao.devops.manager.util.db.BaseEntity;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+
+/**
+ * @author reghao
+ * @date 2023-11-11 00:06:21
+ */
+@NoArgsConstructor
+@AllArgsConstructor
+@Setter
+@Getter
+@Entity
+public class DiskFile extends BaseEntity {
+    @Column(nullable = false, unique = true)
+    private String objectName;
+    @Column(nullable = false, unique = true)
+    private String objectId;
+    @Column(nullable = false)
+    private String absolutePath;
+    @Column(nullable = false)
+    private String sha256sum;
+    @Column(nullable = false)
+    private String filename;
+    @Column(nullable = false)
+    private String contentType;
+    @Column(nullable = false)
+    private Long size;
+    private Long owner;
+
+    public DiskFile(String objectName, String objectId, DiskFile diskFile, String filename) {
+        this.objectName = objectName;
+        this.objectId = objectId;
+        this.absolutePath = diskFile.getAbsolutePath();
+        this.sha256sum = diskFile.getSha256sum();
+        this.filename = filename;
+        this.contentType = diskFile.getContentType();
+        this.size = diskFile.getSize();
+    }
+
+    public DiskFile(String objectName, String objectId, String absolutePath, String sha256sum, String filename, String contentType, long size) {
+        this.objectName = objectName;
+        this.objectId = objectId;
+        this.absolutePath = absolutePath;
+        this.sha256sum = sha256sum;
+        this.filename = filename;
+        this.contentType = contentType;
+        this.size = size;
+    }
+}

+ 155 - 0
manager/src/main/java/cn/reghao/devops/manager/account/service/FileService.java

@@ -0,0 +1,155 @@
+package cn.reghao.devops.manager.account.service;
+
+import cn.reghao.devops.manager.account.db.repository.DiskFileRepository;
+import cn.reghao.devops.manager.account.model.po.DiskFile;
+import cn.reghao.jutil.jdk.security.DigestUtil;
+import cn.reghao.jutil.web.ServletUtil;
+import org.apache.commons.io.FileUtils;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.annotation.PostConstruct;
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
+import java.util.List;
+import java.util.UUID;
+
+/**
+ * @author reghao
+ * @date 2024-01-19 21:29:24
+ */
+@Service
+public class FileService {
+    // 1MiB
+    private final int bufSize = 1024*1024;
+    private final String storeDir = "";
+    private final DiskFileRepository diskFileRepository;
+
+    public FileService(DiskFileRepository diskFileRepository) {
+        this.diskFileRepository = diskFileRepository;
+    }
+
+    @PostConstruct
+    public void initLocalStore() throws IOException {
+        File dir1 = new File(storeDir);
+        if (!dir1.exists()) {
+            //FileUtils.forceMkdir(dir1);
+        }
+    }
+
+    public void getFile(String objectName) throws IOException {
+        DiskFile diskFile = diskFileRepository.findByObjectName(objectName);
+        if (diskFile == null) {
+            writeResponse(HttpServletResponse.SC_NOT_FOUND);
+            return;
+        }
+
+        String absolutePath = diskFile.getAbsolutePath();
+        String contentType = diskFile.getContentType();
+        long size = diskFile.getSize();
+        HttpServletResponse response = ServletUtil.getResponse();
+        response.setStatus(HttpServletResponse.SC_OK);
+        response.setContentType(contentType);
+        response.setContentLengthLong(size);
+
+        OutputStream outputStream = response.getOutputStream();
+        writeResponse(outputStream, absolutePath, 0, size);
+    }
+
+    private void writeResponse(int statusCode) throws IOException {
+        HttpServletResponse response = ServletUtil.getResponse();
+        response.setStatus(statusCode);
+        OutputStream outputStream = response.getOutputStream();
+        outputStream.flush();
+        outputStream.close();
+    }
+
+    private void writeResponse(OutputStream outputStream, String absolutePath, long start, long end) throws IOException {
+        RandomAccessFile raf = new RandomAccessFile(absolutePath, "r");
+        raf.seek(start);
+
+        long len = end-start+1;
+        if (len < bufSize) {
+            int len1 = (int) len;
+            byte[] buf1 = new byte[len1];
+            int readLen1 = raf.read(buf1, 0, len1);
+            outputStream.write(buf1, 0, readLen1);
+        } else {
+            byte[] buf = new byte[bufSize];
+            long totalRead = 0;
+            int readLen;
+            while ((readLen = raf.read(buf, 0, bufSize)) != -1) {
+                outputStream.write(buf, 0, readLen);
+                totalRead += readLen;
+
+                long left = len - totalRead;
+                if (left < bufSize) {
+                    int left1 = (int) left;
+                    byte[] buf1 = new byte[left1];
+                    int readLen1 = raf.read(buf1, 0, left1);
+                    outputStream.write(buf1, 0, readLen1);
+                    break;
+                }
+            }
+        }
+
+        outputStream.flush();
+        outputStream.close();
+        raf.close();
+    }
+
+    public String putFile(MultipartFile file) throws Exception {
+        long size = file.getSize();
+        String filename = file.getOriginalFilename();
+        String suffix = getSuffix(filename);
+
+        String objectId = UUID.randomUUID().toString().replace("-", "");;
+        String objectName;
+        if (suffix.isBlank()) {
+            objectName = String.format("file/%s", objectId);
+        } else {
+            objectName = String.format("file/%s%s", objectId, suffix);
+        }
+
+        String contentId = UUID.randomUUID().toString().replace("-", "");
+        File savedFile = saveFile(file.getInputStream(), contentId, suffix);
+        String contentType = Files.probeContentType(Path.of(savedFile.getAbsolutePath()));
+        String sha256sum = DigestUtil.sha256sum(savedFile.getAbsolutePath());
+
+        List<DiskFile> diskFiles = diskFileRepository.findBySha256sum(sha256sum);
+        DiskFile diskFile;
+        if (!diskFiles.isEmpty()) {
+            DiskFile existFile = diskFiles.get(0);
+            diskFile = new DiskFile(objectName, objectId, existFile, filename);
+            FileUtils.deleteQuietly(savedFile);
+        } else {
+            diskFile = new DiskFile(objectName, objectId, savedFile.getAbsolutePath(), sha256sum, filename, contentType, size);
+        }
+
+        diskFileRepository.save(diskFile);
+        return "/" + objectName;
+    }
+
+    private File saveFile(InputStream inputStream, String contentId, String suffix) throws IOException {
+        String absolutePath = String.format("%s/%s%s", storeDir, contentId, suffix);
+        File file = new File(absolutePath);
+        if (file.exists()) {
+            throw new IOException(absolutePath + " exist");
+        }
+
+        Files.copy(inputStream, Path.of(absolutePath), StandardCopyOption.REPLACE_EXISTING);
+        return file;
+    }
+
+    private String getSuffix(String filename) {
+        if (filename == null) {
+            return "";
+        }
+
+        int idx = filename.lastIndexOf(".");
+        return idx == -1 ? "" : filename.substring(idx);
+    }
+}