Load image resources separately

This commit is contained in:
ViacheslavKlimov 2024-11-11 15:52:42 +02:00
parent 9e2e418cab
commit cd146a9e74
7 changed files with 53 additions and 16 deletions

File diff suppressed because one or more lines are too long

View File

@ -23,6 +23,7 @@ import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.ResourceType; import org.thingsboard.server.common.data.ResourceType;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.msg.queue.ServiceType; import org.thingsboard.server.common.msg.queue.ServiceType;
import org.thingsboard.server.dao.resource.ImageService;
import org.thingsboard.server.dao.resource.ResourceService; import org.thingsboard.server.dao.resource.ResourceService;
import org.thingsboard.server.dao.widget.WidgetsBundleService; import org.thingsboard.server.dao.widget.WidgetsBundleService;
import org.thingsboard.server.queue.discovery.PartitionService; import org.thingsboard.server.queue.discovery.PartitionService;
@ -32,6 +33,7 @@ import org.thingsboard.server.service.sync.GitSyncService;
import org.thingsboard.server.service.sync.vc.GitRepository.FileType; import org.thingsboard.server.service.sync.vc.GitRepository.FileType;
import org.thingsboard.server.service.sync.vc.GitRepository.RepoFile; import org.thingsboard.server.service.sync.vc.GitRepository.RepoFile;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -45,6 +47,7 @@ public class DashboardSyncService {
private final GitSyncService gitSyncService; private final GitSyncService gitSyncService;
private final ResourceService resourceService; private final ResourceService resourceService;
private final ImageService imageService;
private final WidgetsBundleService widgetsBundleService; private final WidgetsBundleService widgetsBundleService;
private final PartitionService partitionService; private final PartitionService partitionService;
@ -70,9 +73,14 @@ public class DashboardSyncService {
List<RepoFile> resources = listFiles("resources"); List<RepoFile> resources = listFiles("resources");
for (RepoFile resourceFile : resources) { for (RepoFile resourceFile : resources) {
String data = getFileContent(resourceFile.path()); byte[] data = getFileContent(resourceFile.path()).getBytes(StandardCharsets.UTF_8);
resourceService.createOrUpdateSystemResource(ResourceType.JS_MODULE, resourceFile.name(), data); resourceService.createOrUpdateSystemResource(ResourceType.JS_MODULE, resourceFile.name(), data);
} }
List<RepoFile> images = listFiles("images");
for (RepoFile imageFile : images) {
byte[] data = getFileContent(imageFile.path()).getBytes(StandardCharsets.UTF_8);
imageService.createOrUpdateSystemImage(imageFile.name(), data);
}
Stream<String> widgetsBundles = listFiles("widget_bundles").stream() Stream<String> widgetsBundles = listFiles("widget_bundles").stream()
.map(widgetsBundleFile -> getFileContent(widgetsBundleFile.path())); .map(widgetsBundleFile -> getFileContent(widgetsBundleFile.path()));
@ -80,11 +88,8 @@ public class DashboardSyncService {
.map(widgetTypeFile -> getFileContent(widgetTypeFile.path())); .map(widgetTypeFile -> getFileContent(widgetTypeFile.path()));
widgetsBundleService.updateSystemWidgets(widgetsBundles, widgetTypes); widgetsBundleService.updateSystemWidgets(widgetsBundles, widgetTypes);
// TODO: read images folder and save images
RepoFile dashboardFile = listFiles("dashboards").get(0); RepoFile dashboardFile = listFiles("dashboards").get(0);
String dashboardJson = getFileContent(dashboardFile.path()); resourceService.createOrUpdateSystemResource(ResourceType.DASHBOARD, GATEWAYS_DASHBOARD_KEY, getFileContent(dashboardFile.path()).getBytes(StandardCharsets.UTF_8));
resourceService.createOrUpdateSystemResource(ResourceType.DASHBOARD, GATEWAYS_DASHBOARD_KEY, dashboardJson);
log.info("Gateways dashboard sync completed"); log.info("Gateways dashboard sync completed");
} }

View File

@ -416,6 +416,8 @@ public class InstallScripts {
} }
Path resourcesDir = Path.of(getDataDir(), RESOURCES_DIR); Path resourcesDir = Path.of(getDataDir(), RESOURCES_DIR);
loadSystemResources(resourcesDir.resolve("images"), ResourceType.IMAGE);
loadSystemResources(resourcesDir.resolve("js_modules"), ResourceType.JS_MODULE);
loadSystemResources(resourcesDir.resolve("dashboards"), ResourceType.DASHBOARD); loadSystemResources(resourcesDir.resolve("dashboards"), ResourceType.DASHBOARD);
} }
@ -520,18 +522,21 @@ public class InstallScripts {
listDir(dir).forEach(resourceFile -> { listDir(dir).forEach(resourceFile -> {
String resourceKey = resourceFile.getFileName().toString(); String resourceKey = resourceFile.getFileName().toString();
try { try {
String data = getContent(resourceFile); byte[] data = getContent(resourceFile);
TbResource resource = resourceService.createOrUpdateSystemResource(resourceType, resourceKey, data); if (resourceType == ResourceType.IMAGE) {
log.info("{} resource {}", (resource.getId() == null ? "Created" : "Updated"), resourceKey); imageService.createOrUpdateSystemImage(resourceKey, data);
} else {
resourceService.createOrUpdateSystemResource(resourceType, resourceKey, data);
}
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Unable to load system resource " + resourceFile, e); throw new RuntimeException("Unable to load system resource " + resourceFile, e);
} }
}); });
} }
private String getContent(Path file) { private byte[] getContent(Path file) {
try { try {
return Files.readString(file); return Files.readAllBytes(file);
} catch (IOException e) { } catch (IOException e) {
throw new UncheckedIOException(e); throw new UncheckedIOException(e);
} }

View File

@ -76,4 +76,6 @@ public interface ImageService {
void inlineImagesForEdge(WidgetTypeDetails widgetTypeDetails); void inlineImagesForEdge(WidgetTypeDetails widgetTypeDetails);
TbResourceInfo createOrUpdateSystemImage(String resourceKey, byte[] data);
} }

View File

@ -88,6 +88,6 @@ public interface ResourceService extends EntityDaoService {
List<TbResourceInfo> getUsedResources(WidgetTypeDetails widgetTypeDetails); List<TbResourceInfo> getUsedResources(WidgetTypeDetails widgetTypeDetails);
TbResource createOrUpdateSystemResource(ResourceType resourceType, String resourceKey, String data); TbResource createOrUpdateSystemResource(ResourceType resourceType, String resourceKey, byte[] data);
} }

View File

@ -315,6 +315,30 @@ public class BaseImageService extends BaseResourceService implements ImageServic
return result.success(success).build(); return result.success(success).build();
} }
@Override
public TbResourceInfo createOrUpdateSystemImage(String resourceKey, byte[] data) {
TbResource image;
TbResourceInfo existingImage = findResourceInfoByTenantIdAndKey(TenantId.SYS_TENANT_ID, ResourceType.IMAGE, resourceKey);
if (existingImage != null) {
image = new TbResource(existingImage);
} else {
image = new TbResource();
image.setTenantId(TenantId.SYS_TENANT_ID);
image.setFileName(resourceKey);
image.setTitle(resourceKey);
image.setResourceKey(resourceKey);
image.setResourceType(ResourceType.IMAGE);
image.setResourceSubType(ResourceSubType.IMAGE);
}
ImageDescriptor descriptor = new ImageDescriptor();
descriptor.setMediaType(ImageUtils.fileExtensionToMediaType(StringUtils.substringAfterLast(resourceKey, ".")));
image.setDescriptorValue(descriptor);
image.setData(data);
image.setPublic(true);
log.debug("{} system image {}", (image.getId() == null ? "Creating" : "Updating"), resourceKey);
return saveImage(image);
}
@Override @Override
public String calculateImageEtag(byte[] imageData) { public String calculateImageEtag(byte[] imageData) {
return calculateEtag(imageData); return calculateEtag(imageData);

View File

@ -546,9 +546,9 @@ public class BaseResourceService extends AbstractCachedEntityService<ResourceInf
} }
@Override @Override
public TbResource createOrUpdateSystemResource(ResourceType resourceType, String resourceKey, String data) { public TbResource createOrUpdateSystemResource(ResourceType resourceType, String resourceKey, byte[] data) {
if (resourceType == ResourceType.DASHBOARD) { if (resourceType == ResourceType.DASHBOARD) {
Dashboard dashboard = JacksonUtil.fromString(data, Dashboard.class); Dashboard dashboard = JacksonUtil.fromBytes(data, Dashboard.class);
dashboard.setTenantId(TenantId.SYS_TENANT_ID); dashboard.setTenantId(TenantId.SYS_TENANT_ID);
if (CollectionUtils.isNotEmpty(dashboard.getResources())) { if (CollectionUtils.isNotEmpty(dashboard.getResources())) {
importResources(dashboard.getTenantId(), dashboard.getResources()); importResources(dashboard.getTenantId(), dashboard.getResources());
@ -556,7 +556,7 @@ public class BaseResourceService extends AbstractCachedEntityService<ResourceInf
imageService.updateImagesUsage(dashboard); imageService.updateImagesUsage(dashboard);
updateResourcesUsage(dashboard); updateResourcesUsage(dashboard);
data = JacksonUtil.toString(dashboard); data = JacksonUtil.writeValueAsBytes(dashboard);
} }
TbResource resource = findResourceByTenantIdAndKey(TenantId.SYS_TENANT_ID, resourceType, resourceKey); TbResource resource = findResourceByTenantIdAndKey(TenantId.SYS_TENANT_ID, resourceType, resourceKey);
@ -568,8 +568,8 @@ public class BaseResourceService extends AbstractCachedEntityService<ResourceInf
resource.setFileName(resourceKey); resource.setFileName(resourceKey);
resource.setTitle(resourceKey); resource.setTitle(resourceKey);
} }
resource.setData(data.getBytes(StandardCharsets.UTF_8)); resource.setData(data);
log.debug("{} system resource {}", (resource.getId() == null ? "Creating" : "Updating"), resourceKey); log.info("{} system resource {}", (resource.getId() == null ? "Creating" : "Updating"), resourceKey);
return saveResource(resource); return saveResource(resource);
} }