@@ -24,10 +24,6 @@ public class StorageFileApi: StorageApi {
24
24
super. init ( configuration: configuration)
25
25
}
26
26
27
- private struct UploadResponse : Decodable {
28
- let Key : String
29
- }
30
-
31
27
private struct MoveResponse : Decodable {
32
28
let message : String
33
29
}
@@ -41,7 +37,7 @@ public class StorageFileApi: StorageApi {
41
37
path: String ,
42
38
file: Data ,
43
39
options: FileOptions
44
- ) async throws -> String {
40
+ ) async throws -> FileUploadResponse {
45
41
let contentType = options. contentType
46
42
var headers = HTTPHeaders ( [
47
43
" x-upsert " : " \( options. upsert) " ,
@@ -56,7 +52,12 @@ public class StorageFileApi: StorageApi {
56
52
file: File ( name: fileName, data: file, fileName: fileName, contentType: contentType)
57
53
)
58
54
59
- return try await execute (
55
+ struct UploadResponse : Decodable {
56
+ let Key : String
57
+ let Id : String
58
+ }
59
+
60
+ let response = try await execute (
60
61
HTTPRequest (
61
62
url: configuration. url. appendingPathComponent ( " object/ \( bucketId) / \( path) " ) ,
62
63
method: method,
@@ -66,7 +67,13 @@ public class StorageFileApi: StorageApi {
66
67
headers: headers
67
68
)
68
69
)
69
- . decoded ( as: UploadResponse . self, decoder: configuration. decoder) . Key
70
+ . decoded ( as: UploadResponse . self, decoder: configuration. decoder)
71
+
72
+ return FileUploadResponse (
73
+ id: response. Id,
74
+ path: path,
75
+ fullPath: response. Key
76
+ )
70
77
}
71
78
72
79
/// Uploads a file to an existing bucket.
@@ -80,7 +87,7 @@ public class StorageFileApi: StorageApi {
80
87
path: String ,
81
88
file: Data ,
82
89
options: FileOptions = FileOptions ( )
83
- ) async throws -> String {
90
+ ) async throws -> FileUploadResponse {
84
91
try await uploadOrUpdate ( method: . post, path: path, file: file, options: options)
85
92
}
86
93
@@ -95,16 +102,20 @@ public class StorageFileApi: StorageApi {
95
102
path: String ,
96
103
file: Data ,
97
104
options: FileOptions = FileOptions ( )
98
- ) async throws -> String {
105
+ ) async throws -> FileUploadResponse {
99
106
try await uploadOrUpdate ( method: . put, path: path, file: file, options: options)
100
107
}
101
108
102
109
/// Moves an existing file, optionally renaming it at the same time.
103
110
/// - Parameters:
104
- /// - from: The original file path, including the current file name. For example
105
- /// `folder/image.png`.
106
- /// - to: The new file path, including the new file name. For example `folder/image-copy.png`.
107
- public func move( from source: String , to destination: String ) async throws {
111
+ /// - source: The original file path, including the current file name. For example `folder/image.png`.
112
+ /// - destination: The new file path, including the new file name. For example `folder/image-copy.png`.
113
+ /// - options: The destination options.
114
+ public func move(
115
+ from source: String ,
116
+ to destination: String ,
117
+ options: DestinationOptions ? = nil
118
+ ) async throws {
108
119
try await execute (
109
120
HTTPRequest (
110
121
url: configuration. url. appendingPathComponent ( " object/move " ) ,
@@ -114,6 +125,7 @@ public class StorageFileApi: StorageApi {
114
125
" bucketId " : bucketId,
115
126
" sourceKey " : source,
116
127
" destinationKey " : destination,
128
+ " destinationBucket " : options? . destinationBucket
117
129
]
118
130
)
119
131
)
@@ -122,12 +134,20 @@ public class StorageFileApi: StorageApi {
122
134
123
135
/// Copies an existing file to a new path in the same bucket.
124
136
/// - Parameters:
125
- /// - from : The original file path, including the current file name. For example
126
- /// `folder/image.png`.
127
- /// - to : The new file path, including the new file name. For example `folder/image-copy.png` .
137
+ /// - source : The original file path, including the current file name. For example `folder/image.png`.
138
+ /// - destination: The new file path, including the new file name. For example `folder/image-copy .png`.
139
+ /// - options : The destination options .
128
140
@discardableResult
129
- public func copy( from source: String , to destination: String ) async throws -> String {
130
- try await execute (
141
+ public func copy(
142
+ from source: String ,
143
+ to destination: String ,
144
+ options: DestinationOptions ? = nil
145
+ ) async throws -> String {
146
+ struct UploadResponse : Decodable {
147
+ let Key : String
148
+ }
149
+
150
+ return try await execute (
131
151
HTTPRequest (
132
152
url: configuration. url. appendingPathComponent ( " object/copy " ) ,
133
153
method: . post,
@@ -136,6 +156,7 @@ public class StorageFileApi: StorageApi {
136
156
" bucketId " : bucketId,
137
157
" sourceKey " : source,
138
158
" destinationKey " : destination,
159
+ " destinationBucket " : options? . destinationBucket
139
160
]
140
161
)
141
162
)
@@ -393,15 +414,24 @@ public class StorageFileApi: StorageApi {
393
414
///
394
415
/// - Note: Signed upload URLs can be used to upload files to the bucket without further
395
416
/// authentication. They are valid for 2 hours.
396
- public func createSignedUploadURL( path: String ) async throws -> SignedUploadURL {
417
+ public func createSignedUploadURL(
418
+ path: String ,
419
+ options: CreateSignedUploadURLOptions ? = nil
420
+ ) async throws -> SignedUploadURL {
397
421
struct Response : Decodable {
398
422
let url : URL
399
423
}
400
424
425
+ var headers = HTTPHeaders ( )
426
+ if let upsert = options? . upsert, upsert {
427
+ headers [ " x-upsert " ] = " true "
428
+ }
429
+
401
430
let response = try await execute (
402
431
HTTPRequest (
403
432
url: configuration. url. appendingPathComponent ( " object/upload/sign/ \( bucketId) / \( path) " ) ,
404
- method: . post
433
+ method: . post,
434
+ headers: headers
405
435
)
406
436
)
407
437
. decoded ( as: Response . self, decoder: configuration. decoder)
@@ -441,7 +471,7 @@ public class StorageFileApi: StorageApi {
441
471
token: String ,
442
472
file: Data ,
443
473
options: FileOptions = FileOptions ( )
444
- ) async throws -> String {
474
+ ) async throws -> SignedURLUploadResponse {
445
475
let contentType = options. contentType
446
476
var headers = HTTPHeaders ( [
447
477
" x-upsert " : " \( options. upsert) " ,
@@ -458,7 +488,11 @@ public class StorageFileApi: StorageApi {
458
488
contentType: contentType
459
489
) )
460
490
461
- return try await execute (
491
+ struct UploadResponse : Decodable {
492
+ let Key : String
493
+ }
494
+
495
+ let fullPath = try await execute (
462
496
HTTPRequest (
463
497
url: configuration. url
464
498
. appendingPathComponent ( " object/upload/sign/ \( bucketId) / \( path) " ) ,
@@ -471,6 +505,8 @@ public class StorageFileApi: StorageApi {
471
505
)
472
506
. decoded ( as: UploadResponse . self, decoder: configuration. decoder)
473
507
. Key
508
+
509
+ return SignedURLUploadResponse ( path: path, fullPath: fullPath)
474
510
}
475
511
}
476
512
0 commit comments