admin
2025-06-09 4eb46966002c6ca24cbb8cc8b519a05610e81649
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package tech.aiflowy.ai.controller;
 
import cn.dev33.satoken.annotation.SaIgnore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerMapping;
import tech.aiflowy.common.util.StringUtil;
 
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
 
@RestController
public class FilePreviewController {
 
    // 定义图片存储的基础路径
    @Value("${aiflowy.storage.local.root}")
    private  String fileUploadPath;
 
    @SaIgnore
    @GetMapping("/api/images/**")
    public ResponseEntity<byte[]> getImage(HttpServletRequest request) throws IOException {
        // 获取完整的路径
        String fullPath = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        String basePath = "/api/images/";
        String filePath = fullPath.substring(basePath.length()-1);
        String imagePath = getRootPath() + filePath;
        File imageFile = new File(imagePath);
 
        if (!imageFile.exists()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
 
        // 读取文件内容
        byte[] fileContent = Files.readAllBytes(imageFile.toPath());
 
        // 返回文件内容
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_TYPE, getContentType(imageFile))
                .body(fileContent);
    }
 
    // 根据文件扩展名获取 MIME 类型
    private String getContentType(File file) {
        String fileName = file.getName().toLowerCase();
        if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")) {
            return "image/jpeg";
        } else if (fileName.endsWith(".png")) {
            return "image/png";
        } else if (fileName.endsWith(".gif")) {
            return "image/gif";
        }
        return "application/octet-stream"; // 默认类型
    }
 
    public String getRootPath() {
        if (StringUtil.hasText(this.fileUploadPath)) {
            return this.fileUploadPath;
        }
        ClassPathResource fileResource = new ClassPathResource("/");
        try {
            return new File(fileResource.getFile(), "/public").getAbsolutePath();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}