public Attachment toAttachment(byte[] data) { return Attachment.builder() .attachmentId(attachmentId) .type(type) .bytes(data) .build(); }
private UploadResponse uploadContent(String contentType, InputStream inputStream, MailboxSession session) throws IOException, MailboxException { Attachment attachment = Attachment.builder() .bytes(ByteStreams.toByteArray(inputStream)) .type(contentType) .build(); attachmentManager.storeAttachment(attachment, session); return UploadResponse.builder() .blobId(attachment.getAttachmentId().getId()) .type(attachment.getType()) .size(attachment.getSize()) .build(); }
@Test public void streamShouldBeConsumedMoreThanOneTime() throws Exception { String input = "mystream"; Attachment attachment = Attachment.builder() .bytes(input.getBytes(CHARSET)) .type("content") .build(); attachment.getStream(); InputStream stream = attachment.getStream(); assertThat(stream).isNotNull(); assertThat(IOUtils.toString(stream, CHARSET)).isEqualTo(input); }
public static DAOAttachment from(Attachment attachment, BlobId blobId) { return new DAOAttachment( attachment.getAttachmentId(), blobId, attachment.getType(), attachment.getSize()); }
@Override public void storeAttachmentForOwner(Attachment attachment, Username owner) throws MailboxException { ownerDAO.addOwner(attachment.getAttachmentId(), owner) .thenCompose(any -> blobStore.save(attachment.getBytes())) .thenApply(blobId -> CassandraAttachmentDAOV2.from(attachment, blobId)) .thenCompose(attachmentDAOV2::storeAttachment) .join(); }
private Stream<String> toAttachmentContent(Attachment attachment) { try { return OptionalUtils.toStream( textExtractor .extractContent( attachment.getStream(), attachment.getType()) .getTextualContent()); } catch (Exception e) { LOGGER.error("Error while parsing attachment content", e); return Stream.of(); } }
@Test public void setMessagesShouldReturnAttachmentsWhenMessageHasInlinedAttachmentButNoCid() throws Exception { Attachment attachment = Attachment.builder() .bytes("attachment".getBytes(StandardCharsets.UTF_8)) .type("application/octet-stream") .build(); String uploadedAttachment1 = uploadAttachment(attachment); Attachment attachment2 = Attachment.builder() .bytes("attachment2".getBytes(StandardCharsets.UTF_8)) .type("application/octet-stream") " \"attachments\": [" + " {\"blobId\" : \"" + uploadedAttachment1 + "\", " + " \"type\" : \"" + attachment.getType() + "\", " + " \"size\" : " + attachment.getSize() + "}," + " {\"blobId\" : \"" + uploadedAttachment2 + "\", " + " \"type\" : \"" + attachment2.getType() + "\", " + " \"size\" : " + attachment2.getSize() + ", " + " \"isInline\" : true }" + " ]" + .body(firstAttachment + ".blobId", equalTo(uploadedAttachment1)) .body(firstAttachment + ".type", equalTo("application/octet-stream; charset=UTF-8")) .body(firstAttachment + ".size", equalTo((int) attachment.getSize())) .body(firstAttachment + ".cid", nullValue()) .body(firstAttachment + ".isInline", equalTo(false)) .body(secondAttachment + ".blobId", equalTo(uploadedAttachment2)) .body(secondAttachment + ".type", equalTo("application/octet-stream; charset=UTF-8")) .body(secondAttachment + ".size", equalTo((int) attachment2.getSize())) .body(secondAttachment + ".cid", nullValue())
@Test public void getByteShouldReturnByteArrayRepresentingTheAttachment() { String input = "mystream"; Attachment attachment = Attachment.builder() .bytes(input.getBytes(CHARSET)) .type("content") .build(); byte[] bytes = attachment.getBytes(); assertThat(new String(bytes, CHARSET)).isEqualTo(input); }
public CompletableFuture<Void> storeAttachment(Attachment attachment) throws IOException { return cassandraAsyncExecutor.executeVoid( insertStatement.bind() .setString(ID, attachment.getAttachmentId().getId()) .setLong(SIZE, attachment.getSize()) .setString(TYPE, attachment.getType()) .setBytes(PAYLOAD, ByteBuffer.wrap(attachment.getBytes()))); }
@Test void deleteAttachmentShouldRemoveAttachment() throws Exception { Attachment attachment = Attachment.builder() .attachmentId(ATTACHMENT_ID) .type("application/json") .bytes("{\"property\":`\"value\"}".getBytes(StandardCharsets.UTF_8)) .build(); testee.storeAttachment(attachment).join(); testee.deleteAttachment(attachment.getAttachmentId()).join(); assertThat(testee.getAttachment(attachment.getAttachmentId()).join()) .isEmpty(); } }
@Test public void toBlobShouldGenerateTheAttachmentBlob() { byte[] bytes = "mystream".getBytes(CHARSET); String content = "content"; Attachment attachment = Attachment.builder() .bytes(bytes) .type(content) .build(); Blob expected = Blob.builder() .id(BlobId.fromBytes(bytes)) .contentType(content) .payload(bytes) .build(); assertThat(attachment.toBlob()).isEqualTo(expected); } }
public AttachmentId getAttachmentId() { return attachment.getAttachmentId(); }
private Attachment fromMailboxAttachment(MessageAttachment attachment) { return Attachment.builder() .blobId(BlobId.of(attachment.getAttachmentId().getId())) .type(attachment.getAttachment().getType()) .size(attachment.getAttachment().getSize()) .name(attachment.getName()) .cid(attachment.getCid().map(Cid::getValue)) .isInline(attachment.isInline()) .build(); }
public CompletableFuture<Void> storeAttachmentAsync(Attachment attachment, MessageId ownerMessageId) { return blobStore.save(attachment.getBytes()) .thenApply(blobId -> CassandraAttachmentDAOV2.from(attachment, blobId)) .thenCompose(daoAttachment -> storeAttachmentWithIndex(daoAttachment, ownerMessageId)); }
private ContentTypeField contentTypeField(MessageAttachment att) { Builder<String, String> parameters = ImmutableMap.builder(); if (att.getName().isPresent()) { parameters.put("name", encode(att.getName().get())); } String type = att.getAttachment().getType(); if (type.contains(FIELD_PARAMETERS_SEPARATOR)) { return Fields.contentType(contentTypeWithoutParameters(type), parameters.build()); } return Fields.contentType(type, parameters.build()); }
private BodyPart attachmentBodyPart(MessageAttachment att) throws IOException { BodyPartBuilder builder = BodyPartBuilder.create() .use(bodyFactory) .setBody(new BasicBodyFactory().binaryBody(ByteStreams.toByteArray(att.getAttachment().getStream()))) .setField(contentTypeField(att)) .setField(contentDispositionField(att.isInline())) .setContentTransferEncoding(BASE64); contentId(builder, att); return builder.build(); }
private Optional<Blob> getBlobFromAttachment(BlobId blobId, MailboxSession mailboxSession) throws MailboxException { try { AttachmentId attachmentId = AttachmentId.from(blobId); return Optional.of(attachmentManager.getAttachment(attachmentId, mailboxSession).toBlob()); } catch (AttachmentNotFoundException e) { return Optional.empty(); } }
@Test public void setMessagesShouldReturnAttachmentsWhenMessageHasAttachment() throws Exception { Attachment attachment = Attachment.builder() .bytes("attachment".getBytes(StandardCharsets.UTF_8)) .type("application/octet-stream") .build(); String uploadedAttachment1 = uploadAttachment(attachment); Attachment attachment2 = Attachment.builder() .bytes("attachment2".getBytes(StandardCharsets.UTF_8)) .type("application/octet-stream") " \"attachments\": [" + " {\"blobId\" : \"" + uploadedAttachment1 + "\", " + " \"type\" : \"" + attachment.getType() + "\", " + " \"size\" : " + attachment.getSize() + "}," + " {\"blobId\" : \"" + uploadedAttachment2 + "\", " + " \"type\" : \"" + attachment2.getType() + "\", " + " \"size\" : " + attachment2.getSize() + ", " + " \"cid\" : \"123456789\", " + " \"isInline\" : true }" + .body(firstAttachment + ".blobId", equalTo(uploadedAttachment1)) .body(firstAttachment + ".type", equalTo("application/octet-stream; charset=UTF-8")) .body(firstAttachment + ".size", equalTo((int) attachment.getSize())) .body(firstAttachment + ".cid", nullValue()) .body(firstAttachment + ".isInline", equalTo(false)) .body(secondAttachment + ".blobId", equalTo(uploadedAttachment2)) .body(secondAttachment + ".type", equalTo("application/octet-stream; charset=UTF-8")) .body(secondAttachment + ".size", equalTo((int) attachment2.getSize())) .body(secondAttachment + ".cid", equalTo("123456789"))
@Test void getAttachmentShouldReturnV2WhenPresentInV1AndV2() throws Exception { Attachment attachment = Attachment.builder() .attachmentId(ATTACHMENT_ID_1) .type("application/json") .bytes("{\"property\":`\"value\"}".getBytes(StandardCharsets.UTF_8)) .build(); Attachment otherAttachment = Attachment.builder() .attachmentId(ATTACHMENT_ID_1) .type("application/json") .bytes("{\"property\":`\"different\"}".getBytes(StandardCharsets.UTF_8)) .build(); BlobId blobId = blobsDAO.save(attachment.getBytes()).join(); attachmentDAOV2.storeAttachment(CassandraAttachmentDAOV2.from(attachment, blobId)).join(); attachmentDAO.storeAttachment(otherAttachment).join(); assertThat(attachmentMapper.getAttachment(ATTACHMENT_ID_1)) .isEqualTo(attachment); }