Fix conflicts

This commit is contained in:
Igor Kulikov 2023-11-21 11:28:52 +02:00
commit 12f5c25698
3 changed files with 26 additions and 10 deletions

View File

@ -19,6 +19,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.CacheControl; import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
@ -54,6 +55,8 @@ import org.thingsboard.server.service.security.model.SecurityUser;
import org.thingsboard.server.service.security.permission.Operation; import org.thingsboard.server.service.security.permission.Operation;
import org.thingsboard.server.service.security.permission.Resource; import org.thingsboard.server.service.security.permission.Resource;
import java.util.concurrent.TimeUnit;
import static org.thingsboard.server.controller.ControllerConstants.PAGE_NUMBER_DESCRIPTION; import static org.thingsboard.server.controller.ControllerConstants.PAGE_NUMBER_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.PAGE_SIZE_DESCRIPTION; import static org.thingsboard.server.controller.ControllerConstants.PAGE_SIZE_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.RESOURCE_SORT_PROPERTY_ALLOWABLE_VALUES; import static org.thingsboard.server.controller.ControllerConstants.RESOURCE_SORT_PROPERTY_ALLOWABLE_VALUES;
@ -70,6 +73,10 @@ public class ImageController extends BaseController {
private final ImageService imageService; private final ImageService imageService;
private final TbImageService tbImageService; private final TbImageService tbImageService;
@Value("${cache.image.systemImagesBrowserTtlInMinutes:0}")
private int systemImagesBrowserTtlInMinutes;
@Value("${cache.image.tenantImagesBrowserTtlInMinutes:0}")
private int tenantImagesBrowserTtlInMinutes;
private static final String IMAGE_URL = "/api/images/{type}/{key}"; private static final String IMAGE_URL = "/api/images/{type}/{key}";
private static final String SYSTEM_IMAGE = "system"; private static final String SYSTEM_IMAGE = "system";
@ -196,14 +203,20 @@ public class ImageController extends BaseController {
data = imageService.getImageData(tenantId, imageInfo.getId()); data = imageService.getImageData(tenantId, imageInfo.getId());
} }
tbImageService.putETag(cacheKey, descriptor.getEtag()); tbImageService.putETag(cacheKey, descriptor.getEtag());
return ResponseEntity.ok() var result = ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName) .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName)
.header("x-filename", fileName) .header("x-filename", fileName)
.contentLength(data.length)
.header("Content-Type", descriptor.getMediaType()) .header("Content-Type", descriptor.getMediaType())
.cacheControl(CacheControl.noCache()) .contentLength(data.length)
.eTag(descriptor.getEtag()) .eTag(descriptor.getEtag());
.body(new ByteArrayResource(data)); if (systemImagesBrowserTtlInMinutes > 0 && imageInfo.getTenantId().isSysTenantId()) {
result.cacheControl(CacheControl.maxAge(systemImagesBrowserTtlInMinutes, TimeUnit.MINUTES));
} else if (tenantImagesBrowserTtlInMinutes > 0 && !imageInfo.getTenantId().isSysTenantId()) {
result.cacheControl(CacheControl.maxAge(tenantImagesBrowserTtlInMinutes, TimeUnit.MINUTES));
} else {
result.cacheControl(CacheControl.noCache());
}
return result.body(new ByteArrayResource(data));
} }
private TbResourceInfo checkImageInfo(String imageType, String key, Operation operation) throws ThingsboardException { private TbResourceInfo checkImageInfo(String imageType, String key, Operation operation) throws ThingsboardException {

View File

@ -41,8 +41,8 @@ public class DefaultTbImageService extends AbstractTbEntityService implements Tb
private final Cache<ImageCacheKey, String> cache; private final Cache<ImageCacheKey, String> cache;
public DefaultTbImageService(ImageService imageService, public DefaultTbImageService(ImageService imageService,
@Value("${cache.imageETags.timeToLiveInMinutes:120}") int cacheTtl, @Value("${cache.image.etag.timeToLiveInMinutes:120}") int cacheTtl,
@Value("${cache.imageETags.maxSize:200000}") int cacheMaxSize) { @Value("${cache.image.etag.maxSize:200000}") int cacheMaxSize) {
this.imageService = imageService; this.imageService = imageService;
this.cache = Caffeine.newBuilder() this.cache = Caffeine.newBuilder()
.expireAfterAccess(cacheTtl, TimeUnit.MINUTES) .expireAfterAccess(cacheTtl, TimeUnit.MINUTES)

View File

@ -586,9 +586,12 @@ cache:
entityLimits: entityLimits:
timeToLiveInMinutes: "${CACHE_SPECS_ENTITY_LIMITS_TTL:5}" # Entity limits cache TTL timeToLiveInMinutes: "${CACHE_SPECS_ENTITY_LIMITS_TTL:5}" # Entity limits cache TTL
maxSize: "${CACHE_SPECS_ENTITY_LIMITS_MAX_SIZE:100000}" # 0 means the cache is disabled maxSize: "${CACHE_SPECS_ENTITY_LIMITS_MAX_SIZE:100000}" # 0 means the cache is disabled
imageETags: image:
etag:
timeToLiveInMinutes: "${CACHE_SPECS_IMAGE_ETAGS_TTL:44640}" # Image ETags cache TTL timeToLiveInMinutes: "${CACHE_SPECS_IMAGE_ETAGS_TTL:44640}" # Image ETags cache TTL
maxSize: "${CACHE_SPECS_IMAGE_ETAGS_MAX_SIZE:1000000}" # 0 means the cache is disabled maxSize: "${CACHE_SPECS_IMAGE_ETAGS_MAX_SIZE:1000000}" # 0 means the cache is disabled
systemImagesBrowserTtlInMinutes: "${CACHE_SPECS_IMAGE_SYSTEM_BROWSER_TTL:0}" # Browser cache TTL for system images in minutes. 0 means the cache is disabled
tenantImagesBrowserTtlInMinutes: "${CACHE_SPECS_IMAGE_TENANT_BROWSER_TTL:0}" # Browser cache TTL for tenant images in minutes. 0 means the cache is disabled
# Spring data parameters # Spring data parameters
spring.data.redis.repositories.enabled: false # Disable this because it is not required. spring.data.redis.repositories.enabled: false # Disable this because it is not required.