Added attachment zipping

pull/210/head
M66B 3 years ago
parent 119d30e6fb
commit 078820c7c9

@ -108,6 +108,12 @@ public interface DaoAttachment {
" AND NOT (error IS :error)") " AND NOT (error IS :error)")
void setWarning(long id, String error); void setWarning(long id, String error);
@Query("UPDATE attachment" +
" SET name = :name, type = :type, size= :size" +
" WHERE id = :id" +
" AND NOT (name IS name AND type IS :type AND size IS :size)")
void setName(long id, String name, String type, Long size);
@Query("UPDATE attachment" + @Query("UPDATE attachment" +
" SET type = :type" + " SET type = :type" +
" WHERE id = :id" + " WHERE id = :id" +

@ -32,12 +32,19 @@ import androidx.room.ForeignKey;
import androidx.room.Index; import androidx.room.Index;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.mail.Part; import javax.mail.Part;
@ -335,6 +342,23 @@ public class EntityAttachment {
return type; return type;
} }
void zip(Context context) throws IOException {
File file = getFile(context);
File zip = new File(file.getAbsolutePath() + ".zip");
try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zip)))) {
ZipEntry entry = new ZipEntry(name);
out.putNextEntry(entry);
Helper.copy(in, out);
}
}
DB db = DB.getInstance(context);
db.attachment().setName(id, name + ".zip", "application/zip", zip.length());
file.delete();
}
public static boolean equals(List<EntityAttachment> a1, List<EntityAttachment> a2) { public static boolean equals(List<EntityAttachment> a1, List<EntityAttachment> a2) {
if (a1 == null || a2 == null) if (a1 == null || a2 == null)
return false; return false;

Loading…
Cancel
Save