-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathItem+Fetch.swift
More file actions
365 lines (330 loc) · 14.3 KB
/
Item+Fetch.swift
File metadata and controls
365 lines (330 loc) · 14.3 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//
// Item+Fetch.swift
//
//
// Created by Claudio Cambra on 16/4/24.
//
import FileProvider
import Foundation
import NextcloudKit
import OSLog
public extension Item {
private func fetchDirectoryContents(
itemIdentifier: NSFileProviderItemIdentifier,
directoryLocalPath: String,
directoryRemotePath: String,
domain: NSFileProviderDomain?,
progress: Progress
) async throws {
progress.totalUnitCount = 1 // Add 1 for final procedures
// Download *everything* within this directory. What we do:
// 1. Enumerate the contents of the directory
// 2. Download everything within this directory
// 3. Detect child directories
// 4. Repeat 1 -> 3 for each child directory
var remoteDirectoryPaths = [directoryRemotePath]
while !remoteDirectoryPaths.isEmpty {
let remoteDirectoryPath = remoteDirectoryPaths.removeFirst()
let (metadatas, _, _, _, readError) = await Enumerator.readServerUrl(
remoteDirectoryPath,
account: account,
remoteInterface: remoteInterface,
dbManager: dbManager
)
if let readError, readError != .success {
Self.logger.error(
"""
Could not enumerate directory contents for
\(self.metadata.fileName, privacy: .public)
at \(remoteDirectoryPath, privacy: .public)
error: \(readError.errorCode, privacy: .public)
\(readError.errorDescription, privacy: .public)
"""
)
throw readError.fileProviderError(
handlingNoSuchItemErrorUsingItemIdentifier: itemIdentifier
) ?? NSFileProviderError(.cannotSynchronize)
}
guard let metadatas else {
Self.logger.error(
"""
Could not fetch directory contents for
\(self.metadata.fileName, privacy: .public)
at \(remoteDirectoryPath, privacy: .public), received nil metadatas
"""
)
throw NSFileProviderError(.cannotSynchronize)
}
progress.totalUnitCount += Int64(metadatas.count)
for var metadata in metadatas {
let remotePath = metadata.serverUrl + "/" + metadata.fileName
let relativePath =
remotePath.replacingOccurrences(of: directoryRemotePath, with: "")
let childLocalPath = directoryLocalPath + relativePath
if metadata.directory {
remoteDirectoryPaths.append(remotePath)
try FileManager.default.createDirectory(
at: URL(fileURLWithPath: childLocalPath),
withIntermediateDirectories: true,
attributes: nil
)
} else {
let (_, _, _, _, _, _, error) = await remoteInterface.download(
remotePath: remotePath,
localPath: childLocalPath,
account: account,
options: .init(),
requestHandler: { progress.setHandlersFromAfRequest($0) },
taskHandler: { task in
if let domain {
NSFileProviderManager(for: domain)?.register(
task,
forItemWithIdentifier:
NSFileProviderItemIdentifier(metadata.ocId),
completionHandler: { _ in }
)
}
},
progressHandler: { _ in }
)
guard error == .success else {
Self.logger.error(
"""
Could not acquire contents of item: \(metadata.fileName, privacy: .public)
at \(remotePath, privacy: .public)
error: \(error.errorCode, privacy: .public)
\(error.errorDescription, privacy: .public)
"""
)
metadata.status = Status.downloadError.rawValue
metadata.sessionError = error.errorDescription
dbManager.addItemMetadata(metadata)
throw error.fileProviderError(
handlingNoSuchItemErrorUsingItemIdentifier: itemIdentifier
) ?? NSFileProviderError(.cannotSynchronize)
}
}
metadata.status = Status.normal.rawValue
metadata.downloaded = true
// HACK: We were previously failing to correctly set the uploaded state to true for
// enumerated items. Fix it now to ensure we do not show "waiting for upload" when
// having downloaded incorrectly enumerated files
metadata.uploaded = true
metadata.sessionError = ""
dbManager.addItemMetadata(metadata)
progress.completedUnitCount += 1
}
}
progress.completedUnitCount += 1 // Finish off
}
func fetchContents(
domain: NSFileProviderDomain? = nil,
progress: Progress = .init(),
dbManager: FilesDatabaseManager
) async -> (URL?, Item?, Error?) {
let ocId = itemIdentifier.rawValue
guard metadata.classFile != "lock", !isLockFileName(filename) else {
Self.logger.info(
"""
System requested fetch of lock file \(self.filename, privacy: .public)
will just provide local contents URL if possible.
"""
)
if let domain, let localUrl = await localUrlForContents(domain: domain) {
return (localUrl, self, nil)
} else if #available(macOS 13.0, *) {
Self.logger.error("Could not get local contents URL for lock file, erroring")
return (nil, self, NSFileProviderError(.excludedFromSync))
} else {
Self.logger.error("Could not get local contents URL for lock file, nilling")
return (nil, self, nil)
}
}
let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
Self.logger.debug(
"""
Fetching item with name \(self.metadata.fileName, privacy: .public)
at URL: \(serverUrlFileName, privacy: .public)
"""
)
let localPath = FileManager.default.temporaryDirectory.appendingPathComponent(metadata.ocId)
guard var updatedMetadata = dbManager.setStatusForItemMetadata(metadata, status: .downloading) else {
Self.logger.error(
"""
Could not acquire updated metadata of item \(ocId, privacy: .public),
unable to update item status to downloading
"""
)
return (
nil,
nil,
NSError.fileProviderErrorForNonExistentItem(withIdentifier: self.itemIdentifier)
)
}
let isDirectory = contentType.conforms(to: .directory)
if isDirectory {
Self.logger.debug(
"""
Item with identifier: \(ocId, privacy: .public)
and filename: \(updatedMetadata.fileName, privacy: .public)
is a directory, creating dir locally and fetching its contents
"""
)
do {
try FileManager.default.createDirectory(
at: localPath,
withIntermediateDirectories: true,
attributes: nil
)
} catch let error {
Self.logger.error(
"""
Could not create directory for item with identifier: \(ocId, privacy: .public)
and fileName: \(updatedMetadata.fileName, privacy: .public)
at \(localPath, privacy: .public)
error: \(error.localizedDescription, privacy: .public)
"""
)
updatedMetadata.status = Status.downloadError.rawValue
updatedMetadata.sessionError = error.localizedDescription
dbManager.addItemMetadata(updatedMetadata)
return (nil, nil, error)
}
do {
try await fetchDirectoryContents(
itemIdentifier: itemIdentifier,
directoryLocalPath: localPath.path,
directoryRemotePath: serverUrlFileName,
domain: domain,
progress: progress
)
} catch let error {
Self.logger.error(
"""
Could not fetch directory contents for \(ocId, privacy: .public)
and fileName: \(updatedMetadata.fileName, privacy: .public)
at \(serverUrlFileName, privacy: .public)
error: \(error.localizedDescription, privacy: .public)
"""
)
updatedMetadata.status = Status.downloadError.rawValue
updatedMetadata.sessionError = error.localizedDescription
dbManager.addItemMetadata(updatedMetadata)
return (nil, nil, error)
}
} else {
let (_, _, _, _, _, _, error) = await remoteInterface.download(
remotePath: serverUrlFileName,
localPath: localPath.path,
account: account,
options: .init(),
requestHandler: { _ in },
taskHandler: { _ in },
progressHandler: { _ in }
)
if error != .success {
Self.logger.error(
"""
Could not acquire contents of item with identifier: \(ocId, privacy: .public)
and fileName: \(updatedMetadata.fileName, privacy: .public)
at \(serverUrlFileName, privacy: .public)
error: \(error.errorCode, privacy: .public)
\(error.errorDescription, privacy: .public)
"""
)
updatedMetadata.status = Status.downloadError.rawValue
updatedMetadata.sessionError = error.errorDescription
dbManager.addItemMetadata(updatedMetadata)
return (nil, nil, error.fileProviderError(
handlingNoSuchItemErrorUsingItemIdentifier: itemIdentifier
))
}
}
Self.logger.debug(
"""
Acquired contents of item with identifier: \(ocId, privacy: .public)
and filename: \(updatedMetadata.fileName, privacy: .public)
"""
)
updatedMetadata.status = Status.normal.rawValue
updatedMetadata.downloaded = true
// HACK: We were previously failing to correctly set the uploaded state to true for
// enumerated items. Fix it now to ensure we do not show "waiting for upload" when
// having downloaded incorrectly enumerated files
updatedMetadata.uploaded = true
updatedMetadata.sessionError = ""
dbManager.addItemMetadata(updatedMetadata)
guard let parentItemIdentifier = await dbManager.parentItemIdentifierWithRemoteFallback(
fromMetadata: metadata,
remoteInterface: remoteInterface,
account: account
) else {
Self.logger.error(
"""
Could not find parent item id for file \(self.metadata.fileName, privacy: .public)
"""
)
return (
nil,
nil,
NSError.fileProviderErrorForNonExistentItem(withIdentifier: self.itemIdentifier)
)
}
let fpItem = Item(
metadata: updatedMetadata,
parentItemIdentifier: parentItemIdentifier,
account: account,
remoteInterface: remoteInterface,
dbManager: dbManager
)
return (localPath, fpItem, nil)
}
func fetchThumbnail(
size: CGSize, domain: NSFileProviderDomain? = nil
) async -> (Data?, Error?) {
guard let thumbnailUrl = metadata.thumbnailUrl(size: size) else {
Self.logger.debug(
"""
Unknown thumbnail URL for: \(self.itemIdentifier.rawValue, privacy: .public)
fileName: \(self.filename, privacy: .public)
"""
)
return (
nil,
NSError.fileProviderErrorForNonExistentItem(withIdentifier: self.itemIdentifier)
)
}
Self.logger.debug(
"""
Fetching thumbnail for: \(self.filename, privacy: .public)
at (\(thumbnailUrl, privacy: .public))
"""
)
let (_, data, error) = await remoteInterface.downloadThumbnail(
url: thumbnailUrl, account: account, options: .init(), taskHandler: { task in
if let domain {
NSFileProviderManager(for: domain)?.register(
task,
forItemWithIdentifier: self.itemIdentifier,
completionHandler: { _ in }
)
}
}
)
if error != .success {
Self.logger.error(
"""
Could not acquire thumbnail for item with identifier:
\(self.itemIdentifier.rawValue, privacy: .public)
and fileName: \(self.filename, privacy: .public)
at \(thumbnailUrl, privacy: .public)
error: \(error.errorCode, privacy: .public)
\(error.errorDescription, privacy: .public)
"""
)
}
return (data, error.fileProviderError(
handlingNoSuchItemErrorUsingItemIdentifier: itemIdentifier
))
}
}