mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-04 16:45:09 -05:00
Get size of decoded body content when saving
Before downloading we show the encoded size of attachments. After download we strip the transport encoding to find out the size of the decoded content.
This commit is contained in:
parent
98bdf54672
commit
564e2432e1
@ -42,11 +42,13 @@ import com.fsck.k9.mail.MessageRetrievalListener;
|
||||
import com.fsck.k9.mail.MessagingException;
|
||||
import com.fsck.k9.mail.Multipart;
|
||||
import com.fsck.k9.mail.Part;
|
||||
import com.fsck.k9.mail.filter.CountingOutputStream;
|
||||
import com.fsck.k9.mail.internet.MimeHeader;
|
||||
import com.fsck.k9.mail.internet.MimeMessage;
|
||||
import com.fsck.k9.mail.internet.MimeMultipart;
|
||||
import com.fsck.k9.mailstore.LockableDatabase.DbCallback;
|
||||
import com.fsck.k9.mailstore.LockableDatabase.WrappedException;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.james.mime4j.MimeException;
|
||||
import org.apache.james.mime4j.parser.ContentHandler;
|
||||
import org.apache.james.mime4j.parser.MimeStreamParser;
|
||||
@ -1373,11 +1375,11 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
|
||||
byte[] bodyData = getBodyBytes(body);
|
||||
String encoding = getTransferEncoding(part);
|
||||
|
||||
if (attachment.size == AttachmentViewInfo.UNKNOWN_SIZE) {
|
||||
//FIXME: Use size of content when transfer encoding is stripped
|
||||
long size = decodeAndCountBytes(bodyData, encoding);
|
||||
if (size == AttachmentViewInfo.UNKNOWN_SIZE) {
|
||||
cv.put("decoded_body_size", bodyData.length);
|
||||
} else {
|
||||
cv.put("decoded_body_size", attachment.size);
|
||||
cv.put("decoded_body_size", size);
|
||||
}
|
||||
|
||||
cv.put("encoding", encoding);
|
||||
@ -1387,6 +1389,29 @@ public class LocalFolder extends Folder<LocalMessage> implements Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
private long decodeAndCountBytes(byte[] bodyData, String encoding) {
|
||||
ByteArrayInputStream rawInputStream = new ByteArrayInputStream(bodyData);
|
||||
return decodeAndCountBytes(encoding, rawInputStream);
|
||||
}
|
||||
|
||||
private long decodeAndCountBytes(String encoding, ByteArrayInputStream rawInputStream) {
|
||||
InputStream decodingInputStream = localStore.getDecodingInputStream(rawInputStream, encoding);
|
||||
try {
|
||||
CountingOutputStream countingOutputStream = new CountingOutputStream();
|
||||
try {
|
||||
IOUtils.copy(decodingInputStream, countingOutputStream);
|
||||
|
||||
return countingOutputStream.getCount();
|
||||
} catch (IOException e) {
|
||||
return AttachmentViewInfo.UNKNOWN_SIZE;
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
decodingInputStream.close();
|
||||
} catch (IOException e) { /* ignore */ }
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] getHeaderBytes(Part part) throws IOException, MessagingException {
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
part.writeHeaderTo(output);
|
||||
|
@ -717,7 +717,7 @@ public class LocalStore extends Store implements Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
private InputStream getDecodingInputStream(InputStream rawInputStream, String encoding) {
|
||||
InputStream getDecodingInputStream(InputStream rawInputStream, String encoding) {
|
||||
if (MimeUtil.ENC_BASE64.equals(encoding)) {
|
||||
return new Base64InputStream(rawInputStream);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user