Put messages large than 4K into an attachment

pull/50/head
M66B 6 years ago
parent 28b9b80e7d
commit ff12eaf0fb

@ -58,6 +58,7 @@ import org.json.JSONException;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -120,6 +121,7 @@ public class ServiceSynchronize extends LifecycleService {
private static final int CONNECT_BACKOFF_MAX = 128; // seconds private static final int CONNECT_BACKOFF_MAX = 128; // seconds
private static final long STORE_NOOP_INTERVAL = 9 * 60 * 1000L; // ms private static final long STORE_NOOP_INTERVAL = 9 * 60 * 1000L; // ms
private static final long FOLDER_NOOP_INTERVAL = 9 * 60 * 1000L; // ms private static final long FOLDER_NOOP_INTERVAL = 9 * 60 * 1000L; // ms
private static final int MAX_MESSAGE_BODY_SIZE = 4096;
private static final int ATTACHMENT_BUFFER_SIZE = 8192; // bytes private static final int ATTACHMENT_BUFFER_SIZE = 8192; // bytes
static final String ACTION_PROCESS_OPERATIONS = BuildConfig.APPLICATION_ID + ".PROCESS_OPERATIONS"; static final String ACTION_PROCESS_OPERATIONS = BuildConfig.APPLICATION_ID + ".PROCESS_OPERATIONS";
@ -1267,44 +1269,94 @@ public class ServiceSynchronize extends LifecycleService {
fp1.add(IMAPFolder.FetchProfileItem.MESSAGE); fp1.add(IMAPFolder.FetchProfileItem.MESSAGE);
ifolder.fetch(new Message[]{imessage}, fp1); ifolder.fetch(new Message[]{imessage}, fp1);
EntityMessage message = new EntityMessage(); try {
message.account = folder.account; db.beginTransaction();
message.folder = folder.id;
message.uid = uid;
if (!EntityFolder.ARCHIVE.equals(folder.type)) { EntityMessage message = new EntityMessage();
message.msgid = helper.getMessageID(); message.account = folder.account;
if (TextUtils.isEmpty(message.msgid)) message.folder = folder.id;
Log.w(Helper.TAG, "No Message-ID id=" + message.id + " uid=" + message.uid); message.uid = uid;
}
if (!EntityFolder.ARCHIVE.equals(folder.type)) {
message.msgid = helper.getMessageID();
if (TextUtils.isEmpty(message.msgid))
Log.w(Helper.TAG, "No Message-ID id=" + message.id + " uid=" + message.uid);
}
message.references = TextUtils.join(" ", helper.getReferences());
message.inreplyto = helper.getInReplyTo();
message.thread = helper.getThreadId(uid);
message.from = helper.getFrom();
message.to = helper.getTo();
message.cc = helper.getCc();
message.bcc = helper.getBcc();
message.reply = helper.getReply();
message.subject = imessage.getSubject();
message.body = helper.getHtml();
message.received = imessage.getReceivedDate().getTime();
message.sent = (imessage.getSentDate() == null ? null : imessage.getSentDate().getTime());
message.seen = seen;
message.ui_seen = seen;
message.ui_hide = false;
String large = null;
if (message.body != null && message.body.length() > MAX_MESSAGE_BODY_SIZE) {
large = message.body;
message.body = null;
}
message.id = db.message().insertMessage(message);
Log.i(Helper.TAG, folder.name + " added id=" + message.id + " uid=" + message.uid);
int sequence = 0;
for (EntityAttachment attachment : helper.getAttachments()) {
sequence++;
Log.i(Helper.TAG, "attachment seq=" + sequence +
" name=" + attachment.name + " type=" + attachment.type);
attachment.message = message.id;
attachment.sequence = sequence;
attachment.id = db.attachment().insertAttachment(attachment);
}
if (large != null) {
sequence++;
EntityAttachment attachment = new EntityAttachment();
attachment.message = message.id;
attachment.sequence = sequence;
attachment.name = "body.html"; // TODO: string resource
attachment.type = "text/html";
attachment.size = large.length();
attachment.id = db.attachment().insertAttachment(attachment);
// Build filename
File dir = new File(getFilesDir(), "attachments");
dir.mkdir();
File file = new File(dir, Long.toString(attachment.id));
message.references = TextUtils.join(" ", helper.getReferences()); FileWriter out = null;
message.inreplyto = helper.getInReplyTo(); try {
message.thread = helper.getThreadId(uid); out = new FileWriter(file);
message.from = helper.getFrom(); out.write(large);
message.to = helper.getTo(); } catch (IOException e) {
message.cc = helper.getCc(); Log.e(Helper.TAG, e + "\n" + Log.getStackTraceString(e));
message.bcc = helper.getBcc(); } finally {
message.reply = helper.getReply(); if (out != null) {
message.subject = imessage.getSubject(); try {
message.body = helper.getHtml(); out.close();
message.received = imessage.getReceivedDate().getTime(); } catch (IOException e) {
message.sent = (imessage.getSentDate() == null ? null : imessage.getSentDate().getTime()); Log.e(Helper.TAG, e + "\n" + Log.getStackTraceString(e));
message.seen = seen; }
message.ui_seen = seen; }
message.ui_hide = false; }
message.id = db.message().insertMessage(message); attachment.filename = file.getName();
Log.i(Helper.TAG, folder.name + " added id=" + message.id + " uid=" + message.uid); db.attachment().updateAttachment(attachment);
}
int sequence = 0;
for (EntityAttachment attachment : helper.getAttachments()) { db.setTransactionSuccessful();
sequence++; } finally {
Log.i(Helper.TAG, "attachment seq=" + sequence + db.endTransaction();
" name=" + attachment.name + " type=" + attachment.type);
attachment.message = message.id;
attachment.sequence = sequence;
attachment.id = db.attachment().insertAttachment(attachment);
} }
return 1; return 1;

Loading…
Cancel
Save