use of com.fsck.k9.mailstore.migrations.MigrationTo51.MimeStructureState in project k-9 by k9mail.
the class MigrationTo51 method insertBodyAsMultipartAlternative.
private static MimeStructureState insertBodyAsMultipartAlternative(SQLiteDatabase db, MimeStructureState structureState, MimeHeader mimeHeader, String textContent, String htmlContent) throws IOException {
if (mimeHeader == null) {
mimeHeader = new MimeHeader();
}
String boundary = MimeUtility.getHeaderParameter(mimeHeader.getFirstHeader(MimeHeader.HEADER_CONTENT_TYPE), "boundary");
if (TextUtils.isEmpty(boundary)) {
boundary = MimeUtil.createUniqueBoundary();
}
mimeHeader.setHeader(MimeHeader.HEADER_CONTENT_TYPE, String.format("multipart/alternative; boundary=\"%s\";", boundary));
int dataLocation = textContent != null || htmlContent != null ? DATA_LOCATION__IN_DATABASE : DATA_LOCATION__MISSING;
ContentValues cv = new ContentValues();
cv.put("type", MESSAGE_PART_TYPE__UNKNOWN);
cv.put("data_location", dataLocation);
cv.put("mime_type", "multipart/alternative");
cv.put("header", mimeHeader.toString());
cv.put("boundary", boundary);
structureState.applyValues(cv);
long multipartAlternativePartId = db.insertOrThrow("message_parts", null, cv);
structureState = structureState.nextMultipartChild(multipartAlternativePartId);
if (textContent != null) {
structureState = insertTextualPartIntoDatabase(db, structureState, null, textContent, false);
}
if (htmlContent != null) {
structureState = insertTextualPartIntoDatabase(db, structureState, null, htmlContent, true);
}
return structureState;
}
use of com.fsck.k9.mailstore.migrations.MigrationTo51.MimeStructureState in project k-9 by k9mail.
the class MigrationTo51 method insertMimeAttachmentPart.
private static MimeStructureState insertMimeAttachmentPart(SQLiteDatabase db, File attachmentDirOld, File attachmentDirNew, MimeStructureState structureState, long id, int size, String name, String mimeType, String storeData, String contentUriString, String contentId, String contentDisposition) {
Timber.d("processing attachment %d, %s, %s, %s, %s", id, name, mimeType, storeData, contentUriString);
if (contentDisposition == null) {
contentDisposition = "attachment";
}
MimeHeader mimeHeader = new MimeHeader();
mimeHeader.setHeader(MimeHeader.HEADER_CONTENT_TYPE, String.format("%s;\r\n name=\"%s\"", mimeType, name));
mimeHeader.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, String.format(Locale.US, "%s;\r\n filename=\"%s\";\r\n size=%d", contentDisposition, name, // TODO: Should use encoded word defined in RFC 2231.
size));
if (contentId != null) {
mimeHeader.setHeader(MimeHeader.HEADER_CONTENT_ID, contentId);
}
boolean hasData = contentUriString != null;
File attachmentFileToMove;
if (hasData) {
try {
Uri contentUri = Uri.parse(contentUriString);
List<String> pathSegments = contentUri.getPathSegments();
String attachmentId = pathSegments.get(1);
boolean isMatchingAttachmentId = Long.parseLong(attachmentId) == id;
File attachmentFile = new File(attachmentDirOld, attachmentId);
boolean isExistingAttachmentFile = attachmentFile.exists();
if (!isMatchingAttachmentId) {
Timber.e("mismatched attachment id. mark as missing");
attachmentFileToMove = null;
} else if (!isExistingAttachmentFile) {
Timber.e("attached file doesn't exist. mark as missing");
attachmentFileToMove = null;
} else {
attachmentFileToMove = attachmentFile;
}
} catch (Exception e) {
// anything here fails, conservatively assume the data doesn't exist
attachmentFileToMove = null;
}
} else {
attachmentFileToMove = null;
}
if (attachmentFileToMove == null) {
Timber.d("matching attachment is in local cache");
}
boolean hasContentTypeAndIsInline = !TextUtils.isEmpty(contentId) && "inline".equalsIgnoreCase(contentDisposition);
int messageType = hasContentTypeAndIsInline ? MESSAGE_PART_TYPE__HIDDEN_ATTACHMENT : MESSAGE_PART_TYPE__UNKNOWN;
ContentValues cv = new ContentValues();
cv.put("type", messageType);
cv.put("mime_type", mimeType);
cv.put("decoded_body_size", size);
cv.put("display_name", name);
cv.put("header", mimeHeader.toString());
cv.put("encoding", MimeUtil.ENC_BINARY);
cv.put("data_location", attachmentFileToMove != null ? DATA_LOCATION__ON_DISK : DATA_LOCATION__MISSING);
cv.put("content_id", contentId);
cv.put("server_extra", storeData);
structureState.applyValues(cv);
long partId = db.insertOrThrow("message_parts", null, cv);
structureState = structureState.nextChild(partId);
if (attachmentFileToMove != null) {
boolean moveOk = attachmentFileToMove.renameTo(new File(attachmentDirNew, Long.toString(partId)));
if (!moveOk) {
Timber.e("Moving attachment to new dir failed!");
}
}
return structureState;
}
use of com.fsck.k9.mailstore.migrations.MigrationTo51.MimeStructureState in project k-9 by k9mail.
the class MigrationMimeStructureStateTest method init_apply_apply_shouldCrash.
@Test(expected = IllegalStateException.class)
public void init_apply_apply_shouldCrash() throws Exception {
MimeStructureState state = MimeStructureState.getNewRootState();
ContentValues cv = new ContentValues();
state.applyValues(cv);
state.applyValues(cv);
}
use of com.fsck.k9.mailstore.migrations.MigrationTo51.MimeStructureState in project k-9 by k9mail.
the class MigrationMimeStructureStateTest method init_apply_nextmulti_apply_shouldYieldMultipartChildValues.
@Test
public void init_apply_nextmulti_apply_shouldYieldMultipartChildValues() throws Exception {
MimeStructureState state = MimeStructureState.getNewRootState();
ContentValues cv = new ContentValues();
state.applyValues(cv);
state = state.nextMultipartChild(123);
cv.clear();
state.applyValues(cv);
Assert.assertEquals(123L, cv.get("root"));
Assert.assertEquals(123L, cv.get("parent"));
Assert.assertEquals(1, cv.get("seq"));
Assert.assertEquals(3, cv.size());
}
use of com.fsck.k9.mailstore.migrations.MigrationTo51.MimeStructureState in project k-9 by k9mail.
the class MigrationMimeStructureStateTest method init_apply_nextmulti_apply_pop_apply_shouldYieldFirstParentIdValues.
@Test
public void init_apply_nextmulti_apply_pop_apply_shouldYieldFirstParentIdValues() throws Exception {
MimeStructureState state = MimeStructureState.getNewRootState();
ContentValues cv = new ContentValues();
state.applyValues(cv);
state = state.nextMultipartChild(123);
cv.clear();
state.applyValues(cv);
state = state.nextMultipartChild(456);
state = state.popParent();
cv.clear();
state.applyValues(cv);
Assert.assertEquals(123L, cv.get("root"));
Assert.assertEquals(123L, cv.get("parent"));
Assert.assertEquals(2, cv.get("seq"));
Assert.assertEquals(3, cv.size());
}
Aggregations