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;
|
}
|
}
|