Get attachment meta info

This commit is contained in:
M66B
2018-08-03 12:07:51 +00:00
parent 485ef3ff56
commit bb4bed926a
7 changed files with 753 additions and 14 deletions

View File

@@ -37,9 +37,10 @@ import android.util.Log;
EntityAccount.class,
EntityFolder.class,
EntityMessage.class,
EntityAttachment.class,
EntityOperation.class
},
version = 1,
version = 2,
exportSchema = true
)
@@ -53,6 +54,8 @@ public abstract class DB extends RoomDatabase {
public abstract DaoMessage message();
public abstract DaoAttachment attachment();
public abstract DaoOperation operation();
private static DB sInstance;
@@ -67,7 +70,7 @@ public abstract class DB extends RoomDatabase {
private static DB migrate(RoomDatabase.Builder<DB> builder) {
return builder
//.addMigrations(MIGRATION_1_2)
.addMigrations(MIGRATION_1_2)
.build();
}
@@ -75,7 +78,14 @@ public abstract class DB extends RoomDatabase {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE message ADD COLUMN error TEXT");
db.execSQL("CREATE TABLE IF NOT EXISTS `attachment`" +
" (`id` INTEGER PRIMARY KEY AUTOINCREMENT" +
", `message` INTEGER NOT NULL" +
", `sequence` INTEGER NOT NULL" +
", `type` TEXT NOT NULL, `name` TEXT" +
", `content` BLOB, FOREIGN KEY(`message`) REFERENCES `message`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )");
db.execSQL("CREATE INDEX `index_attachment_message` ON `attachment` (`message`)");
db.execSQL("CREATE UNIQUE INDEX `index_attachment_message_sequence` ON `attachment` (`message`, `sequence`)");
}
};

View File

@@ -0,0 +1,30 @@
package eu.faircode.email;
/*
This file is part of Safe email.
Safe email is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.OnConflictStrategy;
@Dao
public interface DaoAttachment {
@Insert(onConflict = OnConflictStrategy.REPLACE)
long insertAttachment(EntityAttachment attachment);
}

View File

@@ -33,7 +33,8 @@ import static android.arch.persistence.room.ForeignKey.CASCADE;
@ForeignKey(childColumns = "message", entity = EntityMessage.class, parentColumns = "id", onDelete = CASCADE)
},
indices = {
@Index(value = {"message"})
@Index(value = {"message"}),
@Index(value = {"message", "sequence"}, unique = true)
}
)
public class EntityAttachment {
@@ -44,7 +45,9 @@ public class EntityAttachment {
@NonNull
public Long message;
@NonNull
public String type;
public Integer sequence;
public String name;
@NonNull
public String type;
public byte[] content;
}

View File

@@ -37,12 +37,14 @@ import java.util.List;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.internet.ContentType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
@@ -123,6 +125,10 @@ public class MessageHelper {
this.imessage = new MimeMessage(isession, is);
}
boolean getSeen() throws MessagingException {
return imessage.isSet(Flags.Flag.SEEN);
}
String getMessageID() throws MessagingException {
return imessage.getHeader("Message-ID", null);
}
@@ -205,10 +211,6 @@ public class MessageHelper {
return result.toArray(new Address[0]);
}
String getHtml() throws MessagingException {
return getHtml(imessage);
}
static String getFormattedAddresses(String json) {
if (json == null)
return null;
@@ -230,6 +232,10 @@ public class MessageHelper {
}
}
String getHtml() throws MessagingException {
return getHtml(imessage);
}
private String getHtml(Part part) throws MessagingException {
if (part.isMimeType("text/*"))
try {
@@ -280,8 +286,42 @@ public class MessageHelper {
return null;
}
boolean getSeen() throws MessagingException {
return imessage.isSet(Flags.Flag.SEEN);
public List<EntityAttachment> getAttachments() throws IOException, MessagingException {
List<EntityAttachment> result = new ArrayList<>();
Object content = imessage.getContent();
if (content instanceof String)
return result;
if (content instanceof Multipart) {
Multipart multipart = (Multipart) content;
for (int i = 0; i < multipart.getCount(); i++)
result.addAll(getAttachments(multipart.getBodyPart(i)));
}
return result;
}
private List<EntityAttachment> getAttachments(BodyPart part) throws IOException, MessagingException {
List<EntityAttachment> result = new ArrayList<>();
Object content = part.getContent();
if (content instanceof InputStream || content instanceof String) {
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()) || !TextUtils.isEmpty(part.getFileName())) {
ContentType ct = new ContentType(part.getContentType());
EntityAttachment attachment = new EntityAttachment();
attachment.sequence = result.size() + 1;
attachment.name = part.getFileName();
attachment.type = ct.getBaseType();
result.add(attachment);
}
} else if (content instanceof Multipart) {
Multipart multipart = (Multipart) content;
for (int i = 0; i < multipart.getCount(); i++)
result.addAll(getAttachments(multipart.getBodyPart(i)));
}
return result;
}
String getRaw() throws IOException, MessagingException {

View File

@@ -50,6 +50,7 @@ import com.sun.mail.imap.protocol.IMAPProtocol;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@@ -101,7 +102,10 @@ public class ServiceSynchronize extends LifecycleService {
}
public ServiceSynchronize() {
// https://docs.oracle.com/javaee/6/api/javax/mail/internet/package-summary.html
System.setProperty("mail.mime.ignoreunknownencoding", "true");
System.setProperty("mail.mime.decodefilename", "true");
System.setProperty("mail.mime.encodefilename", "true");
}
@Override
@@ -383,7 +387,7 @@ public class ServiceSynchronize extends LifecycleService {
Log.i(Helper.TAG, account.name + " stopped");
}
private void monitorFolder(final EntityAccount account, final EntityFolder folder, final IMAPStore istore) throws MessagingException, JSONException {
private void monitorFolder(final EntityAccount account, final EntityFolder folder, final IMAPStore istore) throws MessagingException, JSONException, IOException {
IMAPFolder ifolder = null;
try {
Log.i(Helper.TAG, folder.name + " start");
@@ -722,7 +726,7 @@ public class ServiceSynchronize extends LifecycleService {
}
}
private void synchronizeMessages(EntityFolder folder, IMAPFolder ifolder) throws MessagingException, JSONException {
private void synchronizeMessages(EntityFolder folder, IMAPFolder ifolder) throws MessagingException, JSONException, IOException {
try {
Log.i(Helper.TAG, folder.name + " start sync after=" + folder.after);
@@ -801,7 +805,7 @@ public class ServiceSynchronize extends LifecycleService {
}
}
private void synchronizeMessage(EntityFolder folder, IMAPFolder ifolder, IMAPMessage imessage) throws MessagingException, JSONException {
private void synchronizeMessage(EntityFolder folder, IMAPFolder ifolder, IMAPMessage imessage) throws MessagingException, JSONException, IOException {
FetchProfile fp = new FetchProfile();
fp.add(UIDFolder.FetchProfileItem.UID);
fp.add(IMAPFolder.FetchProfileItem.FLAGS);
@@ -856,6 +860,13 @@ public class ServiceSynchronize extends LifecycleService {
message.id = db.message().insertMessage(message);
Log.i(Helper.TAG, folder.name + " added id=" + message.id);
for (EntityAttachment attachment : helper.getAttachments()) {
Log.i(Helper.TAG, "attachment name=" + attachment.name + " type=" + attachment.type);
attachment.message = message.id;
db.attachment().insertAttachment(attachment);
}
} else if (message.seen != seen) {
message.seen = seen;
message.ui_seen = seen;