Skip to content

Commit

Permalink
support additional headers
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Aug 26, 2024
1 parent 3a389c3 commit 0414108
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
9 changes: 9 additions & 0 deletions .swiftpm/configuration/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"pins" : [
{
"identity" : "multipartformdata",
"kind" : "remoteSourceControl",
"location" : "https://github.com/grdsdev/MultipartFormData",
"state" : {
"revision" : "ed7abea9cfc6c3b5e77a73fe6842c57a372d2017",
"version" : "0.1.0"
}
},
{
"identity" : "swift-concurrency-extras",
"kind" : "remoteSourceControl",
Expand Down
20 changes: 10 additions & 10 deletions Sources/Storage/StorageFileApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
return (try? encoder.encode(metadata)) ?? "{}".data(using: .utf8)!
}

func uploadOrUpdate(
private func _uploadOrUpdate(
method: HTTPMethod,
path: String,
formData: MultipartFormData,
options: FileOptions?
) async throws -> FileUploadResponse {
var headers = HTTPHeaders()

let options = options ?? defaultFileOptions
var headers = options.headers.map { HTTPHeaders($0) } ?? HTTPHeaders()

let metadata = options.metadata

if method == .post {
Expand Down Expand Up @@ -114,7 +114,7 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
fileName: fileName,
mimeType: options.contentType ?? mimeType(forPathExtension: path.pathExtension)
)
return try await uploadOrUpdate(method: .post, path: path, formData: formData, options: options)
return try await _uploadOrUpdate(method: .post, path: path, formData: formData, options: options)
}

@discardableResult
Expand All @@ -126,7 +126,7 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
let fileName = path.fileName
let formData = MultipartFormData()
formData.append(fileURL, withName: fileName, fileName: fileName)
return try await uploadOrUpdate(method: .post, path: path, formData: formData, options: options)
return try await _uploadOrUpdate(method: .post, path: path, formData: formData, options: options)
}

/// Replaces an existing file at the specified path with a new one.
Expand All @@ -149,7 +149,7 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
fileName: fileName,
mimeType: options.contentType ?? mimeType(forPathExtension: path.pathExtension)
)
return try await uploadOrUpdate(method: .put, path: path, formData: formData, options: options)
return try await _uploadOrUpdate(method: .put, path: path, formData: formData, options: options)
}

/// Replaces an existing file at the specified path with a new one.
Expand All @@ -166,7 +166,7 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
) async throws -> FileUploadResponse {
let formData = MultipartFormData()
formData.append(fileURL, withName: path.fileName)
return try await uploadOrUpdate(method: .put, path: path, formData: formData, options: options)
return try await _uploadOrUpdate(method: .put, path: path, formData: formData, options: options)
}

/// Moves an existing file, optionally renaming it at the same time.
Expand Down Expand Up @@ -621,9 +621,9 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
options: FileOptions?
) async throws -> SignedURLUploadResponse {
let options = options ?? defaultFileOptions
var headers = HTTPHeaders([
"x-upsert": "\(options.upsert)",
])
var headers = options.headers.map { HTTPHeaders($0) } ?? HTTPHeaders()

headers["x-upsert"] = "\(options.upsert)"
headers["duplex"] = options.duplex

if let metadata = options.metadata {
Expand Down

0 comments on commit 0414108

Please sign in to comment.