in commons-email2-jakarta/src/main/java/org/apache/commons/mail2/jakarta/HtmlEmail.java [382:425]
public String embed(final File file, final String cid) throws EmailException {
EmailException.checkNonEmpty(file.getName(), () -> "File name cannot be null or empty");
// verify that the File can provide a canonical path
String filePath = null;
try {
filePath = file.getCanonicalPath();
} catch (final IOException e) {
throw new EmailException("couldn't get canonical path for " + file.getName(), e);
}
// check if a FileDataSource for this name has already been attached;
// if so, return the cached CID value.
final InlineImage inlineImage = inlineEmbeds.get(file.getName());
if (inlineImage != null) {
final FileDataSource fileDataSource = (FileDataSource) inlineImage.getDataSource();
// make sure the supplied file has the same canonical path
// as the one already associated with this name.
String existingFilePath = null;
try {
existingFilePath = fileDataSource.getFile().getCanonicalPath();
} catch (final IOException e) {
throw new EmailException("couldn't get canonical path for file " + fileDataSource.getFile().getName() + "which has already been embedded", e);
}
if (filePath.equals(existingFilePath)) {
return inlineImage.getCid();
}
throw new EmailException(
"embedded name '" + file.getName() + "' is already bound to file " + existingFilePath + "; existing names cannot be rebound");
}
// verify that the file is valid
if (!file.exists()) {
throw new EmailException("file " + filePath + " doesn't exist");
}
if (!file.isFile()) {
throw new EmailException("file " + filePath + " isn't a normal file");
}
if (!file.canRead()) {
throw new EmailException("file " + filePath + " isn't readable");
}
return embed(new FileDataSource(file), file.getName(), cid);
}