Skip to content

Commit 0315f3b

Browse files
authored
Merge pull request #73 from claucambra/bugfix/improve-delete-api
Improve readability of item deletion procedure
2 parents 6e4db49 + 0dd740a commit 0315f3b

1 file changed

Lines changed: 49 additions & 37 deletions

File tree

Sources/NextcloudFileProviderKit/Item/Item+Delete.swift

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ import NextcloudKit
1212
import OSLog
1313

1414
public extension Item {
15-
// NOTE: the trashing metadata modification procedure here is rough. You SHOULD run a rescan of
16-
// the trash in order to ensure you are getting a correct picture of the item's current remote
17-
// state! This is important particularly for receiving the correct trash bin filename in case of
18-
// there being a previous item in the trash with the same name, prompting the server to rename
19-
// the newly-trashed target item
15+
// NOTE: The trashing parameter does not affect whether the server will trash this or not.
16+
// That's out of our hands. Instead, this is used internally to properly handle the metadata
17+
// update automatically when we conduct a move of an item to the trash.
2018
func delete(
2119
trashing: Bool = false,
2220
domain: NSFileProviderDomain? = nil,
@@ -83,45 +81,59 @@ public extension Item {
8381
"""
8482
)
8583

86-
guard !trashing else {
87-
if self.metadata.directory {
88-
_ = dbManager.renameDirectoryAndPropagateToChildren(
89-
ocId: ocId,
90-
newServerUrl: account.trashUrl,
91-
newFileName: filename
92-
)
93-
} else {
94-
dbManager.renameItemMetadata(
95-
ocId: ocId, newServerUrl: account.trashUrl, newFileName: filename
96-
)
97-
}
98-
99-
guard var metadata = dbManager.itemMetadata(ocId: ocId) else {
100-
Self.logger.warning(
101-
"""
102-
Could not find item metadata for \(self.filename, privacy: .public)
103-
\(self.itemIdentifier.rawValue, privacy: .public)!
104-
Cannot finish trashing procedure.
105-
"""
106-
)
107-
return NSFileProviderError(.cannotSynchronize)
108-
}
109-
metadata.trashbinFileName = filename
110-
metadata.trashbinDeletionTime = Date()
111-
metadata.trashbinOriginalLocation =
112-
String(self.metadata.serverUrl + "/" + filename).replacingOccurrences(
113-
of: account.davFilesUrl + "/", with: ""
114-
)
115-
116-
dbManager.addItemMetadata(metadata)
117-
84+
guard trashing else {
85+
handleMetadataDeletion()
11886
return nil
11987
}
88+
return handleMetadataTrashModification()
89+
}
90+
91+
private func handleMetadataDeletion() {
92+
let ocId = metadata.ocId
12093
if self.metadata.directory {
12194
_ = dbManager.deleteDirectoryAndSubdirectoriesMetadata(ocId: ocId)
12295
} else {
12396
dbManager.deleteItemMetadata(ocId: ocId)
12497
}
98+
}
99+
100+
// NOTE: the trashing metadata modification procedure here is rough. You SHOULD run a rescan of
101+
// the trash in order to ensure you are getting a correct picture of the item's current remote
102+
// state! This is important particularly for receiving the correct trash bin filename in case of
103+
// there being a previous item in the trash with the same name, prompting the server to rename
104+
// the newly-trashed target item
105+
private func handleMetadataTrashModification() -> Error? {
106+
let ocId = metadata.ocId
107+
if metadata.directory {
108+
_ = dbManager.renameDirectoryAndPropagateToChildren(
109+
ocId: ocId,
110+
newServerUrl: account.trashUrl,
111+
newFileName: filename
112+
)
113+
} else {
114+
dbManager.renameItemMetadata(
115+
ocId: ocId, newServerUrl: account.trashUrl, newFileName: filename
116+
)
117+
}
118+
119+
guard var metadata = dbManager.itemMetadata(ocId: ocId) else {
120+
Self.logger.warning(
121+
"""
122+
Could not find item metadata for \(self.filename, privacy: .public)
123+
\(self.itemIdentifier.rawValue, privacy: .public)!
124+
Cannot finish trashing procedure.
125+
"""
126+
)
127+
return NSFileProviderError(.cannotSynchronize)
128+
}
129+
metadata.trashbinFileName = filename
130+
metadata.trashbinDeletionTime = Date()
131+
metadata.trashbinOriginalLocation =
132+
String(self.metadata.serverUrl + "/" + filename).replacingOccurrences(
133+
of: account.davFilesUrl + "/", with: ""
134+
)
135+
136+
dbManager.addItemMetadata(metadata)
125137
return nil
126138
}
127139
}

0 commit comments

Comments
 (0)