Browse Source

Put messages large than 4K into an attachment

main
M66B 6 years ago
parent
commit
ff12eaf0fb
1 changed files with 88 additions and 36 deletions
  1. +88
    -36
      app/src/main/java/eu/faircode/email/ServiceSynchronize.java

+ 88
- 36
app/src/main/java/eu/faircode/email/ServiceSynchronize.java View File

@ -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();
message.account = folder.account;
message.folder = folder.id;
message.uid = uid;
try {
db.beginTransaction();
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);
}
EntityMessage message = new EntityMessage();
message.account = folder.account;
message.folder = folder.id;
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());
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;
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);
FileWriter out = null;
try {
out = new FileWriter(file);
out.write(large);
} catch (IOException e) {
Log.e(Helper.TAG, e + "\n" + Log.getStackTraceString(e));
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
Log.e(Helper.TAG, e + "\n" + Log.getStackTraceString(e));
}
}
}
attachment.filename = file.getName();
db.attachment().updateAttachment(attachment);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
} }
return 1; return 1;


Loading…
Cancel
Save