public static Attachment writeAttachment(byte[] data, String name, String passedType, Path outputDirectory) { String fileName = generateAttachmentFileName(); boolean isTypeProvided = passedType == null || passedType.isEmpty(); String type = isTypeProvided ? getAttachmentType(data) : passedType; String extension = getExtensionByMimeType(type); String source = fileName + extension; write(data, source, outputDirectory); return new Attachment().withName(name).withType(type).withSource(source); }