Remove oldest attachments files when deleted from entries

This commit is contained in:
J-Jamet
2020-08-26 21:25:51 +02:00
parent a994bf9dd8
commit 359d85727e
6 changed files with 20 additions and 6 deletions

View File

@@ -361,7 +361,7 @@ class EntryEditActivity : LockingActivity(),
mDatabase?.binaryPool?.let { binaryPool ->
assignAttachments(newEntry.getAttachments(binaryPool), StreamDirection.UPLOAD) { attachment ->
newEntry.removeAttachment(attachment, binaryPool)
newEntry.removeAttachment(attachment)
}
}
}

View File

@@ -34,7 +34,6 @@ open class SaveDatabaseRunnable(protected var context: Context,
override fun onStartRun() {}
override fun onActionRun() {
// TODO as setting database.removeUnlinkedAttachments()
if (saveDatabase && result.isSuccess) {
try {
database.saveData(context.contentResolver)

View File

@@ -64,6 +64,10 @@ class DeleteNodesRunnable(context: Context,
} else {
database.deleteEntry(currentNode)
}
// Remove the oldest attachments
currentNode.getAttachments(database.binaryPool).forEach {
database.removeAttachmentIfNotUsed(it)
}
}
}
}

View File

@@ -20,6 +20,7 @@
package com.kunzisoft.keepass.database.action.node
import android.content.Context
import com.kunzisoft.keepass.database.element.Attachment
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.Entry
import com.kunzisoft.keepass.database.element.node.Node
@@ -40,6 +41,12 @@ class UpdateEntryRunnable constructor(
// WARNING : Re attribute parent removed in entry edit activity to save memory
mNewEntry.addParentFrom(mOldEntry)
// Build oldest attachments
val oldEntryAttachments = mOldEntry.getAttachments(database.binaryPool)
val newEntryAttachments = mNewEntry.getAttachments(database.binaryPool)
val differenceAttachments = ArrayList<Attachment>(oldEntryAttachments)
differenceAttachments.removeAll(newEntryAttachments)
// Update entry with new values
mOldEntry.updateWith(mNewEntry)
mNewEntry.touch(modified = true, touchParents = true)
@@ -50,6 +57,11 @@ class UpdateEntryRunnable constructor(
// Only change data in index
database.updateEntry(mOldEntry)
// Remove oldest attachments
differenceAttachments.forEach {
database.removeAttachmentIfNotUsed(it)
}
}
override fun nodeFinish(): ActionNodesValues {

View File

@@ -346,9 +346,9 @@ class Entry : Node, EntryVersionedInterface<Group> {
entryKDBX?.putAttachment(attachment, binaryPool)
}
fun removeAttachment(attachment: Attachment, binaryPool: BinaryPool) {
fun removeAttachment(attachment: Attachment) {
entryKDB?.removeAttachment(attachment)
entryKDBX?.removeAttachment(attachment, binaryPool)
entryKDBX?.removeAttachment(attachment)
}
fun getHistory(): ArrayList<Entry> {

View File

@@ -305,9 +305,8 @@ class EntryKDBX : EntryVersioned<UUID, UUID, GroupKDBX, EntryKDBX>, NodeKDBXInte
binaries[attachment.name] = binaryPool.put(attachment.binaryAttachment)
}
fun removeAttachment(attachment: Attachment, binaryPool: BinaryPool) {
fun removeAttachment(attachment: Attachment) {
binaries.remove(attachment.name)
binaryPool.remove(attachment.binaryAttachment)
}
private fun getAttachmentsSize(binaryPool: BinaryPool): Long {