1
1
import Alamofire
2
2
import Foundation
3
- import HTTPTypes
4
3
5
4
#if canImport(FoundationNetworking)
6
5
import FoundationNetworking
@@ -74,26 +73,19 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
74
73
}
75
74
76
75
private func _uploadOrUpdate(
77
- method: HTTPTypes . HTTPRequest . Method ,
76
+ method: HTTPMethod ,
78
77
path: String ,
79
78
file: FileUpload ,
80
79
options: FileOptions ?
81
80
) async throws -> FileUploadResponse {
82
81
let options = options ?? defaultFileOptions
83
- var headers = options. headers. map { HTTPFields ( $0) } ?? HTTPFields ( )
82
+ var headers = options. headers. map { HTTPHeaders ( $0) } ?? HTTPHeaders ( )
84
83
85
84
if method == . post {
86
- headers [ . xUpsert ] = " \( options. upsert) "
85
+ headers [ " x-upsert " ] = " \( options. upsert) "
87
86
}
88
87
89
- headers [ . duplex] = options. duplex
90
-
91
- #if DEBUG
92
- let formData = MultipartFormData ( boundary: testingBoundary. value)
93
- #else
94
- let formData = MultipartFormData ( )
95
- #endif
96
- file. encode ( to: formData, withPath: path, options: options)
88
+ headers [ " duplex " ] = options. duplex
97
89
98
90
struct UploadResponse : Decodable {
99
91
let Key : String
@@ -103,12 +95,15 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
103
95
let cleanPath = _removeEmptyFolders ( path)
104
96
let _path = _getFinalPath ( cleanPath)
105
97
106
- let response = try await execute (
98
+ let response = try await upload (
107
99
configuration. url. appendingPathComponent ( " object/ \( _path) " ) ,
108
- method: HTTPMethod ( rawValue: method. rawValue) ,
109
- headers: HTTPHeaders ( headers. map { HTTPHeader ( name: $0. name. rawName, value: $0. value) } ) ,
110
- body: formData. encode ( )
111
- ) . serializingDecodable ( UploadResponse . self, decoder: configuration. decoder) . value
100
+ method: method,
101
+ headers: headers
102
+ ) { formData in
103
+ file. encode ( to: formData, withPath: path, options: options)
104
+ }
105
+ . serializingDecodable ( UploadResponse . self, decoder: configuration. decoder)
106
+ . value
112
107
113
108
return FileUploadResponse (
114
109
id: response. Id,
@@ -514,15 +509,15 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
514
509
let url : String
515
510
}
516
511
517
- var headers = HTTPFields ( )
512
+ var headers = HTTPHeaders ( )
518
513
if let upsert = options? . upsert, upsert {
519
- headers [ . xUpsert ] = " true "
514
+ headers [ " x-upsert " ] = " true "
520
515
}
521
516
522
517
let response = try await execute (
523
518
configuration. url. appendingPathComponent ( " object/upload/sign/ \( bucketId) / \( path) " ) ,
524
519
method: . post,
525
- headers: HTTPHeaders ( headers. map { HTTPHeader ( name : $0 . name . rawName , value : $0 . value ) } )
520
+ headers: headers
526
521
) . serializingDecodable ( Response . self, decoder: configuration. decoder) . value
527
522
528
523
let signedURL = try makeSignedURL ( response. url, download: nil )
@@ -597,10 +592,10 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
597
592
options: FileOptions ?
598
593
) async throws -> SignedURLUploadResponse {
599
594
let options = options ?? defaultFileOptions
600
- var headers = options. headers. map { HTTPFields ( $0) } ?? HTTPFields ( )
595
+ var headers = options. headers. map { HTTPHeaders ( $0) } ?? HTTPHeaders ( )
601
596
602
- headers [ . xUpsert ] = " \( options. upsert) "
603
- headers [ . duplex] = options. duplex
597
+ headers [ " x-upsert " ] = " \( options. upsert) "
598
+ headers [ " duplex " ] = options. duplex
604
599
605
600
#if DEBUG
606
601
let formData = MultipartFormData ( boundary: testingBoundary. value)
@@ -613,14 +608,16 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
613
608
let Key : String
614
609
}
615
610
616
- let response = try await execute (
617
- configuration. url
618
- . appendingPathComponent ( " object/upload/sign/ \( bucketId) / \( path) " ) ,
611
+ let response = try await upload (
612
+ configuration. url. appendingPathComponent ( " object/upload/sign/ \( bucketId) / \( path) " ) ,
619
613
method: . put,
620
- headers: HTTPHeaders ( headers. map { HTTPHeader ( name: $0. name. rawName, value: $0. value) } ) ,
621
- query: [ " token " : token] ,
622
- body: formData. encode ( )
623
- ) . serializingDecodable ( UploadResponse . self, decoder: configuration. decoder) . value
614
+ headers: headers,
615
+ query: [ " token " : token]
616
+ ) { formData in
617
+ file. encode ( to: formData, withPath: path, options: options)
618
+ }
619
+ . serializingDecodable ( UploadResponse . self, decoder: configuration. decoder)
620
+ . value
624
621
625
622
let fullPath = response. Key
626
623
@@ -641,8 +638,3 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
641
638
return cleanedPath
642
639
}
643
640
}
644
-
645
- extension HTTPField . Name {
646
- static let duplex = Self ( " duplex " ) !
647
- static let xUpsert = Self ( " x-upsert " ) !
648
- }
0 commit comments