Skip to content

Commit e5f7d9e

Browse files
committed
Split delete method for readability
Signed-off-by: Claudio Cambra <developer@claudiocambra.com>
1 parent 52e92f5 commit e5f7d9e

1 file changed

Lines changed: 46 additions & 37 deletions

File tree

Sources/NextcloudFileProviderKit/Item/Item+Delete.swift

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ 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
2015
func delete(
2116
trashing: Bool = false,
2217
domain: NSFileProviderDomain? = nil,
@@ -83,45 +78,59 @@ public extension Item {
8378
"""
8479
)
8580

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

0 commit comments

Comments
 (0)