Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asset content_type auto detect and post image accordingly #50

Merged
merged 6 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<artifactId>cms</artifactId>
<packaging>jar</packaging>
<name>contentstack-management-java</name>
<version>1.3.0</version>
<version>1.3.1</version>
<description>Contentstack Java Management SDK for Content Management API, Contentstack is a headless CMS with an
API-first approach
</description>
Expand Down Expand Up @@ -91,10 +91,10 @@
<rxjava-source.version>3.1.8</rxjava-source.version>
<retrofit-source.version>2.9.0</retrofit-source.version>
<converter-gson-version>2.9.0</converter-gson-version>
<logging.version>5.0.0-alpha.11</logging.version>
<logging.version>5.0.0-alpha.12</logging.version>
<jococo-plugin.version>0.8.7</jococo-plugin.version>
<lombok-source.version>1.18.30</lombok-source.version>
<junit-jupiter.version>5.10.0</junit-jupiter.version>
<junit-jupiter.version>5.10.1</junit-jupiter.version>
<junit-jupiter-engine.version>5.8.0-M1</junit-jupiter-engine.version>
<gson.version>2.10.1</gson.version>
<maven-site-plugin.version>3.3</maven-site-plugin.version>
Expand Down Expand Up @@ -138,7 +138,7 @@
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.0.1</version>
<version>24.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -168,7 +168,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.6.0</version>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/contentstack/cms/core/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Util {
// The line `public static final String SDK_VERSION = "1.3.0";`
// named `SDK_VERSION` of type `String`. The value of this constant is set to
// "1.2.0".
public static final String SDK_VERSION = "1.3.0";
public static final String SDK_VERSION = "1.3.1";

static final String PRIVATE_CONSTRUCTOR = "private constructor can't be accessed outside the class";
public static final Boolean RETRY_ON_FAILURE = true;
Expand Down
40 changes: 3 additions & 37 deletions src/main/java/com/contentstack/cms/stack/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,50 +327,16 @@ public Call<ResponseBody> uploadAsset(@NotNull String filePath, String descripti
* @return Call
*/
public Call<ResponseBody> uploadAsset(@NotNull String filePath, String parentUid, String title, String description, String[] tags) {
RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), description);
RequestBody body = RequestBody.create(Objects.requireNonNull(MediaType.parse("multipart/form-data")), description);
MultipartBody.Part partFile = createMultipartBody(filePath, parentUid, title, description, tags);
return this.service.uploadAsset(this.headers, partFile, body, this.params);
}

private String tagConvertor(String[] tags) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < tags.length; i++) {
stringBuilder.append(tags[i]);
if (i < tags.length - 1) {
stringBuilder.append(", ");
}
}
return stringBuilder.toString();
}


private MultipartBody.Part createMultipartBody(String filePath, String parentUid, String title, String description, String[] tags) {
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);

if (!filePath.isEmpty()) {
File file = new File(filePath);
if (file.exists()) {
RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
builder.addFormDataPart("asset[upload]", file.getName(), fileBody);
}
}

// Add other parts
if (parentUid != null) {
builder.addFormDataPart("asset[parent_uid]", parentUid);
}
if (title != null) {
builder.addFormDataPart("asset[title]", title);
}
if (description != null) {
builder.addFormDataPart("asset[description]", description);
}
if (tags != null) {
builder.addFormDataPart("asset[tags]", tagConvertor(tags));
}

return builder.build().part(0);
return new FileUploader().createMultipartBody(filePath, parentUid, title, description, tags);
}


Expand Down Expand Up @@ -403,7 +369,7 @@ private MultipartBody.Part createMultipartBody(String filePath, String parentUid
public Call<ResponseBody> replace(@NotNull String filePath, @NotNull String description) {
Objects.requireNonNull(this.assetUid, "Asset Uid Can Not Be Null OR Empty");
MultipartBody.Part assetPath = uploadFile(filePath);
RequestBody body = RequestBody.create(MediaType.parse(String.valueOf(MultipartBody.FORM)), description);
RequestBody body = RequestBody.create(Objects.requireNonNull(MediaType.parse(String.valueOf(MultipartBody.FORM))), description);
return this.service.replace(this.headers, this.assetUid, assetPath, body, this.params);
}

Expand Down
63 changes: 63 additions & 0 deletions src/main/java/com/contentstack/cms/stack/FileUploader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.contentstack.cms.stack;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Objects;

public class FileUploader {


public MultipartBody.Part createMultipartBody(String filePath, String parentUid, String title, String description, String[] tags) {
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);

// Check if filePath is not empty and file exists
if (!filePath.isEmpty()) {
File file = new File(filePath);
if (file.exists()) {
String contentType = getContentType(file);
RequestBody fileBody = RequestBody.create(Objects.requireNonNull(MediaType.parse(contentType)), file);
builder.addFormDataPart("asset[upload]", file.getName(), fileBody);
}
}

// Add other parts if not null
addFormDataPartIfNotNull(builder, "asset[parent_uid]", parentUid);
addFormDataPartIfNotNull(builder, "asset[title]", title);
addFormDataPartIfNotNull(builder, "asset[description]", description);

// Handle tags array null case
if (tags != null) {
addFormDataPartIfNotNull(builder, "asset[tags]", tagConvertor(tags));
}

return builder.build().part(0);
}

// Helper method to add form data part if value is not null
private void addFormDataPartIfNotNull(MultipartBody.Builder builder, String name, String value) {
if (value != null) {
builder.addFormDataPart(name, value);
}
}

// Helper method to get content type of file
private String getContentType(File file) {
try {
return Files.probeContentType(file.toPath());
} catch (IOException e) {
throw new RuntimeException("Failed to determine content type of file", e);
}
}

// Dummy implementation of tagConvertor method
private String tagConvertor(String[] tags) {
// Implement your tag conversion logic here
return String.join(",", tags);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/contentstack/cms/stack/Taxonomy.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public Terms terms() {
* <p>Example usage:</p>
* <pre>JSONObject object = new JSonObject();</pre>
* <pre>object.put("taxonomies.color", Object)</pre>
* <pre>Taxonomy taxonomy = stack("authtoken").taxonomy("taxonomyId").filterTaxonomy(object);</pre>
* <pre>Taxonomy taxonomy = stack("authtoken").taxonomy("taxonomyId").query(object);</pre>
*
* @param query the query of type @{@link JSONObject}
* @return instance of {@link Call}
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/com/contentstack/cms/stack/AssetAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static void setup() {

@Order(1)
@Test
void testFindAssets() throws IOException {
void testFindAssets() {
asset.clearParams();
asset.addParam("include_folders", true);
asset.addParam("environment", "production");
Expand Down Expand Up @@ -141,10 +141,9 @@ void testAssetUpload() {
asset.addHeader("api_key", API_KEY);
asset.addHeader("authorization", MANAGEMENT_TOKEN);
asset.addHeader("authtoken", AUTHTOKEN);
String filePath = "/Users/shaileshmishra/Desktop/pexels.png";
String description = "The calender has been placed to assets by ishaileshmishra";
String filePath = "/Users/shaileshmishra/Desktop/image.jpeg";
String description = "The calender has been placed to assets by shaileshmishra";
Request request = asset.uploadAsset(filePath, description).request();

// The assertions
Assertions.assertEquals(3, request.headers().size());
Assertions.assertTrue(request.headers().names().contains("api_key"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.contentstack.cms.TestClient;
import okhttp3.Request;
import okhttp3.ResponseBody;
import org.json.simple.JSONObject;
import org.junit.jupiter.api.*;
import retrofit2.Response;

import java.io.IOException;

Expand Down Expand Up @@ -363,4 +365,24 @@ void testPublishWithReference() {
request.url().toString());
}


@Test
public void testEntryQuery() {
JSONObject query = new JSONObject();
query.put("taxonomies.taxonomy_uid", "{ \"$in\" : [\"term_uid1\" , \"term_uid2\" ] }");
Request request = entry.query(query).request();
Assertions.assertEquals(2, request.headers().names().size());
Assertions.assertEquals("GET", request.method());
Assertions.assertTrue(request.url().isHttps());
Assertions.assertEquals("api.contentstack.io", request.url().host());
Assertions.assertEquals(4, request.url().pathSegments().size());
Assertions.assertEquals("v3", request.url().pathSegments().get(0));
Assertions.assertEquals("content_types", request.url().pathSegments().get(1));
Assertions.assertEquals("product", request.url().pathSegments().get(2));
Assertions.assertEquals("entries", request.url().pathSegments().get(3));
Assertions.assertNull(request.body());
Assertions.assertEquals("query={\"taxonomies.taxonomy_uid\":\"{ \\\"$in\\\" : [\\\"term_uid1\\\" , \\\"term_uid2\\\" ] }\"}", request.url().query());

}

}
Loading