liujie
2026-08-21 77649c76ec1c3afcb93e9963243d637665b3a307
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
package com.tourguide.modules.user.service;
 
import com.tourguide.common.exception.BusinessException;
import com.tourguide.config.properties.StorageProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
 
@Service
public class GuideRegistrationDocumentStorageService {
    private static final long MAX_FILE_SIZE = 5L * 1024 * 1024;
    private static final Map<String, String> CONTENT_TYPE_EXTENSIONS = createContentTypeExtensions();
    private final StorageProperties storageProperties;
 
    public GuideRegistrationDocumentStorageService(StorageProperties storageProperties) {
        this.storageProperties = storageProperties;
    }
 
    public String store(MultipartFile file) {
        if (file == null || file.isEmpty()) {
            throw new BusinessException(400, "请选择证件图片");
        }
        if (file.getSize() > MAX_FILE_SIZE) {
            throw new BusinessException(400, "证件图片不能超过5MB");
        }
        String extension = CONTENT_TYPE_EXTENSIONS.get(file.getContentType());
        if (extension == null) {
            throw new BusinessException(400, "证件图片仅支持 JPG、PNG 或 WebP 格式");
        }
        Path directory = Paths.get(storageProperties.getGuideApplicationDirectory()).toAbsolutePath().normalize();
        String filename = UUID.randomUUID().toString().replace("-", "") + extension;
        Path target = directory.resolve(filename).normalize();
        if (!target.startsWith(directory)) {
            throw new BusinessException(400, "证件文件路径不合法");
        }
        try {
            Files.createDirectories(directory);
            try (InputStream inputStream = file.getInputStream()) {
                Files.copy(inputStream, target, StandardCopyOption.REPLACE_EXISTING);
            }
        } catch (IOException exception) {
            throw new BusinessException(500, "证件图片保存失败,请稍后重试");
        }
        return normalizePublicPath(storageProperties.getGuideApplicationPublicPath()) + "/" + filename;
    }
 
    private String normalizePublicPath(String publicPath) {
        String normalized = publicPath == null ? "/uploads/guide-applications" : publicPath.trim();
        if (!normalized.startsWith("/")) normalized = "/" + normalized;
        while (normalized.endsWith("/")) normalized = normalized.substring(0, normalized.length() - 1);
        return normalized;
    }
 
    private static Map<String, String> createContentTypeExtensions() {
        Map<String, String> extensions = new HashMap<String, String>();
        extensions.put("image/jpeg", ".jpg");
        extensions.put("image/png", ".png");
        extensions.put("image/webp", ".webp");
        return extensions;
    }
}