diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientTable.kt b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientTable.kt index 33d3ae6456..a24a034942 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientTable.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientTable.kt @@ -2061,7 +2061,18 @@ open class RecipientTable(context: Context, databaseHelper: SignalDatabase) : Da * Migrates all recipients using [legacyUri] for their wallpaper to [newUri]. * Needed for an app migration. */ - fun migrateWallpaperUri(legacyUri: Uri, newUri: Uri): Int { + fun migrateWallpaperUri(legacyUri: Uri, newUri: Uri?): Int { + if (newUri == null) { + return writableDatabase + .update(TABLE_NAME) + .values( + WALLPAPER to null, + WALLPAPER_URI to null + ) + .where("$WALLPAPER_URI = ?", legacyUri) + .run() + } + val newWallpaper = ChatWallpaperFactory.create(newUri) return writableDatabase diff --git a/app/src/main/java/org/thoughtcrime/securesms/migrations/WallpaperStorageMigrationJob.kt b/app/src/main/java/org/thoughtcrime/securesms/migrations/WallpaperStorageMigrationJob.kt index 3b6fe64f9e..98a528d816 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/migrations/WallpaperStorageMigrationJob.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/migrations/WallpaperStorageMigrationJob.kt @@ -50,13 +50,25 @@ internal class WallpaperStorageMigrationJob(parameters: Parameters = Parameters. val currentDefaultWallpaperUri = SignalStore.wallpaper.currentRawWallpaper?.file_?.uri for (filename in wallpaperFileNames) { - val inputStream = FileStorage.read(context, DIRECTORY, filename) - val wallpaperAttachmentId = SignalDatabase.attachments.insertWallpaper(inputStream) - + val legacyUri = Uri.withAppendedPath(CONTENT_URI, filename) val directory = context.getDir(DIRECTORY, Context.MODE_PRIVATE) val file = File(directory, filename) - val legacyUri = Uri.withAppendedPath(CONTENT_URI, filename) + val inputStream = try { + FileStorage.read(context, DIRECTORY, filename) + } catch (e: IOException) { + Log.w(TAG, "Failed to read $filename! Clearing references.") + val updatedUserCount = SignalDatabase.recipients.migrateWallpaperUri( + legacyUri = legacyUri, + newUri = null + ) + Log.d(TAG, "Wallpaper with name '$filename' was in use by $updatedUserCount recipients.") + file.delete() + continue + } + + val wallpaperAttachmentId = SignalDatabase.attachments.insertWallpaper(inputStream) + val newUri = PartAuthority.getAttachmentDataUri(wallpaperAttachmentId) val updatedUserCount = SignalDatabase.recipients.migrateWallpaperUri(