Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clot.email attchment #217

Merged
merged 4 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.codeforcommunity.dto.emailer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the same comments as on reject email

  • avoid using comments and instead focus on good names (which it looks like you have, so you can delete these)
  • baseEncoding validation based on length


import com.codeforcommunity.aws.EncodedImage;
import com.codeforcommunity.dto.ApiDto;
import com.codeforcommunity.exceptions.BadRequestImageException;
import com.codeforcommunity.exceptions.HandledException;
import com.codeforcommunity.exceptions.MalformedParameterException;

import org.simplejavamail.api.email.AttachmentResource;

import javax.mail.util.ByteArrayDataSource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
Expand All @@ -26,12 +24,12 @@ public EmailAttachment(String name, String data) {

private EmailAttachment() {}

private static String getFileExtension(String base64Image) {
if (base64Image == null || base64Image.length() < 10) {
private static String getFileMimeType(String base64File) {
if (base64File == null || base64File.length() < 10) {
return null;
}

String[] base64ImageSplit = base64Image.split(",", 2);
String[] base64ImageSplit = base64File.split(",", 2);
if (base64ImageSplit.length != 2) {
return null;
}
Expand All @@ -44,24 +42,24 @@ private static String getFileExtension(String base64Image) {
}

String[] dataSplit = metaSplit[0].split(":", 2);
if (dataSplit.length != 2) {
return null;
}

String[] data = dataSplit[1].split("/", 2);
if (data.length != 2 || !data[0].equals("image")) {
if (dataSplit.length != 2) {
return null;
}

String fileExtension = data[1];
String fileExtension = dataSplit[1];
return fileExtension;
}
private static ByteArrayDataSource stringToDataSource(String data) {
byte[] imageData;
String mimeType;

try {
mimeType = getFileExtension(data);
mimeType = getFileMimeType(data);
if (mimeType==null) {
throw new MalformedParameterException("data");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know we had this exception, good find!

}

String[] base64Split = data.split(",", 2);
imageData = Base64.getDecoder().decode(base64Split[1]);

Expand Down
12 changes: 12 additions & 0 deletions node_modules/.yarn-integrity

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


Loading