Use attachment disposition for inline images

This commit is contained in:
M66B 2019-01-06 11:00:08 +00:00
parent ae1a36b482
commit 2be2d10c27
10 changed files with 1314 additions and 38 deletions

File diff suppressed because it is too large Load Diff

View File

@ -103,7 +103,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
}
private void bindTo(EntityAttachment attachment) {
ivDelete.setVisibility(readonly ? View.GONE : View.VISIBLE);
ivDelete.setVisibility(readonly ? View.GONE : attachment.isInline() ? View.INVISIBLE : View.VISIBLE);
tvName.setText(attachment.name);
if (attachment.size != null)
@ -128,7 +128,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
progressbar.setVisibility(
attachment.progress == null || attachment.available ? View.GONE : View.VISIBLE);
tvType.setText(attachment.type + " " + attachment.cid + " " + attachment.encryption);
tvType.setText(attachment.type + " " + attachment.disposition + " " + attachment.cid + " " + attachment.encryption);
tvType.setVisibility(debug ? View.VISIBLE : View.GONE);
}

View File

@ -630,7 +630,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
List<EntityAttachment> images = new ArrayList<>();
for (EntityAttachment attachment : attachments)
if (attachment.cid == null && attachment.type.startsWith("image"))
if (attachment.type.startsWith("image") && !attachment.isInline())
images.add(attachment);
adapterImage.set(images);
rvImage.setVisibility(images.size() > 0 ? View.VISIBLE : View.INVISIBLE);
@ -1850,7 +1850,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
long id = args.getLong("id");
List<EntityAttachment> attachments = DB.getInstance(context).attachment().getAttachments(id);
for (EntityAttachment attachment : attachments)
if (attachment.cid != null && !attachment.available)
if (!attachment.available && attachment.isInline())
return false;
return true;
}

View File

@ -49,7 +49,7 @@ import io.requery.android.database.sqlite.RequerySQLiteOpenHelperFactory;
// https://developer.android.com/topic/libraries/architecture/room.html
@Database(
version = 30,
version = 31,
entities = {
EntityIdentity.class,
EntityAccount.class,
@ -385,6 +385,13 @@ public abstract class DB extends RoomDatabase {
db.execSQL("UPDATE attachment SET encryption = " + EntityAttachment.PGP_MESSAGE + " where name = 'encrypted.asc'");
}
})
.addMigrations(new Migration(30, 31) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i("DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `attachment` ADD COLUMN `disposition` TEXT");
}
})
.build();
}

View File

@ -25,7 +25,6 @@ import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;
@Dao
public interface DaoAttachment {
@ -69,17 +68,9 @@ public interface DaoAttachment {
" WHERE id = :id")
void setDownloaded(long id, Integer size);
@Query("UPDATE attachment" +
" SET cid = NULL" +
" WHERE id = :id")
void clearCid(long id);
@Insert
long insertAttachment(EntityAttachment attachment);
@Update
void updateAttachment(EntityAttachment attachment);
@Query("DELETE FROM attachment" +
" WHERE id = :id")
int deleteAttachment(long id);

View File

@ -32,6 +32,7 @@ import java.io.OutputStream;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Part;
import androidx.annotation.NonNull;
import androidx.room.Entity;
@ -69,6 +70,7 @@ public class EntityAttachment {
public String name;
@NonNull
public String type;
public String disposition;
public String cid; // Content-ID
public Integer encryption;
public Integer size;
@ -79,6 +81,10 @@ public class EntityAttachment {
@Ignore
BodyPart part;
boolean isInline() {
return (disposition != null && disposition.equalsIgnoreCase(Part.INLINE));
}
static File getFile(Context context, Long id) {
File dir = new File(context.getFilesDir(), "attachments");
if (!dir.exists())

View File

@ -101,6 +101,7 @@ import java.util.Properties;
import javax.mail.Address;
import javax.mail.MessageRemovedException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
@ -1145,6 +1146,8 @@ public class FragmentCompose extends FragmentEx {
attachment.type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
if (attachment.type == null)
attachment.type = "application/octet-stream";
if (image)
attachment.disposition = Part.INLINE;
attachment.size = (s == null ? null : Integer.parseInt(s));
attachment.progress = 0;
@ -1422,12 +1425,13 @@ public class FragmentCompose extends FragmentEx {
List<EntityAttachment> attachments = db.attachment().getAttachments(ref.id);
for (EntityAttachment attachment : attachments)
if (attachment.available &&
("forward".equals(action) || attachment.cid != null)) {
("forward".equals(action) || attachment.isInline())) {
EntityAttachment copy = new EntityAttachment();
copy.message = result.draft.id;
copy.sequence = ++sequence;
copy.name = attachment.name;
copy.type = attachment.type;
copy.disposition = attachment.disposition;
copy.cid = attachment.cid;
copy.encryption = attachment.encryption;
copy.size = attachment.size;

View File

@ -43,7 +43,6 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@ -289,16 +288,4 @@ public class HtmlHelper {
return sb.toString();
}
static List<String> getCids(String html) {
List<String> result = new ArrayList<>();
for (Element element : Jsoup.parse(html).select("img")) {
String src = element.attr("src");
if (src.startsWith("cid:"))
result.add("<" + src.substring(4) + ">");
}
return result;
}
}

View File

@ -340,6 +340,8 @@ public class MessageHelper {
}
});
bpAttachment.setDataHandler(new DataHandler(dataSource));
if (attachment.disposition != null)
bpAttachment.setDisposition(attachment.disposition);
if (attachment.cid != null)
bpAttachment.setHeader("Content-ID", attachment.cid);
@ -639,6 +641,7 @@ public class MessageHelper {
EntityAttachment attachment = new EntityAttachment();
attachment.name = filename;
attachment.type = ct.getBaseType().toLowerCase();
attachment.disposition = disposition;
attachment.size = part.getSize();
attachment.cid = (cid == null || cid.length == 0 ? null : cid[0]);
attachment.encryption = (pgp ? EntityAttachment.PGP_MESSAGE : null);

View File

@ -1901,11 +1901,6 @@ public class ServiceSynchronize extends LifecycleService {
String preview = HtmlHelper.getPreview(body);
message.write(this, body);
db.message().setMessageContent(message.id, true, preview);
List<String> cids = HtmlHelper.getCids(body);
for (EntityAttachment attachment : db.attachment().getAttachments(message.id))
if (attachment.cid != null && !cids.contains(attachment.cid))
db.attachment().clearCid(attachment.id);
}
private void doAttachment(EntityFolder folder, EntityOperation op, IMAPFolder ifolder, EntityMessage message, JSONArray jargs, DB db) throws JSONException, MessagingException, IOException {
@ -2566,11 +2561,6 @@ public class ServiceSynchronize extends LifecycleService {
db.message().setMessageContent(
message.id, true, HtmlHelper.getPreview(body));
Log.i(folder.name + " downloaded message id=" + message.id + " size=" + message.size);
List<String> cids = HtmlHelper.getCids(body);
for (EntityAttachment attachment : attachments)
if (attachment.cid != null && !cids.contains(attachment.cid))
db.attachment().clearCid(attachment.id);
}
List<EntityAttachment> iattachments = null;