-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFilesDatabaseManager+Directories.swift
More file actions
211 lines (189 loc) · 7.76 KB
/
Copy pathFilesDatabaseManager+Directories.swift
File metadata and controls
211 lines (189 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Copyright (C) 2023 by Claudio Cambra <claudio.cambra@nextcloud.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
import FileProvider
import Foundation
import OSLog
import RealmSwift
extension FilesDatabaseManager {
private func fullServerPathUrl(for metadata: any ItemMetadata) -> String {
if metadata.ocId == NSFileProviderItemIdentifier.rootContainer.rawValue {
metadata.serverUrl
} else {
metadata.serverUrl + "/" + metadata.fileName
}
}
public func childItems(directoryMetadata: SendableItemMetadata) -> [SendableItemMetadata] {
let directoryServerUrl = fullServerPathUrl(for: directoryMetadata)
return itemMetadatas
.where({ $0.serverUrl.starts(with: directoryServerUrl) })
.toUnmanagedResults()
}
public func childItemCount(directoryMetadata: SendableItemMetadata) -> Int {
let directoryServerUrl = fullServerPathUrl(for: directoryMetadata)
return itemMetadatas
.where({ $0.serverUrl.starts(with: directoryServerUrl) })
.count
}
public func parentDirectoryMetadataForItem(
_ itemMetadata: SendableItemMetadata
) -> SendableItemMetadata? {
self.itemMetadata(
account: itemMetadata.account, locatedAtRemoteUrl: itemMetadata.serverUrl
)
}
public func directoryMetadata(ocId: String) -> SendableItemMetadata? {
if let metadata = itemMetadatas.where({ $0.ocId == ocId && $0.directory }).first {
return SendableItemMetadata(value: metadata)
}
return nil
}
// Deletes all metadatas related to the info of the directory provided
public func deleteDirectoryAndSubdirectoriesMetadata(
ocId: String
) -> [SendableItemMetadata]? {
guard let directoryMetadata = itemMetadatas
.where({ $0.ocId == ocId && $0.directory })
.first
else {
Self.logger.error(
"""
Could not find directory metadata for ocId \(ocId, privacy: .public).
Not proceeding with deletion
"""
)
return nil
}
let directoryMetadataCopy = SendableItemMetadata(value: directoryMetadata)
let directoryOcId = directoryMetadata.ocId
let directoryUrlPath = directoryMetadata.serverUrl + "/" + directoryMetadata.fileName
let directoryAccount = directoryMetadata.account
let directoryEtag = directoryMetadata.etag
Self.logger.debug(
"""
Deleting root directory metadata in recursive delete.
ocID: \(directoryMetadata.ocId, privacy: .public)
etag: \(directoryEtag, privacy: .public)
serverUrl: \(directoryUrlPath, privacy: .public)
"""
)
let database = ncDatabase()
do {
try database.write { directoryMetadata.deleted = true }
} catch let error {
Self.logger.error(
"""
Failure to delete root directory metadata in recursive delete.
Received error: \(error.localizedDescription)
ocID: \(directoryOcId, privacy: .public),
etag: \(directoryEtag, privacy: .public),
serverUrl: \(directoryUrlPath, privacy: .public)
"""
)
return nil
}
var deletedMetadatas: [SendableItemMetadata] = [directoryMetadataCopy]
let results = itemMetadatas.where {
$0.account == directoryAccount && $0.serverUrl.starts(with: directoryUrlPath)
}
for result in results {
let inactiveItemMetadata = SendableItemMetadata(value: result)
do {
try database.write { result.deleted = true }
deletedMetadatas.append(inactiveItemMetadata)
} catch let error {
Self.logger.error(
"""
Failure to delete directory metadata child in recursive delete.
Received error: \(error.localizedDescription)
ocID: \(directoryOcId, privacy: .public),
etag: \(directoryEtag, privacy: .public),
serverUrl: \(directoryUrlPath, privacy: .public)
"""
)
}
}
Self.logger.debug(
"""
Completed deletions in directory recursive delete.
ocID: \(directoryOcId, privacy: .public),
etag: \(directoryEtag, privacy: .public),
serverUrl: \(directoryUrlPath, privacy: .public)
"""
)
return deletedMetadatas
}
public func renameDirectoryAndPropagateToChildren(
ocId: String, newServerUrl: String, newFileName: String
) -> [SendableItemMetadata]? {
guard let directoryMetadata = itemMetadatas
.where({ $0.ocId == ocId && $0.directory })
.first
else {
Self.logger.error(
"""
Could not find a directory with ocID \(ocId, privacy: .public)
cannot proceed with recursive renaming
"""
)
return nil
}
let oldItemServerUrl = directoryMetadata.serverUrl
let oldItemFilename = directoryMetadata.fileName
let oldDirectoryServerUrl = oldItemServerUrl + "/" + oldItemFilename
let newDirectoryServerUrl = newServerUrl + "/" + newFileName
let childItemResults = itemMetadatas.where {
$0.account == directoryMetadata.account &&
$0.serverUrl.starts(with: oldDirectoryServerUrl)
}
renameItemMetadata(ocId: ocId, newServerUrl: newServerUrl, newFileName: newFileName)
Self.logger.debug(
"""
Renamed root renaming directory from: \(oldDirectoryServerUrl, privacy: .public)
to: \(newDirectoryServerUrl, privacy: .public)
"""
)
do {
let database = ncDatabase()
try database.write {
for childItem in childItemResults {
let oldServerUrl = childItem.serverUrl
let movedServerUrl = oldServerUrl.replacingOccurrences(
of: oldDirectoryServerUrl, with: newDirectoryServerUrl)
childItem.serverUrl = movedServerUrl
database.add(childItem, update: .all)
Self.logger.debug(
"""
Moved childItem at: \(oldServerUrl, privacy: .public)
to: \(movedServerUrl, privacy: .public)
""")
}
}
} catch {
Self.logger.error(
"""
Could not rename directory metadata with ocId: \(ocId, privacy: .public)
to new serverUrl: \(newServerUrl)
received error: \(error.localizedDescription, privacy: .public)
"""
)
return nil
}
return itemMetadatas
.where {
$0.account == directoryMetadata.account &&
$0.serverUrl.starts(with: newDirectoryServerUrl)
}
.toUnmanagedResults()
}
}