Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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