-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathStorage.swift
More file actions
377 lines (341 loc) 路 12.3 KB
/
Copy pathStorage.swift
File metadata and controls
377 lines (341 loc) 路 12.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
366
367
368
369
370
371
372
373
374
375
376
377
import AsyncHTTPClient
import Foundation
import NIO
import JSONCodable
import AppwriteEnums
import AppwriteModels
/// The Storage service allows you to manage your project files.
open class Storage: Service {
///
/// Get a list of all the user files. You can use the query params to filter
/// your results.
///
/// - Parameters:
/// - bucketId: String
/// - queries: [String] (optional)
/// - search: String (optional)
/// - total: Bool (optional)
/// - Throws: Exception if the request fails
/// - Returns: AppwriteModels.FileList
///
open func listFiles(
bucketId: String,
queries: [String]? = nil,
search: String? = nil,
total: Bool? = nil
) async throws -> AppwriteModels.FileList {
let apiPath: String = "/storage/buckets/{bucketId}/files"
.replacingOccurrences(of: "{bucketId}", with: bucketId)
let apiParams: [String: Any?] = [
"queries": queries,
"search": search,
"total": total
]
let apiHeaders: [String: String] = [
"X-Appwrite-Project": client.config["project"] ?? "",
"accept": "application/json"
]
let converter: (Any) throws -> AppwriteModels.FileList = { response in
return AppwriteModels.FileList.from(map: response as! [String: Any])
}
return try await client.call(
method: "GET",
path: apiPath,
headers: apiHeaders,
params: apiParams,
converter: converter
)
}
///
/// Create a new file. Before using this route, you should create a new bucket
/// resource using either a [server
/// integration](https://appwrite.io/docs/server/storage#storageCreateBucket)
/// API or directly from your Appwrite console.
///
/// Larger files should be uploaded using multiple requests with the
/// [content-range](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range)
/// header to send a partial request with a maximum supported chunk of `5MB`.
/// The `content-range` header values should always be in bytes.
///
/// When the first request is sent, the server will return the **File** object,
/// and the subsequent part request must include the file's **id** in
/// `x-appwrite-id` header to allow the server to know that the partial upload
/// is for the existing file and not for a new one.
///
/// If you're creating a new file using one of the Appwrite SDKs, all the
/// chunking logic will be managed by the SDK internally.
///
///
/// - Parameters:
/// - bucketId: String
/// - fileId: String
/// - file: InputFile
/// - permissions: [String] (optional)
/// - Throws: Exception if the request fails
/// - Returns: AppwriteModels.File
///
open func createFile(
bucketId: String,
fileId: String,
file: InputFile,
permissions: [String]? = nil,
onProgress: ((UploadProgress) -> Void)? = nil
) async throws -> AppwriteModels.File {
let apiPath: String = "/storage/buckets/{bucketId}/files"
.replacingOccurrences(of: "{bucketId}", with: bucketId)
var apiParams: [String: Any?] = [
"fileId": fileId,
"file": file,
"permissions": permissions
]
var apiHeaders: [String: String] = [
"X-Appwrite-Project": client.config["project"] ?? "",
"content-type": "multipart/form-data",
"accept": "application/json"
]
let converter: (Any) throws -> AppwriteModels.File = { response in
return AppwriteModels.File.from(map: response as! [String: Any])
}
let idParamName: String? = "fileId"
let paramName = "file"
return try await client.chunkedUpload(
path: apiPath,
headers: &apiHeaders,
params: &apiParams,
paramName: paramName,
idParamName: idParamName,
converter: converter,
onProgress: onProgress
)
}
///
/// Get a file by its unique ID. This endpoint response returns a JSON object
/// with the file metadata.
///
/// - Parameters:
/// - bucketId: String
/// - fileId: String
/// - Throws: Exception if the request fails
/// - Returns: AppwriteModels.File
///
open func getFile(
bucketId: String,
fileId: String
) async throws -> AppwriteModels.File {
let apiPath: String = "/storage/buckets/{bucketId}/files/{fileId}"
.replacingOccurrences(of: "{bucketId}", with: bucketId)
.replacingOccurrences(of: "{fileId}", with: fileId)
let apiParams: [String: Any] = [:]
let apiHeaders: [String: String] = [
"X-Appwrite-Project": client.config["project"] ?? "",
"accept": "application/json"
]
let converter: (Any) throws -> AppwriteModels.File = { response in
return AppwriteModels.File.from(map: response as! [String: Any])
}
return try await client.call(
method: "GET",
path: apiPath,
headers: apiHeaders,
params: apiParams,
converter: converter
)
}
///
/// Update a file by its unique ID. Only users with write permissions have
/// access to update this resource.
///
/// - Parameters:
/// - bucketId: String
/// - fileId: String
/// - name: String (optional)
/// - permissions: [String] (optional)
/// - Throws: Exception if the request fails
/// - Returns: AppwriteModels.File
///
open func updateFile(
bucketId: String,
fileId: String,
name: String? = nil,
permissions: [String]? = nil
) async throws -> AppwriteModels.File {
let apiPath: String = "/storage/buckets/{bucketId}/files/{fileId}"
.replacingOccurrences(of: "{bucketId}", with: bucketId)
.replacingOccurrences(of: "{fileId}", with: fileId)
let apiParams: [String: Any?] = [
"name": name,
"permissions": permissions
]
let apiHeaders: [String: String] = [
"X-Appwrite-Project": client.config["project"] ?? "",
"content-type": "application/json",
"accept": "application/json"
]
let converter: (Any) throws -> AppwriteModels.File = { response in
return AppwriteModels.File.from(map: response as! [String: Any])
}
return try await client.call(
method: "PUT",
path: apiPath,
headers: apiHeaders,
params: apiParams,
converter: converter
)
}
///
/// Delete a file by its unique ID. Only users with write permissions have
/// access to delete this resource.
///
/// - Parameters:
/// - bucketId: String
/// - fileId: String
/// - Throws: Exception if the request fails
/// - Returns: Any
///
open func deleteFile(
bucketId: String,
fileId: String
) async throws -> Any {
let apiPath: String = "/storage/buckets/{bucketId}/files/{fileId}"
.replacingOccurrences(of: "{bucketId}", with: bucketId)
.replacingOccurrences(of: "{fileId}", with: fileId)
let apiParams: [String: Any] = [:]
let apiHeaders: [String: String] = [
"X-Appwrite-Project": client.config["project"] ?? "",
"content-type": "application/json"
]
return try await client.call(
method: "DELETE",
path: apiPath,
headers: apiHeaders,
params: apiParams )
}
///
/// Get a file content by its unique ID. The endpoint response return with a
/// 'Content-Disposition: attachment' header that tells the browser to start
/// downloading the file to user downloads directory.
///
/// - Parameters:
/// - bucketId: String
/// - fileId: String
/// - token: String (optional)
/// - Throws: Exception if the request fails
/// - Returns: ByteBuffer
///
open func getFileDownload(
bucketId: String,
fileId: String,
token: String? = nil
) async throws -> ByteBuffer {
let apiPath: String = "/storage/buckets/{bucketId}/files/{fileId}/download"
.replacingOccurrences(of: "{bucketId}", with: bucketId)
.replacingOccurrences(of: "{fileId}", with: fileId)
let apiParams: [String: Any?] = [
"token": token,
"project": client.config["project"],
"impersonateuserid": client.config["impersonateuserid"]
]
return try await client.call(
method: "GET",
path: apiPath,
params: apiParams
)
}
///
/// Get a file preview image. Currently, this method supports preview for image
/// files (jpg, png, and gif), other supported formats, like pdf, docs, slides,
/// and spreadsheets, will return the file icon image. You can also pass query
/// string arguments for cutting and resizing your preview image. Preview is
/// supported only for image files smaller than 10MB.
///
/// - Parameters:
/// - bucketId: String
/// - fileId: String
/// - width: Int (optional)
/// - height: Int (optional)
/// - gravity: AppwriteEnums.ImageGravity (optional)
/// - quality: Int (optional)
/// - borderWidth: Int (optional)
/// - borderColor: String (optional)
/// - borderRadius: Int (optional)
/// - opacity: Double (optional)
/// - rotation: Int (optional)
/// - background: String (optional)
/// - output: AppwriteEnums.ImageFormat (optional)
/// - token: String (optional)
/// - Throws: Exception if the request fails
/// - Returns: ByteBuffer
///
open func getFilePreview(
bucketId: String,
fileId: String,
width: Int? = nil,
height: Int? = nil,
gravity: AppwriteEnums.ImageGravity? = nil,
quality: Int? = nil,
borderWidth: Int? = nil,
borderColor: String? = nil,
borderRadius: Int? = nil,
opacity: Double? = nil,
rotation: Int? = nil,
background: String? = nil,
output: AppwriteEnums.ImageFormat? = nil,
token: String? = nil
) async throws -> ByteBuffer {
let apiPath: String = "/storage/buckets/{bucketId}/files/{fileId}/preview"
.replacingOccurrences(of: "{bucketId}", with: bucketId)
.replacingOccurrences(of: "{fileId}", with: fileId)
let apiParams: [String: Any?] = [
"width": width,
"height": height,
"gravity": gravity?.rawValue,
"quality": quality,
"borderWidth": borderWidth,
"borderColor": borderColor,
"borderRadius": borderRadius,
"opacity": opacity,
"rotation": rotation,
"background": background,
"output": output?.rawValue,
"token": token,
"project": client.config["project"],
"impersonateuserid": client.config["impersonateuserid"]
]
return try await client.call(
method: "GET",
path: apiPath,
params: apiParams
)
}
///
/// Get a file content by its unique ID. This endpoint is similar to the
/// download method but returns with no 'Content-Disposition: attachment'
/// header.
///
/// - Parameters:
/// - bucketId: String
/// - fileId: String
/// - token: String (optional)
/// - Throws: Exception if the request fails
/// - Returns: ByteBuffer
///
open func getFileView(
bucketId: String,
fileId: String,
token: String? = nil
) async throws -> ByteBuffer {
let apiPath: String = "/storage/buckets/{bucketId}/files/{fileId}/view"
.replacingOccurrences(of: "{bucketId}", with: bucketId)
.replacingOccurrences(of: "{fileId}", with: fileId)
let apiParams: [String: Any?] = [
"token": token,
"project": client.config["project"],
"impersonateuserid": client.config["impersonateuserid"]
]
return try await client.call(
method: "GET",
path: apiPath,
params: apiParams
)
}
}