Skip to content
6 changes: 3 additions & 3 deletions .github/workflows/maven-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ 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 }}
gpg-passphrase: GPG_PASSPHRASE
- 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
Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v1.7.1

### Jul 21, 2025

- FileUpload method fix

## v1.7.0

### Jul 07, 2025
Expand Down
29 changes: 14 additions & 15 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.7.0</version>
<version>1.7.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 @@ -61,23 +61,23 @@
</developer>
</developers>

<distributionManagement>
<!-- <distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<name>Apache Maven Packages Snapshot</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<!-- <snapshotRepository>-->
<!-- <id>github</id>-->
<!-- <name>GitHub Apache Maven Packages</name>-->
<!-- <url>https://maven.pkg.github.com/contentstack/contentstack-management-java</url>-->
<!-- </snapshotRepository> -->
<snapshotRepository>
<id>github</id>
<name>GitHub Apache Maven Packages</name>
<url>https://maven.pkg.github.com/contentstack/contentstack-management-java</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<name>Apache Maven Packages Release</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</distributionManagement> -->

<properties>
<sdk.version>1.0.0</sdk.version>
Expand All @@ -100,7 +100,6 @@
<maven-site-plugin.version>3.3</maven-site-plugin.version>
<maven-gpg-plugin.version>1.5</maven-gpg-plugin.version>
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
<nexus-staging-maven-plugin.version>1.6.13</nexus-staging-maven-plugin.version>
<maven-release-plugin.version>2.5.3</maven-release-plugin.version>
</properties>

Expand Down Expand Up @@ -338,14 +337,14 @@
</configuration>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>${nexus-staging-maven-plugin.version}</version>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
<version>0.8.0</version> <!-- or latest -->
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
<publishingServerId>central</publishingServerId>
<autoPublish>true</autoPublish>
<waitUntil>published</waitUntil>
</configuration>
</plugin>
<plugin>
Expand Down
33 changes: 31 additions & 2 deletions src/main/java/com/contentstack/cms/stack/FileUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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();
Expand Down Expand Up @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/com/contentstack/cms/stack/AssetAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<ResponseBody> 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());
}
}
}
Loading