Skip to content

Commit be3ed08

Browse files
Merge pull request #50 from contentstack/next
Asset content_type auto detect and post image accordingly
2 parents 5619159 + f1328ec commit be3ed08

File tree

8 files changed

+105
-48
lines changed

8 files changed

+105
-48
lines changed

changelog.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## v1.3.1
4+
5+
### Feb 22, 2024
6+
7+
- Fix:
8+
- auto checking data type of the asset object
9+
310
## v1.3.0
411

512
### Jan 18, 2024

pom.xml

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<artifactId>cms</artifactId>
88
<packaging>jar</packaging>
99
<name>contentstack-management-java</name>
10-
<version>1.3.0</version>
10+
<version>1.3.1</version>
1111
<description>Contentstack Java Management SDK for Content Management API, Contentstack is a headless CMS with an
1212
API-first approach
1313
</description>
@@ -91,10 +91,10 @@
9191
<rxjava-source.version>3.1.8</rxjava-source.version>
9292
<retrofit-source.version>2.9.0</retrofit-source.version>
9393
<converter-gson-version>2.9.0</converter-gson-version>
94-
<logging.version>5.0.0-alpha.11</logging.version>
94+
<logging.version>5.0.0-alpha.12</logging.version>
9595
<jococo-plugin.version>0.8.7</jococo-plugin.version>
9696
<lombok-source.version>1.18.30</lombok-source.version>
97-
<junit-jupiter.version>5.10.0</junit-jupiter.version>
97+
<junit-jupiter.version>5.10.1</junit-jupiter.version>
9898
<junit-jupiter-engine.version>5.8.0-M1</junit-jupiter-engine.version>
9999
<gson.version>2.10.1</gson.version>
100100
<maven-site-plugin.version>3.3</maven-site-plugin.version>
@@ -138,7 +138,7 @@
138138
<dependency>
139139
<groupId>org.jetbrains</groupId>
140140
<artifactId>annotations</artifactId>
141-
<version>24.0.1</version>
141+
<version>24.1.0</version>
142142
<scope>provided</scope>
143143
</dependency>
144144
<dependency>
@@ -168,7 +168,7 @@
168168
<dependency>
169169
<groupId>org.mockito</groupId>
170170
<artifactId>mockito-core</artifactId>
171-
<version>5.6.0</version>
171+
<version>5.9.0</version>
172172
<scope>test</scope>
173173
</dependency>
174174
<dependency>

src/main/java/com/contentstack/cms/core/Util.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Util {
1818
// The line `public static final String SDK_VERSION = "1.3.0";`
1919
// named `SDK_VERSION` of type `String`. The value of this constant is set to
2020
// "1.2.0".
21-
public static final String SDK_VERSION = "1.3.0";
21+
public static final String SDK_VERSION = "1.3.1";
2222

2323
static final String PRIVATE_CONSTRUCTOR = "private constructor can't be accessed outside the class";
2424
public static final Boolean RETRY_ON_FAILURE = true;

src/main/java/com/contentstack/cms/stack/Asset.java

+3-37
Original file line numberDiff line numberDiff line change
@@ -327,50 +327,16 @@ public Call<ResponseBody> uploadAsset(@NotNull String filePath, String descripti
327327
* @return Call
328328
*/
329329
public Call<ResponseBody> uploadAsset(@NotNull String filePath, String parentUid, String title, String description, String[] tags) {
330-
RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), description);
330+
RequestBody body = RequestBody.create(Objects.requireNonNull(MediaType.parse("multipart/form-data")), description);
331331
MultipartBody.Part partFile = createMultipartBody(filePath, parentUid, title, description, tags);
332332
return this.service.uploadAsset(this.headers, partFile, body, this.params);
333333
}
334334

335-
private String tagConvertor(String[] tags) {
336-
StringBuilder stringBuilder = new StringBuilder();
337-
for (int i = 0; i < tags.length; i++) {
338-
stringBuilder.append(tags[i]);
339-
if (i < tags.length - 1) {
340-
stringBuilder.append(", ");
341-
}
342-
}
343-
return stringBuilder.toString();
344-
}
345-
346335

347336
private MultipartBody.Part createMultipartBody(String filePath, String parentUid, String title, String description, String[] tags) {
348337
MultipartBody.Builder builder = new MultipartBody.Builder();
349338
builder.setType(MultipartBody.FORM);
350-
351-
if (!filePath.isEmpty()) {
352-
File file = new File(filePath);
353-
if (file.exists()) {
354-
RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
355-
builder.addFormDataPart("asset[upload]", file.getName(), fileBody);
356-
}
357-
}
358-
359-
// Add other parts
360-
if (parentUid != null) {
361-
builder.addFormDataPart("asset[parent_uid]", parentUid);
362-
}
363-
if (title != null) {
364-
builder.addFormDataPart("asset[title]", title);
365-
}
366-
if (description != null) {
367-
builder.addFormDataPart("asset[description]", description);
368-
}
369-
if (tags != null) {
370-
builder.addFormDataPart("asset[tags]", tagConvertor(tags));
371-
}
372-
373-
return builder.build().part(0);
339+
return new FileUploader().createMultipartBody(filePath, parentUid, title, description, tags);
374340
}
375341

376342

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.contentstack.cms.stack;
2+
3+
import okhttp3.MediaType;
4+
import okhttp3.MultipartBody;
5+
import okhttp3.RequestBody;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.nio.file.Files;
10+
import java.util.Objects;
11+
12+
public class FileUploader {
13+
14+
15+
public MultipartBody.Part createMultipartBody(String filePath, String parentUid, String title, String description, String[] tags) {
16+
MultipartBody.Builder builder = new MultipartBody.Builder();
17+
builder.setType(MultipartBody.FORM);
18+
19+
// Check if filePath is not empty and file exists
20+
if (!filePath.isEmpty()) {
21+
File file = new File(filePath);
22+
if (file.exists()) {
23+
String contentType = getContentType(file);
24+
RequestBody fileBody = RequestBody.create(Objects.requireNonNull(MediaType.parse(contentType)), file);
25+
builder.addFormDataPart("asset[upload]", file.getName(), fileBody);
26+
}
27+
}
28+
29+
// Add other parts if not null
30+
addFormDataPartIfNotNull(builder, "asset[parent_uid]", parentUid);
31+
addFormDataPartIfNotNull(builder, "asset[title]", title);
32+
addFormDataPartIfNotNull(builder, "asset[description]", description);
33+
34+
// Handle tags array null case
35+
if (tags != null) {
36+
addFormDataPartIfNotNull(builder, "asset[tags]", tagConvertor(tags));
37+
}
38+
39+
return builder.build().part(0);
40+
}
41+
42+
// Helper method to add form data part if value is not null
43+
private void addFormDataPartIfNotNull(MultipartBody.Builder builder, String name, String value) {
44+
if (value != null) {
45+
builder.addFormDataPart(name, value);
46+
}
47+
}
48+
49+
// Helper method to get content type of file
50+
private String getContentType(File file) {
51+
try {
52+
return Files.probeContentType(file.toPath());
53+
} catch (IOException e) {
54+
throw new RuntimeException("Failed to determine content type of file", e);
55+
}
56+
}
57+
58+
// Dummy implementation of tagConvertor method
59+
private String tagConvertor(String[] tags) {
60+
// Implement your tag conversion logic here
61+
return String.join(",", tags);
62+
}
63+
}

src/main/java/com/contentstack/cms/stack/Taxonomy.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public Terms terms() {
257257
* <p>Example usage:</p>
258258
* <pre>JSONObject object = new JSonObject();</pre>
259259
* <pre>object.put("taxonomies.color", Object)</pre>
260-
* <pre>Taxonomy taxonomy = stack("authtoken").taxonomy("taxonomyId").filterTaxonomy(object);</pre>
260+
* <pre>Taxonomy taxonomy = stack("authtoken").taxonomy("taxonomyId").query(object);</pre>
261261
*
262262
* @param query the query of type @{@link JSONObject}
263263
* @return instance of {@link Call}

src/test/java/com/contentstack/cms/stack/AssetAPITest.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static void setup() {
3030

3131
@Order(1)
3232
@Test
33-
void testFindAssets() throws IOException {
33+
void testFindAssets() {
3434
asset.clearParams();
3535
asset.addParam("include_folders", true);
3636
asset.addParam("environment", "production");
@@ -141,10 +141,9 @@ void testAssetUpload() {
141141
asset.addHeader("api_key", API_KEY);
142142
asset.addHeader("authorization", MANAGEMENT_TOKEN);
143143
asset.addHeader("authtoken", AUTHTOKEN);
144-
String filePath = "/Users/shaileshmishra/Desktop/pexels.png";
145-
String description = "The calender has been placed to assets by ***REMOVED***";
144+
String filePath = "/Users/shaileshmishra/Desktop/image.jpeg";
145+
String description = "The calender has been placed to assets by shaileshmishra";
146146
Request request = asset.uploadAsset(filePath, description).request();
147-
148147
// The assertions
149148
Assertions.assertEquals(3, request.headers().size());
150149
Assertions.assertTrue(request.headers().names().contains("api_key"));

src/test/java/com/contentstack/cms/stack/EntryFieldsAPITest.java

+22
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import com.contentstack.cms.TestClient;
44
import okhttp3.Request;
5+
import okhttp3.ResponseBody;
56
import org.json.simple.JSONObject;
67
import org.junit.jupiter.api.*;
8+
import retrofit2.Response;
79

810
import java.io.IOException;
911

@@ -363,4 +365,24 @@ void testPublishWithReference() {
363365
request.url().toString());
364366
}
365367

368+
369+
@Test
370+
public void testEntryQuery() {
371+
JSONObject query = new JSONObject();
372+
query.put("taxonomies.taxonomy_uid", "{ \"$in\" : [\"term_uid1\" , \"term_uid2\" ] }");
373+
Request request = entry.query(query).request();
374+
Assertions.assertEquals(2, request.headers().names().size());
375+
Assertions.assertEquals("GET", request.method());
376+
Assertions.assertTrue(request.url().isHttps());
377+
Assertions.assertEquals("api.contentstack.io", request.url().host());
378+
Assertions.assertEquals(4, request.url().pathSegments().size());
379+
Assertions.assertEquals("v3", request.url().pathSegments().get(0));
380+
Assertions.assertEquals("content_types", request.url().pathSegments().get(1));
381+
Assertions.assertEquals("product", request.url().pathSegments().get(2));
382+
Assertions.assertEquals("entries", request.url().pathSegments().get(3));
383+
Assertions.assertNull(request.body());
384+
Assertions.assertEquals("query={\"taxonomies.taxonomy_uid\":\"{ \\\"$in\\\" : [\\\"term_uid1\\\" , \\\"term_uid2\\\" ] }\"}", request.url().query());
385+
386+
}
387+
366388
}

0 commit comments

Comments
 (0)