diff --git a/zulia-client/src/main/java/io/zulia/client/rest/ZuliaRESTClient.java b/zulia-client/src/main/java/io/zulia/client/rest/ZuliaRESTClient.java index f5f92cfe..9b34d953 100644 --- a/zulia-client/src/main/java/io/zulia/client/rest/ZuliaRESTClient.java +++ b/zulia-client/src/main/java/io/zulia/client/rest/ZuliaRESTClient.java @@ -18,7 +18,6 @@ import java.io.File; import java.io.OutputStream; -import java.net.URLConnection; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Objects; @@ -48,14 +47,12 @@ public void storeAssociated(String uniqueId, String indexName, String fileName, if (metadata != null) { body = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("id", uniqueId).addFormDataPart("fileName", fileName) .addFormDataPart("indexName", indexName).addFormDataPart("metaJson", metadata.toJson()) - .addFormDataPart("file", fileName, RequestBody.create(bytes, MediaType.parse(URLConnection.guessContentTypeFromName(fileName)))) - .build(); + .addFormDataPart("file", fileName, RequestBody.create(bytes, MediaType.parse(ZuliaUtil.guessExtension(bytes)))).build(); } else { body = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("id", uniqueId).addFormDataPart("fileName", fileName) .addFormDataPart("indexName", indexName) - .addFormDataPart("file", fileName, RequestBody.create(bytes, MediaType.parse(URLConnection.guessContentTypeFromName(fileName)))) - .build(); + .addFormDataPart("file", fileName, RequestBody.create(bytes, MediaType.parse(ZuliaUtil.guessExtension(bytes)))).build(); } Request request = new Request.Builder().url(url + ZuliaConstants.ASSOCIATED_DOCUMENTS_URL).method("POST", body).build(); diff --git a/zulia-common/build.gradle.kts b/zulia-common/build.gradle.kts index 45372d3e..4a4fbe4f 100644 --- a/zulia-common/build.gradle.kts +++ b/zulia-common/build.gradle.kts @@ -25,6 +25,7 @@ dependencies { api("org.apache.commons:commons-pool2:2.11.1") api("javax.annotation:javax.annotation-api:1.3.2") api("com.google.guava:guava:31.1-jre") + api("com.j256.simplemagic:simplemagic:1.17") } diff --git a/zulia-common/src/main/java/io/zulia/util/ZuliaUtil.java b/zulia-common/src/main/java/io/zulia/util/ZuliaUtil.java index 178fe884..1e37bfe3 100644 --- a/zulia-common/src/main/java/io/zulia/util/ZuliaUtil.java +++ b/zulia-common/src/main/java/io/zulia/util/ZuliaUtil.java @@ -1,6 +1,8 @@ package io.zulia.util; import com.google.protobuf.ByteString; +import com.j256.simplemagic.ContentInfo; +import com.j256.simplemagic.ContentInfoUtil; import org.bson.BsonBinaryReader; import org.bson.BsonBinaryWriter; import org.bson.Document; @@ -168,4 +170,10 @@ public static CodecRegistry getPojoCodecRegistry() { return pojoCodecRegistry; } + public static String guessExtension(byte[] bytes) { + ContentInfoUtil util = new ContentInfoUtil(); + ContentInfo info = util.findMatch(bytes); + return info.getMimeType(); + } + }