Skip to content

Commit

Permalink
Added better Mime type handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
payammeyer committed May 8, 2022
1 parent a098f97 commit bafdee7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions zulia-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}


Expand Down
8 changes: 8 additions & 0 deletions zulia-common/src/main/java/io/zulia/util/ZuliaUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}

}

0 comments on commit bafdee7

Please sign in to comment.