-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathItemMetadata.swift
More file actions
179 lines (162 loc) · 6.47 KB
/
ItemMetadata.swift
File metadata and controls
179 lines (162 loc) · 6.47 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
/*
* 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 Foundation
public enum Status: Int {
case downloadError = -4
case downloading = -3
case inDownload = -2
case normal = 0
case inUpload = 2
case uploading = 3
case uploadError = 4
}
public enum SharePermissions: Int {
case readShare = 1
case updateShare = 2
case createShare = 4
case deleteShare = 8
case shareShare = 16
case maxFileShare = 19
case maxFolderShare = 31
}
public protocol ItemMetadata: Equatable {
var ocId: String { get set }
var account: String { get set }
var checksums: String { get set }
var chunkUploadId: String? { get set }
var classFile: String { get set }
var commentsUnread: Bool { get set }
var contentType: String { get set }
var creationDate: Date { get set }
var dataFingerprint: String { get set }
var date: Date { get set }
var directory: Bool { get set }
var downloadURL: String { get set }
var e2eEncrypted: Bool { get set }
var etag: String { get set }
var favorite: Bool { get set }
var fileId: String { get set }
var fileName: String { get set } // What the file's real file name is
var fileNameView: String { get set } // What the user sees (usually same as fileName)
var hasPreview: Bool { get set }
var hidden: Bool { get set }
var iconName: String { get set }
var iconUrl: String { get set }
var mountType: String { get set }
var name: String { get set } // for unifiedSearch is the provider.id
var note: String { get set }
var ownerId: String { get set }
var ownerDisplayName: String { get set }
var livePhotoFile: String? { get set }
var lock: Bool { get set }
var lockOwner: String? { get set }
var lockOwnerEditor: String? { get set }
var lockOwnerType: Int? { get set }
var lockOwnerDisplayName: String? { get set }
var lockTime: Date? { get set } // Time the file was locked
var lockTimeOut: Date? { get set } // Time the file's lock will expire
var path: String { get set }
var permissions: String { get set }
var shareType: [Int] { get set }
var quotaUsedBytes: Int64 { get set }
var quotaAvailableBytes: Int64 { get set }
var resourceType: String { get set }
var richWorkspace: String? { get set }
var serverUrl: String { get set } // For parent folder! Build remote url by adding fileName
var session: String? { get set }
var sessionError: String? { get set }
var sessionTaskIdentifier: Int? { get set }
var sharePermissionsCollaborationServices: Int { get set }
// TODO: Find a way to compare these two below in remote state check
var sharePermissionsCloudMesh: [String] { get set }
var size: Int64 { get set }
var status: Int { get set }
var tags: [String] { get set }
var downloaded: Bool { get set }
var uploaded: Bool { get set }
var keepDownloaded: Bool { get set }
var trashbinFileName: String { get set }
var trashbinOriginalLocation: String { get set }
var trashbinDeletionTime: Date { get set }
var uploadDate: Date { get set }
var urlBase: String { get set }
var user: String { get set } // The user who owns the file (Nextcloud username)
var userId: String { get set } // The user who owns the file (backend user id)
// (relevant for alt. backends like LDAP)
}
public extension ItemMetadata {
var livePhoto: Bool {
livePhotoFile != nil && livePhotoFile?.isEmpty == false
}
var isDownloadUpload: Bool {
status == Status.inDownload.rawValue || status == Status.downloading.rawValue
|| status == Status.inUpload.rawValue || status == Status.uploading.rawValue
}
var isDownload: Bool {
status == Status.inDownload.rawValue || status == Status.downloading.rawValue
}
var isUpload: Bool {
status == Status.inUpload.rawValue || status == Status.uploading.rawValue
}
var isTrashed: Bool {
serverUrl.hasPrefix(urlBase + Account.webDavTrashUrlSuffix + userId + "/trash")
}
mutating func apply(fileName: String) {
self.fileName = fileName
fileNameView = fileName
name = fileName
}
mutating func apply(account: Account) {
self.account = account.ncKitAccount
user = account.username
userId = account.id
urlBase = account.serverUrl
}
func isInSameDatabaseStoreableRemoteState(_ comparingMetadata: any ItemMetadata)
-> Bool
{
comparingMetadata.etag == etag
&& comparingMetadata.fileNameView == fileNameView
&& comparingMetadata.date == date
&& comparingMetadata.permissions == permissions
&& comparingMetadata.hasPreview == hasPreview
&& comparingMetadata.note == note
&& comparingMetadata.lock == lock
&& comparingMetadata.sharePermissionsCollaborationServices
== sharePermissionsCollaborationServices
&& comparingMetadata.favorite == favorite
}
/// Returns false if the user is lokced out of the file. I.e. The file is locked but by someone else
func canUnlock(as user: String) -> Bool {
!lock || (lockOwner == user && lockOwnerType == 0)
}
func thumbnailUrl(size: CGSize) -> URL? {
guard hasPreview else {
return nil
}
guard #available(macOS 13.0, iOS 16.0, visionOS 1.0, *) else {
return URL(
string: "\(urlBase.urlEncoded ?? "")/index.php/core/preview?fileId=\(fileId)&x=\(size.width)&y=\(size.height)&a=true"
)
}
return URL(string: urlBase.urlEncoded ?? "")?
.appending(components: "index.php", "core", "preview")
.appending(queryItems: [
.init(name: "fileId", value: fileId),
.init(name: "x", value: "\(size.width)"),
.init(name: "y", value: "\(size.height)"),
.init(name: "a", value: "true")
])
}
}