Skip to content

Commit

Permalink
fix: bundles urls (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ authored Nov 1, 2024
1 parent e4d4425 commit 69b99bb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/main/java/com/crowdin/ui/panel/download/DownloadWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.Optional;

import static com.crowdin.Constants.*;
import static com.crowdin.util.Util.extractOrganization;
import static com.crowdin.util.Util.isEnterpriseUrl;

public class DownloadWindow implements ContentTab {

Expand Down Expand Up @@ -183,17 +185,17 @@ public String buildBundleUrl() {
var bundle = this.getSelectedBundle();

if (bundle == null) {
if (this.baseUrl != null) {
String base = this.baseUrl.endsWith("/") ? this.baseUrl : this.baseUrl + "/";
return base + "u/projects/" + project.getId() + "/download";
if (this.baseUrl != null && isEnterpriseUrl(this.baseUrl)) {
String organization = extractOrganization(this.baseUrl);
return "https://" + organization + ".crowdin.com/u/projects/" + project.getId() + "/download";
} else {
return "https://crowdin.com/project/" + project.getIdentifier() + "/download#bundles";
}
}

if (this.baseUrl != null) {
String base = this.baseUrl.endsWith("/") ? this.baseUrl : this.baseUrl + "/";
return base + "u/projects/" + project.getId() + "/translations/bundle/" + bundle.getId();
if (this.baseUrl != null && isEnterpriseUrl(this.baseUrl)) {
String organization = extractOrganization(this.baseUrl);
return "https://" + organization + ".crowdin.com/u/projects/" + project.getId() + "/translations/bundle/" + bundle.getId();
} else {
return "https://crowdin.com/project/" + project.getIdentifier() + "/download#bundles:" + bundle.getId();
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/crowdin/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,15 @@ public static boolean isFileFormatNotAllowed(Exception e) {
return e.getMessage().contains("files are not allowed to upload in string-based projects") ||
e.getMessage().contains("files are not allowed to upload in strings-based projects");
}

public static String extractOrganization(String baseUrl) {
if (baseUrl.contains(".api.crowdin.com")) {
return baseUrl.split(".api.crowdin.com")[0].split("https://")[1];
}
return baseUrl.split(".crowdin.com")[0].split("https://")[1];
}

public static boolean isEnterpriseUrl(String baseUrl) {
return !baseUrl.contains("https://api.crowdin.com");
}
}
2 changes: 1 addition & 1 deletion src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ errors.config.invalid_format=Property '%s' has wrong format
errors.config.has_errors=Errors in the configuration file:
errors.config.missing_config_file=File <b>'%s'</b> with Crowdin plugin configuration doesn't exist in the project root directory
errors.config.missing_property=Required property <b>'%s'</b> is missing in the configuration file
errors.config.invalid_url_property=Base URL contains <b>'%s'</b> unexpected value. The expected format is 'https://crowdin.com' or 'https://{domain_name}.api.crowdin.com'
errors.config.invalid_url_property=Base URL contains <b>'%s'</b> unexpected value. The expected format is 'https://api.crowdin.com' or 'https://{domain_name}.api.crowdin.com'
errors.config.invalid_url_env=Environment variable <b>'%s'</b> contains unexpected <b>'%s'</b> value. The expected format is 'https://crowdin.com' or 'https://{domain_name}.crowdin.com'

errors.extract_file=Failed to extract the file '%s'
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/crowdin/util/UtilTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.crowdin.util;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -27,4 +28,13 @@ public static Stream<Arguments> testPrepareListMessageText() {
"</ul></body>")
);
}

@Test
public void extractOrganizationTest() {
Assertions.assertEquals("test", Util.extractOrganization("https://test.api.crowdin.com"));
Assertions.assertEquals("test342", Util.extractOrganization("https://test342.crowdin.com"));
Assertions.assertEquals("org-1", Util.extractOrganization("https://org-1.api.crowdin.com"));
Assertions.assertEquals("org-test", Util.extractOrganization("https://org-test.api.crowdin.com"));
Assertions.assertEquals("org-test", Util.extractOrganization("https://org-test.api.crowdin.com/"));
}
}

0 comments on commit 69b99bb

Please sign in to comment.