Fix improper deletion of stickers when restored from backup.

master
Cody Henthorne 2020-09-08 14:04:14 -04:00
parent 3b925f8674
commit b8c7e86223
4 changed files with 27 additions and 7 deletions

View File

@ -156,7 +156,7 @@ public class FullBackupImporter extends FullBackupBase {
private static void processSticker(@NonNull Context context, @NonNull AttachmentSecret attachmentSecret, @NonNull SQLiteDatabase db, @NonNull Sticker sticker, BackupRecordInputStream inputStream)
throws IOException
{
File stickerDirectory = context.getDir(AttachmentDatabase.DIRECTORY, Context.MODE_PRIVATE);
File stickerDirectory = context.getDir(StickerDatabase.DIRECTORY, Context.MODE_PRIVATE);
File dataFile = File.createTempFile("sticker", ".mms", stickerDirectory);
Pair<byte[], OutputStream> output = ModernEncryptingPartOutputStream.createFor(attachmentSecret, dataFile, false);

View File

@ -509,6 +509,8 @@ public class AttachmentDatabase extends Database {
}
}
filesInDb.addAll(DatabaseFactory.getStickerDatabase(context).getAllStickerFiles());
Set<String> onDiskButNotInDatabase = SetUtil.difference(filesOnDisk, filesInDb);
for (String filePath : onDiskButNotInDatabase) {

View File

@ -3,12 +3,12 @@ package org.thoughtcrime.securesms.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.Pair;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.sqlcipher.database.SQLiteDatabase;
import org.greenrobot.eventbus.EventBus;
@ -23,6 +23,7 @@ import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri;
import org.thoughtcrime.securesms.stickers.BlessedPacks;
import org.thoughtcrime.securesms.stickers.StickerPackInstallEvent;
import org.thoughtcrime.securesms.util.CursorUtil;
import org.thoughtcrime.securesms.util.Util;
import java.io.Closeable;
@ -30,9 +31,9 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class StickerDatabase extends Database {
@ -77,7 +78,7 @@ public class StickerDatabase extends Database {
"CREATE INDEX IF NOT EXISTS sticker_sticker_id_index ON " + TABLE_NAME + " (" + STICKER_ID + ");"
};
private static final String DIRECTORY = "stickers";
public static final String DIRECTORY = "stickers";
private final AttachmentSecret attachmentSecret;
@ -190,6 +191,19 @@ public class StickerDatabase extends Database {
return cursor;
}
public @NonNull Set<String> getAllStickerFiles() {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Set<String> files = new HashSet<>();
try (Cursor cursor = db.query(TABLE_NAME, new String[] { FILE_PATH }, null, null, null, null, null)) {
while (cursor != null && cursor.moveToNext()) {
files.add(CursorUtil.requireString(cursor, FILE_PATH));
}
}
return files;
}
public @Nullable InputStream getStickerStream(long rowId) throws IOException {
String selection = _ID + " = ?";
String[] args = new String[] { String.valueOf(rowId) };

View File

@ -296,6 +296,10 @@ public class ThreadDatabase extends Database {
}
public void trimThread(long threadId, int length, long trimBeforeDate) {
if (length == NO_TRIM_MESSAGE_COUNT_SET && trimBeforeDate == NO_TRIM_BEFORE_DATE_SET) {
return;
}
SQLiteDatabase db = databaseHelper.getWritableDatabase();
AttachmentDatabase attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context);
GroupReceiptDatabase groupReceiptDatabase = DatabaseFactory.getGroupReceiptDatabase(context);