Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Sources/Storage/StorageFileApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ enum FileUpload {
}

switch self {
case let .data(data):
case .data(let data):
formData.append(
data,
withName: "",
fileName: path.fileName,
mimeType: options.contentType ?? mimeType(forPathExtension: path.pathExtension)
)

case let .url(url):
case .url(let url):
formData.append(url, withName: "")
}
}
Expand Down Expand Up @@ -422,15 +422,22 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
/// - Parameters:
/// - path: The file path to be downloaded, including the path and file name. For example `folder/image.png`.
/// - options: Transform the asset before serving it to the client.
/// - additionalQueryItems: Additional query items to be added to the request.
/// - Returns: The data of the downloaded file.
@discardableResult
public func download(
path: String,
options: TransformOptions? = nil
options: TransformOptions? = nil,
query additionalQueryItems: [URLQueryItem]? = nil
) async throws -> Data {
let queryItems = options?.queryItems ?? []
var queryItems = options?.queryItems ?? []
let renderPath = options != nil ? "render/image/authenticated" : "object"
let _path = _getFinalPath(path)

if let additionalQueryItems {
queryItems.append(contentsOf: additionalQueryItems)
}

return try await execute(
HTTPRequest(
url: configuration.url
Expand Down
28 changes: 28 additions & 0 deletions Tests/StorageTests/StorageFileAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,34 @@ final class StorageFileAPITests: XCTestCase {
XCTAssertEqual(data, Data("hello world".utf8))
}

func testDownloadWithAdditionalQuery() async throws {
Mock(
url: url.appendingPathComponent("object/bucket/file.txt"),
ignoreQuery: true,
statusCode: 200,
data: [
.get: Data("hello world".utf8)
]
)
.snapshotRequest {
#"""
curl \
--header "X-Client-Info: storage-swift/0.0.0" \
--header "apikey: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0" \
"http://localhost:54321/storage/v1/object/bucket/file.txt?version=1"
"""#
}
.register()

let data = try await storage.from("bucket")
.download(
path: "file.txt",
query: [URLQueryItem(name: "version", value: "1")]
)

XCTAssertEqual(data, Data("hello world".utf8))
}

func testDownload_withOptions() async throws {
let imageData = try! Data(
contentsOf: Bundle.module.url(forResource: "sadcat", withExtension: "jpg")!)
Expand Down
Loading