mirror of
https://github.com/M66B/FairEmail.git
synced 2025-02-24 15:11:03 +00:00
parent
7e3f4563d1
commit
6d3c4a96fa
3 changed files with 69 additions and 19 deletions
|
@ -39,6 +39,12 @@ public interface DaoAttachment {
|
|||
" WHERE message = :message")
|
||||
int getAttachmentCount(long message);
|
||||
|
||||
@Query("SELECT COUNT(id)" +
|
||||
" FROM attachment" +
|
||||
" WHERE message = :message" +
|
||||
" AND name = :name")
|
||||
int getAttachmentCount(long message, String name);
|
||||
|
||||
@Query("SELECT COUNT(id)" +
|
||||
" FROM attachment" +
|
||||
" WHERE id = :id")
|
||||
|
|
|
@ -68,6 +68,7 @@ import org.openintents.openpgp.util.OpenPgpApi;
|
|||
import org.openintents.openpgp.util.OpenPgpServiceConnection;
|
||||
import org.xml.sax.XMLReader;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
|
@ -75,6 +76,7 @@ import java.io.FileInputStream;
|
|||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.text.Collator;
|
||||
import java.text.DateFormat;
|
||||
|
@ -86,8 +88,6 @@ import java.util.Date;
|
|||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.mail.Multipart;
|
||||
import javax.mail.Part;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.InternetHeaders;
|
||||
import javax.mail.internet.MimeBodyPart;
|
||||
|
@ -1185,24 +1185,68 @@ public class FragmentMessage extends FragmentEx {
|
|||
Log.i(Helper.TAG, "Decrypted");
|
||||
String decrypted = os.toString("UTF-8");
|
||||
if (isPart) {
|
||||
String plain = null;
|
||||
String html = null;
|
||||
InternetHeaders ih = new InternetHeaders();
|
||||
ih.addHeader("Content-Type", "multipart/alternative");
|
||||
MimeBodyPart part = new MimeBodyPart(ih, decrypted.getBytes());
|
||||
Multipart mp = (Multipart) part.getContent();
|
||||
for (int i = 0; i < mp.getCount(); i++) {
|
||||
Part bp = mp.getBodyPart(i);
|
||||
if (bp.isMimeType("text/plain"))
|
||||
plain = bp.getContent().toString();
|
||||
else if (bp.isMimeType("text/html"))
|
||||
html = bp.getContent().toString();
|
||||
}
|
||||
final MimeBodyPart part = new MimeBodyPart(ih, decrypted.getBytes());
|
||||
FragmentMessage.this.decrypted = MessageHelper.getHtml(part);
|
||||
|
||||
if (html != null)
|
||||
FragmentMessage.this.decrypted = html;
|
||||
else if (plain != null)
|
||||
FragmentMessage.this.decrypted = "<pre>" + plain.replaceAll("\\r?\\n", "<br />") + "</pre>";
|
||||
// Store attachments
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
DB db = DB.getInstance(getContext());
|
||||
int sequence = db.attachment().getAttachmentCount(message.id);
|
||||
|
||||
for (EntityAttachment attachment : MessageHelper.getAttachments(part))
|
||||
if (db.attachment().getAttachmentCount(message.id, attachment.name) == 0)
|
||||
try {
|
||||
db.beginTransaction();
|
||||
|
||||
attachment.message = message.id;
|
||||
attachment.sequence = ++sequence;
|
||||
attachment.id = db.attachment().insertAttachment(attachment);
|
||||
|
||||
File file = EntityAttachment.getFile(getContext(), attachment.id);
|
||||
|
||||
// Store attachment
|
||||
InputStream is = null;
|
||||
OutputStream os = null;
|
||||
try {
|
||||
is = attachment.part.getInputStream();
|
||||
os = new BufferedOutputStream(new FileOutputStream(file));
|
||||
|
||||
int size = 0;
|
||||
byte[] buffer = new byte[4096];
|
||||
for (int len = is.read(buffer); len != -1; len = is.read(buffer)) {
|
||||
size += len;
|
||||
os.write(buffer, 0, len);
|
||||
}
|
||||
|
||||
// Store attachment data
|
||||
attachment.size = size;
|
||||
attachment.progress = null;
|
||||
attachment.available = true;
|
||||
db.attachment().updateAttachment(attachment);
|
||||
} finally {
|
||||
try {
|
||||
if (is != null)
|
||||
is.close();
|
||||
} finally {
|
||||
if (os != null)
|
||||
os.close();
|
||||
}
|
||||
}
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
}
|
||||
} catch (Throwable ex) {
|
||||
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
} else
|
||||
FragmentMessage.this.decrypted = "<pre>" + decrypted.replaceAll("\\r?\\n", "<br />") + "</pre>";
|
||||
|
||||
|
|
|
@ -281,7 +281,7 @@ public class MessageHelper {
|
|||
return getHtml(imessage);
|
||||
}
|
||||
|
||||
private String getHtml(Part part) throws MessagingException, IOException {
|
||||
static String getHtml(Part part) throws MessagingException, IOException {
|
||||
if (part.isMimeType("text/*")) {
|
||||
String s;
|
||||
try {
|
||||
|
@ -361,7 +361,7 @@ public class MessageHelper {
|
|||
return result;
|
||||
}
|
||||
|
||||
private List<EntityAttachment> getAttachments(BodyPart part) throws
|
||||
static List<EntityAttachment> getAttachments(BodyPart part) throws
|
||||
IOException, MessagingException {
|
||||
List<EntityAttachment> result = new ArrayList<>();
|
||||
|
||||
|
|
Loading…
Reference in a new issue