lrj
2 天以前 c61d4fe27c97d2ecc907756aa571a4ef14a7b9b6
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
package com.rongyichuang.common.api;
 
import com.rongyichuang.common.entity.Media;
import com.rongyichuang.common.dto.response.MediaResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
 
@Controller
public class MediaFieldResolver {
 
    @Value("${app.media-url:${app.media.url:}}")
    private String mediaBaseUrl;
 
    @SchemaMapping(typeName = "Media", field = "fullUrl")
    public String fullUrl(Media media) {
        if (!StringUtils.hasText(media.getPath())) return null;
        return join(mediaBaseUrl, media.getPath());
    }
 
    @SchemaMapping(typeName = "Media", field = "fullThumbUrl")
    public String fullThumbUrl(Media media) {
        if (!StringUtils.hasText(media.getThumbPath())) return null;
        return join(mediaBaseUrl, media.getThumbPath());
    }
 
    @SchemaMapping(typeName = "MediaResponse", field = "fullUrl")
    public String fullUrlResponse(MediaResponse mediaResponse) {
        if (!StringUtils.hasText(mediaResponse.getPath())) return null;
        return join(mediaBaseUrl, mediaResponse.getPath());
    }
 
    @SchemaMapping(typeName = "MediaResponse", field = "fullThumbUrl")
    public String fullThumbUrlResponse(MediaResponse mediaResponse) {
        if (!StringUtils.hasText(mediaResponse.getThumbPath())) return null;
        return join(mediaBaseUrl, mediaResponse.getThumbPath());
    }
 
    private String join(String base, String path) {
        if (!StringUtils.hasText(base)) return path;
        String b = base.endsWith("/") ? base.substring(0, base.length() - 1) : base;
        String p = path.startsWith("/") ? path.substring(1) : path;
        return b + "/" + p;
    }
}