Skip to content

Commit

Permalink
adding getMimeToExt as a separate function for extensive list
Browse files Browse the repository at this point in the history
  • Loading branch information
Sachin Karve committed Jul 17, 2024
1 parent b654739 commit a2c1bed
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions src/main/java/com/meta/cp4m/message/Payload.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ final class Image implements Payload<byte[]> {
private final byte[] payload;

public Image(byte[] payload, String mimeType) {
this.extension =
Objects.requireNonNull(
MIME_TO_EXTENSION.get(mimeType.strip()), "Unknown mime type " + mimeType);
// this.extension =
// Objects.requireNonNull(
// MIME_TO_EXTENSION.get(mimeType.strip()), "Unknown mime type " + mimeType);
System.out.println("Image constructor");
this.extension = getMimeToExt(mimeType);
this.mimeType = mimeType;
this.payload = payload;
}
Expand Down Expand Up @@ -113,26 +115,27 @@ public int hashCode() {

final class Document implements Payload<byte[]> {
// https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#document
private static final Map<String, String> MIME_TO_EXTENSION =
ImmutableMap.<String, String>builder()
.put("text/plain", "txt")
.put("application/vnd.ms-excel", "xls")
.put("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx")
.put("application/msword", "doc")
.put("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx")
.put("application/vnd.ms-powerpoint", "ppt")
.put(
"application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx")
.put("application/pdf", "pdf")
.build();
// private static final Map<String, String> MIME_TO_EXTENSION =
// ImmutableMap.<String, String>builder()
// .put("text/plain", "txt")
// .put("application/vnd.ms-excel", "xls")
// .put("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx")
// .put("application/msword", "doc")
// .put("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx")
// .put("application/vnd.ms-powerpoint", "ppt")
// .put(
// "application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx")
// .put("application/pdf", "pdf")
// .build();

private final String mimeType;
private final String extension;
private final byte[] payload;

public Document(byte[] payload, String mimeType) {
@Nullable String extension = MIME_TO_EXTENSION.get(mimeType.strip());
this.extension = extension == null ? "bin" : extension; // default to binary if it's unknown
// @Nullable String extension = MIME_TO_EXTENSION.get(mimeType.strip());
// this.extension = extension == null ? "bin" : extension; // default to binary if it's unknown
this.extension = getMimeToExt(mimeType);
this.mimeType = mimeType;
this.payload = payload;
}
Expand Down Expand Up @@ -172,4 +175,25 @@ public int hashCode() {
return result;
}
}

static String getMimeToExt(String mimeType) {
System.out.println("getMimeToExt function");
final Map<String, String> MIME_TO_EXTENSION =
ImmutableMap.<String, String>builder()
.put("text/plain", "txt")
.put("application/vnd.ms-excel", "xls")
.put("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx")
.put("application/msword", "doc")
.put("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx")
.put("application/vnd.ms-powerpoint", "ppt")
.put("application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx")
.put("application/pdf", "pdf")
.put("image/jpeg", "jpeg")
.put("image/png", "png")
.put("image/webp", "webp")
.build();
String ext = Objects.requireNonNullElse(MIME_TO_EXTENSION.get(mimeType.strip()), "bin");
System.out.println("Extension is:: " + ext);
return ext;
}
}

0 comments on commit a2c1bed

Please sign in to comment.