-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathDownloadTaskDBManager.swift
More file actions
47 lines (41 loc) · 1.59 KB
/
Copy pathDownloadTaskDBManager.swift
File metadata and controls
47 lines (41 loc) · 1.59 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
//
// DownloadTaskDBManager.swift
// CryptomatorFileProvider
//
// Created by Philipp Schmid on 19.10.21.
// Copyright © 2021 Skymatic GmbH. All rights reserved.
//
import CryptomatorCloudAccessCore
import Foundation
import GRDB
protocol DownloadTaskManager {
func createTask(for item: ItemMetadata, replaceExisting: Bool, localURL: URL, onURLSessionTaskCreation: URLSessionTaskCreationClosure?) throws -> DownloadTask
func removeTaskRecord(_ task: DownloadTaskRecord) throws
}
class DownloadTaskDBManager: DownloadTaskManager {
private let database: DatabaseWriter
private let itemMetadataManager: ItemMetadataManager
init(database: DatabaseWriter, itemMetadataManager: ItemMetadataManager) throws {
self.database = database
self.itemMetadataManager = itemMetadataManager
_ = try database.write { db in
try DownloadTaskRecord.deleteAll(db)
}
}
func createTask(for item: ItemMetadata, replaceExisting: Bool, localURL: URL, onURLSessionTaskCreation: URLSessionTaskCreationClosure?) throws -> DownloadTask {
guard let id = item.id else {
throw DBManagerError.nonSavedItemMetadata
}
let cloudPath = try itemMetadataManager.getCloudPath(for: id)
return try database.write { db in
let taskRecord = DownloadTaskRecord(correspondingItem: id, replaceExisting: replaceExisting, localURL: localURL)
try taskRecord.save(db)
return DownloadTask(taskRecord: taskRecord, itemMetadata: item, cloudPath: cloudPath, onURLSessionTaskCreation: onURLSessionTaskCreation)
}
}
func removeTaskRecord(_ task: DownloadTaskRecord) throws {
_ = try database.write { db in
try task.delete(db)
}
}
}