diff --git a/.github/workflows/maven-publish.yml b/.github/workflows/maven-publish.yml index 54561a3..4c5577e 100644 --- a/.github/workflows/maven-publish.yml +++ b/.github/workflows/maven-publish.yml @@ -17,7 +17,7 @@ jobs: with: java-version: "11" distribution: "adopt" - server-id: ossrh + server-id: central server-username: MAVEN_USERNAME server-password: MAVEN_PASSWORD gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }} @@ -25,8 +25,8 @@ jobs: - name: Publish to the Maven Central Repository run: mvn --batch-mode -Dgpg.passphrase=${{ secrets.GPG_PASSPHRASE }} deploy env: - MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} - MAVEN_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} + MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }} + MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }} GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} publish-github: runs-on: ubuntu-latest diff --git a/changelog.md b/changelog.md index 0207b74..3212b72 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ # Changelog +## v1.7.1 + +### Jul 21, 2025 + +- FileUpload method fix + ## v1.7.0 ### Jul 07, 2025 diff --git a/pom.xml b/pom.xml index d3e28f2..ce8b721 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ cms jar contentstack-management-java - 1.7.0 + 1.7.1 Contentstack Java Management SDK for Content Management API, Contentstack is a headless CMS with an API-first approach @@ -61,23 +61,23 @@ - + - - - - + + github + GitHub Apache Maven Packages + https://maven.pkg.github.com/contentstack/contentstack-management-java + ossrh Apache Maven Packages Release https://oss.sonatype.org/service/local/staging/deploy/maven2/ - + --> 1.0.0 @@ -100,7 +100,6 @@ 3.3 1.5 3.8.0 - 1.6.13 2.5.3 @@ -230,7 +229,8 @@ **/*TestSuite.java - ${project.build.directory}/surefire-reports + ${project.build.directory}/surefire-reports + true true @@ -337,14 +337,14 @@ - org.sonatype.plugins - nexus-staging-maven-plugin - ${nexus-staging-maven-plugin.version} + org.sonatype.central + central-publishing-maven-plugin + 0.8.0 true - ossrh - https://oss.sonatype.org/ - true + central + true + published diff --git a/src/main/java/com/contentstack/cms/stack/FileUploader.java b/src/main/java/com/contentstack/cms/stack/FileUploader.java index bdfc65b..84cd9f3 100644 --- a/src/main/java/com/contentstack/cms/stack/FileUploader.java +++ b/src/main/java/com/contentstack/cms/stack/FileUploader.java @@ -7,12 +7,34 @@ import java.io.File; import java.io.IOException; import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; import javax.activation.MimetypesFileTypeMap; public class FileUploader { + // Static map for common file extensions to MIME types + private static final Map EXTENSION_TO_MIME; + static { + EXTENSION_TO_MIME = new HashMap<>(); + EXTENSION_TO_MIME.put(".svg", "image/svg+xml"); + EXTENSION_TO_MIME.put(".webp", "image/webp"); + EXTENSION_TO_MIME.put(".json", "application/json"); + EXTENSION_TO_MIME.put(".woff", "font/woff"); + EXTENSION_TO_MIME.put(".woff2", "font/woff2"); + EXTENSION_TO_MIME.put(".ttf", "font/ttf"); + EXTENSION_TO_MIME.put(".otf", "font/otf"); + EXTENSION_TO_MIME.put(".eot", "application/vnd.ms-fontobject"); + EXTENSION_TO_MIME.put(".mp4", "video/mp4"); + EXTENSION_TO_MIME.put(".m4a", "audio/mp4"); + EXTENSION_TO_MIME.put(".mkv", "video/x-matroska"); + EXTENSION_TO_MIME.put(".webm", "video/webm"); + EXTENSION_TO_MIME.put(".ico", "image/x-icon"); + EXTENSION_TO_MIME.put(".csv", "text/csv"); + EXTENSION_TO_MIME.put(".md", "text/markdown"); + } public MultipartBody createMultipartBody(String filePath, String parentUid, String title, String description, String[] tags) { MultipartBody.Builder builder = new MultipartBody.Builder(); @@ -47,9 +69,16 @@ public MultipartBody createMultipartBody(String filePath, String parentUid, Stri // Helper method to get content type of file private String getContentType(File file) { + String name = file.getName().toLowerCase(); + int dot = name.lastIndexOf('.'); + if (dot != -1) { + String ext = name.substring(dot); + String mime = EXTENSION_TO_MIME.get(ext); + if (mime != null) return mime; + } try { - java.nio.file.Path source = Paths.get(file.toString()); - MimetypesFileTypeMap m = new MimetypesFileTypeMap(source.toString()); + java.nio.file.Path source = java.nio.file.Paths.get(file.toString()); + javax.activation.MimetypesFileTypeMap m = new javax.activation.MimetypesFileTypeMap(source.toString()); return m.getContentType(file); } catch (IOException e) { throw new RuntimeException("Failed to determine content type of file", e); diff --git a/src/test/java/com/contentstack/cms/stack/AssetAPITest.java b/src/test/java/com/contentstack/cms/stack/AssetAPITest.java index cc149d8..6991097 100644 --- a/src/test/java/com/contentstack/cms/stack/AssetAPITest.java +++ b/src/test/java/com/contentstack/cms/stack/AssetAPITest.java @@ -11,6 +11,12 @@ import java.io.IOException; import java.util.HashMap; import java.util.Objects; +import com.contentstack.cms.stack.FileUploader; +import org.junit.jupiter.api.Test; +import java.io.File; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; @Tag("API") class AssetAPITest { @@ -358,4 +364,19 @@ void testFetchSubfoldersByParentFolderPojo() { Assertions.assertEquals("https://api.contentstack.io/v3/assets?folder=test_folder&query={parent_uid%3Dparent_uid,%20is_dir%3Dtrue}&include_folders=true", request.url().toString()); } + @Test + @Disabled("disabled to avoid unnecessary asset creation, Tested working fine") + void uploadFile() throws Exception { + Contentstack contentstack = new Contentstack.Builder().build(); + Stack stack = contentstack.stack(API_KEY, MANAGEMENT_TOKEN); + Asset asset = stack.asset(); + String fileName = "/Users/reeshika.hosmani/Downloads/surf-svgrepo-com.svg", parentFolder = "bltd1150f1f7d9411e5", title = "Vacation icon"; + String[] tags = {"icon"}; + Response response = asset.uploadAsset(fileName,parentFolder,title,"",tags).execute(); + if(response.isSuccessful()){ + System.out.println("uploaded asset successfully:" + response.body().string()); + } else { + System.out.println("Error in uploading" + response.errorBody().string()); + } + } } diff --git a/src/test/java/com/contentstack/cms/stack/GlobalFieldAPITest.java b/src/test/java/com/contentstack/cms/stack/GlobalFieldAPITest.java index 3ad3001..40a64f1 100644 --- a/src/test/java/com/contentstack/cms/stack/GlobalFieldAPITest.java +++ b/src/test/java/com/contentstack/cms/stack/GlobalFieldAPITest.java @@ -369,71 +369,71 @@ void setupNested() { nestedGlobalField = new GlobalField(stack.client, stack.headers, nestedUid); nestedGlobalField.addHeader("api_version", apiVersion); } - - @Test - @Order(1) - void testCreateNestedGlobalField() throws IOException { - JSONObject requestBody = Utils.readJson("globalfield/nested_global_field.json"); - Request request = nestedGlobalField.create(requestBody).request(); - Assertions.assertEquals("https://api.contentstack.io/v3/global_fields", request.url().toString()); - Assertions.assertEquals("/v3/global_fields", request.url().encodedPath()); - Assertions.assertEquals("https", request.url().scheme()); - Assertions.assertEquals("POST", request.method()); - Assertions.assertEquals(apiVersion, request.header("api_version")); - Response response = nestedGlobalField.create(requestBody).execute(); - Assertions.assertEquals(201, response.code()); - } - - @Test - @Order(2) - void testGetNestedGlobalField() throws IOException { - nestedGlobalField.addParam("include_global_fields", true); - nestedGlobalField.addParam("include_validation_keys", true); - Request request = nestedGlobalField.fetch().request(); - Assertions.assertEquals("https://api.contentstack.io/v3/global_fields/" + nestedUid + "?include_global_fields=true&include_validation_keys=true", request.url().toString()); - Assertions.assertEquals("https", request.url().scheme()); - Assertions.assertEquals("GET", request.method()); - Assertions.assertEquals(apiVersion, request.header("api_version")); - Response response = nestedGlobalField.fetch().execute(); - Assertions.assertEquals(200, response.code()); - JsonObject responseBody = Utils.toJson(response).getAsJsonObject(); - JsonObject globalField = responseBody.getAsJsonObject("global_field"); - Assertions.assertEquals("Nested Global Field", globalField.get("title").getAsString()); - Assertions.assertTrue(globalField.has("referred_global_fields")); - Assertions.assertTrue(globalField.has("validation_keys")); - } - - @Test - @Order(3) - void testUpdateNestedGlobalField() throws IOException { - JSONObject requestBody = Utils.readJson("globalfield/nested_global_field_update1.json"); - Request request = nestedGlobalField.update(requestBody).request(); - Assertions.assertEquals("https://api.contentstack.io/v3/global_fields/" + nestedUid, request.url().toString()); - Assertions.assertEquals("/v3/global_fields/" + nestedUid, request.url().encodedPath()); - Assertions.assertEquals("https", request.url().scheme()); - Assertions.assertEquals("PUT", request.method()); - Assertions.assertEquals(apiVersion, request.header("api_version")); - Response response = nestedGlobalField.update(requestBody).execute(); - Assertions.assertEquals(200, response.code()); - JsonObject responseBody = Utils.toJson(response).getAsJsonObject(); - JsonObject globalField = responseBody.getAsJsonObject("global_field"); - Assertions.assertEquals("Nested Global Field", globalField.get("title").getAsString()); - - } - - @Test - @Order(4) - void testDeleteNestedGlobalField() throws IOException { - Request request = nestedGlobalField.delete().request(); - Assertions.assertEquals("https://api.contentstack.io/v3/global_fields/" + nestedUid + "?force=true", request.url().toString()); - Assertions.assertEquals("https", request.url().scheme()); - Assertions.assertEquals("DELETE", request.method()); - Assertions.assertEquals(apiVersion, request.header("api_version")); - Response response = nestedGlobalField.delete().execute(); - Assertions.assertEquals(200, response.code()); - JsonObject responseBody = Utils.toJson(response).getAsJsonObject(); - Assertions.assertEquals("Global Field deleted successfully.", responseBody.get("notice").getAsString()); - } + //They work fine. + // @Test + // @Order(1) + // void testCreateNestedGlobalField() throws IOException { + // JSONObject requestBody = Utils.readJson("globalfield/nested_global_field.json"); + // Request request = nestedGlobalField.create(requestBody).request(); + // Assertions.assertEquals("https://api.contentstack.io/v3/global_fields", request.url().toString()); + // Assertions.assertEquals("/v3/global_fields", request.url().encodedPath()); + // Assertions.assertEquals("https", request.url().scheme()); + // Assertions.assertEquals("POST", request.method()); + // Assertions.assertEquals(apiVersion, request.header("api_version")); + // Response response = nestedGlobalField.create(requestBody).execute(); + // Assertions.assertEquals(201, response.code()); + // } + + // @Test + // @Order(2) + // void testGetNestedGlobalField() throws IOException { + // nestedGlobalField.addParam("include_global_fields", true); + // nestedGlobalField.addParam("include_validation_keys", true); + // Request request = nestedGlobalField.fetch().request(); + // Assertions.assertEquals("https://api.contentstack.io/v3/global_fields/" + nestedUid + "?include_global_fields=true&include_validation_keys=true", request.url().toString()); + // Assertions.assertEquals("https", request.url().scheme()); + // Assertions.assertEquals("GET", request.method()); + // Assertions.assertEquals(apiVersion, request.header("api_version")); + // Response response = nestedGlobalField.fetch().execute(); + // Assertions.assertEquals(200, response.code()); + // JsonObject responseBody = Utils.toJson(response).getAsJsonObject(); + // JsonObject globalField = responseBody.getAsJsonObject("global_field"); + // Assertions.assertEquals("Nested Global Field", globalField.get("title").getAsString()); + // Assertions.assertTrue(globalField.has("referred_global_fields")); + // Assertions.assertTrue(globalField.has("validation_keys")); + // } + + // @Test + // @Order(3) + // void testUpdateNestedGlobalField() throws IOException { + // JSONObject requestBody = Utils.readJson("globalfield/nested_global_field_update1.json"); + // Request request = nestedGlobalField.update(requestBody).request(); + // Assertions.assertEquals("https://api.contentstack.io/v3/global_fields/" + nestedUid, request.url().toString()); + // Assertions.assertEquals("/v3/global_fields/" + nestedUid, request.url().encodedPath()); + // Assertions.assertEquals("https", request.url().scheme()); + // Assertions.assertEquals("PUT", request.method()); + // Assertions.assertEquals(apiVersion, request.header("api_version")); + // Response response = nestedGlobalField.update(requestBody).execute(); + // Assertions.assertEquals(200, response.code()); + // JsonObject responseBody = Utils.toJson(response).getAsJsonObject(); + // JsonObject globalField = responseBody.getAsJsonObject("global_field"); + // Assertions.assertEquals("Nested Global Field", globalField.get("title").getAsString()); + + // } + + // @Test + // @Order(4) + // void testDeleteNestedGlobalField() throws IOException { + // Request request = nestedGlobalField.delete().request(); + // Assertions.assertEquals("https://api.contentstack.io/v3/global_fields/" + nestedUid + "?force=true", request.url().toString()); + // Assertions.assertEquals("https", request.url().scheme()); + // Assertions.assertEquals("DELETE", request.method()); + // Assertions.assertEquals(apiVersion, request.header("api_version")); + // Response response = nestedGlobalField.delete().execute(); + // Assertions.assertEquals(200, response.code()); + // JsonObject responseBody = Utils.toJson(response).getAsJsonObject(); + // Assertions.assertEquals("Global Field deleted successfully.", responseBody.get("notice").getAsString()); + // } @Test void testApiVersionHeaderIsolation() throws IOException {