From a2c1bed446de910620b107958ff67228ca3878f8 Mon Sep 17 00:00:00 2001 From: Sachin Karve Date: Wed, 17 Jul 2024 16:01:58 -0700 Subject: [PATCH 1/3] adding getMimeToExt as a separate function for extensive list --- .../java/com/meta/cp4m/message/Payload.java | 58 +++++++++++++------ 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/meta/cp4m/message/Payload.java b/src/main/java/com/meta/cp4m/message/Payload.java index 35a5843..6b8d271 100644 --- a/src/main/java/com/meta/cp4m/message/Payload.java +++ b/src/main/java/com/meta/cp4m/message/Payload.java @@ -68,9 +68,11 @@ final class Image implements Payload { 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; } @@ -113,26 +115,27 @@ public int hashCode() { final class Document implements Payload { // https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#document - private static final Map MIME_TO_EXTENSION = - ImmutableMap.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 MIME_TO_EXTENSION = +// ImmutableMap.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; } @@ -172,4 +175,25 @@ public int hashCode() { return result; } } + + static String getMimeToExt(String mimeType) { + System.out.println("getMimeToExt function"); + final Map MIME_TO_EXTENSION = + ImmutableMap.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; + } } From 172637eef94952e60144289e5f1709d22cb52012 Mon Sep 17 00:00:00 2001 From: Sachin Karve Date: Wed, 17 Jul 2024 20:04:43 -0700 Subject: [PATCH 2/3] cleanup and structure --- .../java/com/meta/cp4m/message/Payload.java | 97 +++++++++---------- 1 file changed, 47 insertions(+), 50 deletions(-) diff --git a/src/main/java/com/meta/cp4m/message/Payload.java b/src/main/java/com/meta/cp4m/message/Payload.java index 6b8d271..03627a1 100644 --- a/src/main/java/com/meta/cp4m/message/Payload.java +++ b/src/main/java/com/meta/cp4m/message/Payload.java @@ -53,26 +53,13 @@ public int hashCode() { } final class Image implements Payload { - // mime types and corresponding extensions derived from - // https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#image - - private static final Map MIME_TO_EXTENSION = - ImmutableMap.builder() - .put("image/jpeg", "jpeg") - .put("image/png", "png") - .put("image/webp", "webp") - .build(); - private final String mimeType; private final String extension; private final byte[] payload; public Image(byte[] payload, String 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.extension = MimeTypeUtils.getMimeToExt(mimeType.strip()); this.mimeType = mimeType; this.payload = payload; } @@ -114,28 +101,12 @@ public int hashCode() { } final class Document implements Payload { - // https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#document -// private static final Map MIME_TO_EXTENSION = -// ImmutableMap.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 - this.extension = getMimeToExt(mimeType); + this.extension = MimeTypeUtils.getMimeToExt(mimeType.strip()); this.mimeType = mimeType; this.payload = payload; } @@ -176,24 +147,50 @@ public int hashCode() { } } - static String getMimeToExt(String mimeType) { - System.out.println("getMimeToExt function"); - final Map MIME_TO_EXTENSION = - ImmutableMap.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; + final class MimeTypeUtils { + // MIME types and corresponding extensions + private static final Map MIME_TO_EXTENSION = + ImmutableMap.builder() + .put("application/gzip", "gz") + .put("application/json", "json") + .put("application/msword", "doc") + .put("application/pdf", "pdf") + .put("application/rtf", "rtf") + .put("application/vnd.amazon.ebook", "azw") + .put("application/vnd.ms-excel", "xls") + .put("application/vnd.ms-powerpoint", "ppt") + .put("application/vnd.oasis.opendocument.presentation", "odp") + .put("application/vnd.oasis.opendocument.spreadsheet", "ods") + .put("application/vnd.oasis.opendocument.text", "odt") + .put("application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx") + .put("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx") + .put("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx") + .put("application/vnd.rar", "rar") + .put("application/x-abiword", "abw") + .put("application/x-bzip", "bz") + .put("application/x-bzip2", "bz2") + .put("application/x-freearc", "arc") + .put("application/x-tar", "tar") + .put("application/xml", "xml") + .put("application/zip", "zip") + .put("image/apng", "apng") + .put("image/avif", "avif") + .put("image/bmp", "bmp") + .put("image/gif", "gif") + .put("image/jpeg", "jpeg") + .put("image/png", "png") + .put("image/svg+xml", "svg") + .put("image/tiff", "tif") + .put("image/webp", "webp") + .put("text/calendar", "ics") + .put("text/csv", "csv") + .put("text/html", "html") + .put("text/plain", "txt") + .build(); + + // Static method to get the file extension based on MIME type + public static String getMimeToExt(String mimeType) { + return Objects.requireNonNullElse(MIME_TO_EXTENSION.get(mimeType), "bin"); + } } } From 91583c62fb99977234ad5fe01d21ecbab88911ee Mon Sep 17 00:00:00 2001 From: Sachin Karve Date: Wed, 17 Jul 2024 20:06:14 -0700 Subject: [PATCH 3/3] clean up --- src/main/java/com/meta/cp4m/message/Payload.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/meta/cp4m/message/Payload.java b/src/main/java/com/meta/cp4m/message/Payload.java index 03627a1..bfeff66 100644 --- a/src/main/java/com/meta/cp4m/message/Payload.java +++ b/src/main/java/com/meta/cp4m/message/Payload.java @@ -58,7 +58,6 @@ final class Image implements Payload { private final byte[] payload; public Image(byte[] payload, String mimeType) { - System.out.println("Image constructor"); this.extension = MimeTypeUtils.getMimeToExt(mimeType.strip()); this.mimeType = mimeType; this.payload = payload;