From bfbb0a7fbd4c53e4949103d7b5a66d53b3494461 Mon Sep 17 00:00:00 2001 From: ViacheslavKlimov Date: Thu, 21 Nov 2024 12:02:08 +0200 Subject: [PATCH] Fix Git file content downloading; fix gateway image name --- ...gateways_dashboard.png => gateway-dashboard.png} | Bin .../entitiy/dashboard/DashboardSyncService.java | 12 ++++++------ .../server/service/sync/DefaultGitSyncService.java | 9 ++------- .../server/service/sync/GitSyncService.java | 2 +- .../sync/vc/DefaultGitRepositoryService.java | 2 +- .../server/service/sync/vc/GitRepository.java | 11 +++++------ 6 files changed, 15 insertions(+), 21 deletions(-) rename application/src/main/data/resources/images/{gateways_dashboard.png => gateway-dashboard.png} (100%) diff --git a/application/src/main/data/resources/images/gateways_dashboard.png b/application/src/main/data/resources/images/gateway-dashboard.png similarity index 100% rename from application/src/main/data/resources/images/gateways_dashboard.png rename to application/src/main/data/resources/images/gateway-dashboard.png diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/dashboard/DashboardSyncService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/dashboard/DashboardSyncService.java index dcb382263f..b56ae8d46f 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/dashboard/DashboardSyncService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/dashboard/DashboardSyncService.java @@ -73,23 +73,23 @@ public class DashboardSyncService { List resources = listFiles("resources"); for (RepoFile resourceFile : resources) { - byte[] data = getFileContent(resourceFile.path()).getBytes(StandardCharsets.UTF_8); + byte[] data = getFileContent(resourceFile.path()); resourceService.createOrUpdateSystemResource(ResourceType.JS_MODULE, resourceFile.name(), data); } List images = listFiles("images"); for (RepoFile imageFile : images) { - byte[] data = getFileContent(imageFile.path()).getBytes(StandardCharsets.UTF_8); + byte[] data = getFileContent(imageFile.path()); imageService.createOrUpdateSystemImage(imageFile.name(), data); } Stream widgetsBundles = listFiles("widget_bundles").stream() - .map(widgetsBundleFile -> getFileContent(widgetsBundleFile.path())); + .map(widgetsBundleFile -> new String(getFileContent(widgetsBundleFile.path()), StandardCharsets.UTF_8)); Stream widgetTypes = listFiles("widget_types").stream() - .map(widgetTypeFile -> getFileContent(widgetTypeFile.path())); + .map(widgetTypeFile -> new String(getFileContent(widgetTypeFile.path()), StandardCharsets.UTF_8)); widgetsBundleService.updateSystemWidgets(widgetsBundles, widgetTypes); RepoFile dashboardFile = listFiles("dashboards").get(0); - resourceService.createOrUpdateSystemResource(ResourceType.DASHBOARD, GATEWAYS_DASHBOARD_KEY, getFileContent(dashboardFile.path()).getBytes(StandardCharsets.UTF_8)); + resourceService.createOrUpdateSystemResource(ResourceType.DASHBOARD, GATEWAYS_DASHBOARD_KEY, getFileContent(dashboardFile.path())); log.info("Gateways dashboard sync completed"); } @@ -98,7 +98,7 @@ public class DashboardSyncService { return gitSyncService.listFiles(REPO_KEY, path, 1, FileType.FILE); } - private String getFileContent(String path) { + private byte[] getFileContent(String path) { return gitSyncService.getFileContent(REPO_KEY, path); } diff --git a/application/src/main/java/org/thingsboard/server/service/sync/DefaultGitSyncService.java b/application/src/main/java/org/thingsboard/server/service/sync/DefaultGitSyncService.java index 30a22fe5ca..f5b2f71d12 100644 --- a/application/src/main/java/org/thingsboard/server/service/sync/DefaultGitSyncService.java +++ b/application/src/main/java/org/thingsboard/server/service/sync/DefaultGitSyncService.java @@ -92,14 +92,9 @@ public class DefaultGitSyncService implements GitSyncService { @Override - public String getFileContent(String key, String path) { + public byte[] getFileContent(String key, String path) { GitRepository repository = getRepository(key); - try { - return repository.getFileContentAtCommit(path, getBranchRef(repository)); - } catch (Exception e) { - log.warn("[{}] Failed to get file content for path {}: {}", key, path, e.getMessage()); - return "{}"; - } + return repository.getFileContentAtCommit(path, getBranchRef(repository)); } @Override diff --git a/application/src/main/java/org/thingsboard/server/service/sync/GitSyncService.java b/application/src/main/java/org/thingsboard/server/service/sync/GitSyncService.java index d1a09757f7..6258038cf6 100644 --- a/application/src/main/java/org/thingsboard/server/service/sync/GitSyncService.java +++ b/application/src/main/java/org/thingsboard/server/service/sync/GitSyncService.java @@ -26,7 +26,7 @@ public interface GitSyncService { List listFiles(String key, String path, int depth, FileType type); - String getFileContent(String key, String path); + byte[] getFileContent(String key, String path); String getGithubRawContentUrl(String key, String path); diff --git a/common/version-control/src/main/java/org/thingsboard/server/service/sync/vc/DefaultGitRepositoryService.java b/common/version-control/src/main/java/org/thingsboard/server/service/sync/vc/DefaultGitRepositoryService.java index 239eb18748..e2aaf5e35f 100644 --- a/common/version-control/src/main/java/org/thingsboard/server/service/sync/vc/DefaultGitRepositoryService.java +++ b/common/version-control/src/main/java/org/thingsboard/server/service/sync/vc/DefaultGitRepositoryService.java @@ -172,7 +172,7 @@ public class DefaultGitRepositoryService implements GitRepositoryService { @Override public String getFileContentAtCommit(TenantId tenantId, String relativePath, String versionId) throws IOException { GitRepository repository = checkRepository(tenantId); - return repository.getFileContentAtCommit(relativePath, versionId); + return new String(repository.getFileContentAtCommit(relativePath, versionId), StandardCharsets.UTF_8); } @Override diff --git a/common/version-control/src/main/java/org/thingsboard/server/service/sync/vc/GitRepository.java b/common/version-control/src/main/java/org/thingsboard/server/service/sync/vc/GitRepository.java index 50d1a00b09..33e15baccf 100644 --- a/common/version-control/src/main/java/org/thingsboard/server/service/sync/vc/GitRepository.java +++ b/common/version-control/src/main/java/org/thingsboard/server/service/sync/vc/GitRepository.java @@ -284,8 +284,8 @@ public class GitRepository { return files; } - - public String getFileContentAtCommit(String file, String commitId) throws IOException { + @SneakyThrows + public byte[] getFileContentAtCommit(String file, String commitId) { log.debug("Executing getFileContentAtCommit [{}][{}][{}]", settings.getRepositoryUri(), commitId, file); RevCommit revCommit = resolveCommit(commitId); try (TreeWalk treeWalk = TreeWalk.forPath(git.getRepository(), file, revCommit.getTree())) { @@ -296,8 +296,7 @@ public class GitRepository { try (ObjectReader objectReader = git.getRepository().newObjectReader()) { ObjectLoader objectLoader = objectReader.open(blobId); try { - byte[] bytes = objectLoader.getBytes(); - return new String(bytes, StandardCharsets.UTF_8); + return objectLoader.getBytes(); } catch (LargeObjectException e) { throw new RuntimeException("File " + file + " is too big to load"); } @@ -397,11 +396,11 @@ public class GitRepository { diff.setFilePath(diffEntry.getChangeType() != DiffEntry.ChangeType.DELETE ? diffEntry.getNewPath() : diffEntry.getOldPath()); diff.setChangeType(diffEntry.getChangeType()); try { - diff.setFileContentAtCommit1(getFileContentAtCommit(diff.getFilePath(), commit1)); + diff.setFileContentAtCommit1(new String(getFileContentAtCommit(diff.getFilePath(), commit1), StandardCharsets.UTF_8)); } catch (IllegalArgumentException ignored) { } try { - diff.setFileContentAtCommit2(getFileContentAtCommit(diff.getFilePath(), commit2)); + diff.setFileContentAtCommit2(new String(getFileContentAtCommit(diff.getFilePath(), commit2), StandardCharsets.UTF_8)); } catch (IllegalArgumentException ignored) { } return diff;