Skip to content

Commit 114c11b

Browse files
authored
Merge pull request #165 from contentstack/fix/DX-3270-fileUpload-issue
File Upload fix
2 parents 0cf7b8d + a850dcf commit 114c11b

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v1.7.1
4+
5+
### Jul 21, 2025
6+
7+
- FileUpload method fix
8+
39
## v1.7.0
410

511
### Jul 07, 2025

pom.xml

Lines changed: 1 addition & 1 deletion
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.7.0</version>
10+
<version>1.7.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>

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,34 @@
77
import java.io.File;
88
import java.io.IOException;
99
import java.nio.file.Paths;
10+
import java.util.HashMap;
11+
import java.util.Map;
1012
import java.util.Objects;
1113

1214
import javax.activation.MimetypesFileTypeMap;
1315

1416
public class FileUploader {
1517

18+
// Static map for common file extensions to MIME types
19+
private static final Map<String, String> EXTENSION_TO_MIME;
20+
static {
21+
EXTENSION_TO_MIME = new HashMap<>();
22+
EXTENSION_TO_MIME.put(".svg", "image/svg+xml");
23+
EXTENSION_TO_MIME.put(".webp", "image/webp");
24+
EXTENSION_TO_MIME.put(".json", "application/json");
25+
EXTENSION_TO_MIME.put(".woff", "font/woff");
26+
EXTENSION_TO_MIME.put(".woff2", "font/woff2");
27+
EXTENSION_TO_MIME.put(".ttf", "font/ttf");
28+
EXTENSION_TO_MIME.put(".otf", "font/otf");
29+
EXTENSION_TO_MIME.put(".eot", "application/vnd.ms-fontobject");
30+
EXTENSION_TO_MIME.put(".mp4", "video/mp4");
31+
EXTENSION_TO_MIME.put(".m4a", "audio/mp4");
32+
EXTENSION_TO_MIME.put(".mkv", "video/x-matroska");
33+
EXTENSION_TO_MIME.put(".webm", "video/webm");
34+
EXTENSION_TO_MIME.put(".ico", "image/x-icon");
35+
EXTENSION_TO_MIME.put(".csv", "text/csv");
36+
EXTENSION_TO_MIME.put(".md", "text/markdown");
37+
}
1638

1739
public MultipartBody createMultipartBody(String filePath, String parentUid, String title, String description, String[] tags) {
1840
MultipartBody.Builder builder = new MultipartBody.Builder();
@@ -47,9 +69,16 @@ public MultipartBody createMultipartBody(String filePath, String parentUid, Stri
4769

4870
// Helper method to get content type of file
4971
private String getContentType(File file) {
72+
String name = file.getName().toLowerCase();
73+
int dot = name.lastIndexOf('.');
74+
if (dot != -1) {
75+
String ext = name.substring(dot);
76+
String mime = EXTENSION_TO_MIME.get(ext);
77+
if (mime != null) return mime;
78+
}
5079
try {
51-
java.nio.file.Path source = Paths.get(file.toString());
52-
MimetypesFileTypeMap m = new MimetypesFileTypeMap(source.toString());
80+
java.nio.file.Path source = java.nio.file.Paths.get(file.toString());
81+
javax.activation.MimetypesFileTypeMap m = new javax.activation.MimetypesFileTypeMap(source.toString());
5382
return m.getContentType(file);
5483
} catch (IOException e) {
5584
throw new RuntimeException("Failed to determine content type of file", e);

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
import java.io.IOException;
1212
import java.util.HashMap;
1313
import java.util.Objects;
14+
import com.contentstack.cms.stack.FileUploader;
15+
import org.junit.jupiter.api.Test;
16+
import java.io.File;
17+
import java.util.Map;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
1420

1521
@Tag("API")
1622
class AssetAPITest {
@@ -358,4 +364,19 @@ void testFetchSubfoldersByParentFolderPojo() {
358364
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());
359365
}
360366

367+
@Test
368+
@Disabled("disabled to avoid unnecessary asset creation, Tested working fine")
369+
void uploadFile() throws Exception {
370+
Contentstack contentstack = new Contentstack.Builder().build();
371+
Stack stack = contentstack.stack(API_KEY, MANAGEMENT_TOKEN);
372+
Asset asset = stack.asset();
373+
String fileName = "/Users/reeshika.hosmani/Downloads/surf-svgrepo-com.svg", parentFolder = "bltd1150f1f7d9411e5", title = "Vacation icon";
374+
String[] tags = {"icon"};
375+
Response<ResponseBody> response = asset.uploadAsset(fileName,parentFolder,title,"",tags).execute();
376+
if(response.isSuccessful()){
377+
System.out.println("uploaded asset successfully:" + response.body().string());
378+
} else {
379+
System.out.println("Error in uploading" + response.errorBody().string());
380+
}
381+
}
361382
}

0 commit comments

Comments
 (0)