Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
zunda-pixel committed Sep 8, 2024
1 parent 55fe5d0 commit 6ded734
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 42 deletions.
9 changes: 5 additions & 4 deletions Sources/Storage/ContentUrl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ extension Storage {
let filePath = self.percentEncode(filePath)
let queries: [String: String] = [
"alt": "media",
"token": downloadTokens
"token": downloadTokens,
]
let path = "v0/b/\(bucket)/o/\(filePath)?\(queries.map { [$0.key, $0.value].joined(separator: "=") }.joined(separator: "&"))"

let path =
"v0/b/\(bucket)/o/\(filePath)?\(queries.map { [$0.key, $0.value].joined(separator: "=") }.joined(separator: "&"))"

return URL(string: "\(baseUrl.absoluteString)\(path)")!
}

public func contentUrl(item: Item) -> URL {
return self.contentUrl(
bucket: item.bucket,
Expand Down
4 changes: 2 additions & 2 deletions Sources/Storage/Delete.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ extension Storage {
method: .delete,
url: endpoint
)

let (data, response) = try await httpClient.execute(for: request, from: nil)

if response.status.code != 204 {
let response = try JSONDecoder().decode(ErrorsResponse.self, from: data)
throw response.error
Expand Down
4 changes: 2 additions & 2 deletions Sources/Storage/GetItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ extension Storage {
method: .get,
url: endpoint
)

let (data, _) = try await httpClient.execute(for: request, from: nil)

let item = try JSONDecoder().decode(Item.self, from: data)

return item
}
}
15 changes: 8 additions & 7 deletions Sources/Storage/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct Item: Codable, Hashable, Sendable {
public var etag: String
public var downloadTokens: String
public var customMetadata: [String: String]?

private enum CodingKeys: String, CodingKey {
case name
case bucket
Expand Down Expand Up @@ -51,18 +51,18 @@ public struct Item: Codable, Hashable, Sendable {
self.bucket = try container.decode(String.self, forKey: .bucket)
let generationString = try container.decode(String.self, forKey: .generation)
let generation = TimeInterval(generationString)!
self.generation = Date(timeIntervalSince1970: generation / 1000000)
self.generation = Date(timeIntervalSince1970: generation / 1_000_000)
let metagenerationString = try container.decode(String.self, forKey: .metageneration)
self.metageneration = Int(metagenerationString)!

self.contentType = try container.decode(String.self, forKey: .contentType)

let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions.insert(.withFractionalSeconds)

let timeCreated = try container.decode(String.self, forKey: .timeCreated)
self.timeCreated = dateFormatter.date(from: timeCreated)!

let updated = try container.decode(String.self, forKey: .updated)
self.updated = dateFormatter.date(from: updated)!

Expand All @@ -79,6 +79,7 @@ public struct Item: Codable, Hashable, Sendable {
self.crc32c = try container.decode(String.self, forKey: .crc32c)
self.etag = try container.decode(String.self, forKey: .etag)
self.downloadTokens = try container.decode(String.self, forKey: .downloadTokens)
self.customMetadata = try container.decodeIfPresent([String: String].self, forKey: .customMetadata)
self.customMetadata = try container.decodeIfPresent(
[String: String].self, forKey: .customMetadata)
}
}
13 changes: 8 additions & 5 deletions Sources/Storage/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,25 @@ extension Storage {
"delimiter": "/",
"prefix": directoryPath,
"maxResults": maxResults?.description,
"pageToken": pageToken.map { $0.addingPercentEncoding(withAllowedCharacters: .alphanumerics)! }
"pageToken": pageToken.map {
$0.addingPercentEncoding(withAllowedCharacters: .alphanumerics)!
},
].compactMapValues { $0 }

let path: String = "v0/b/\(bucket)/o?\(queries.map { [$0, $1].joined(separator: "=") }.joined(separator: "&"))"
let path: String =
"v0/b/\(bucket)/o?\(queries.map { [$0, $1].joined(separator: "=") }.joined(separator: "&"))"

let endpoint = URL(string: "\(baseUrl.absoluteString)\(path)")!

let request = HTTPRequest(
method: .get,
url: endpoint
)

let (data, _) = try await httpClient.execute(for: request, from: nil)

let item = try JSONDecoder().decode(ListResponse.self, from: data)

return item
}
}
Expand All @@ -47,7 +50,7 @@ public struct ListResponse: Codable, Hashable, Sendable {
public var bucket: String
public var name: String
}

public var prefixes: [String]
public var items: [Item]
public var nextPageToken: String?
Expand Down
4 changes: 2 additions & 2 deletions Sources/Storage/Storage.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Foundation
import HTTPClient
import HTTPTypes
import HTTPTypesFoundation
import HTTPClient

public struct Storage<HTTPClient: HTTPClientProtocol & Sendable>: Sendable {
public var baseUrl = URL(string : "https://firebasestorage.googleapis.com/")!
public var baseUrl = URL(string: "https://firebasestorage.googleapis.com/")!
public var httpClient: HTTPClient

public init(httpClient: HTTPClient) {
Expand Down
10 changes: 5 additions & 5 deletions Sources/Storage/Update.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ private struct Body: Codable, Sendable, Hashable {
var contentLanguage: String?
var contentType: String?
var customMetadata: [String: String]?

private enum CodingKeys: String, CodingKey {
case cacheControl
case contentDisposition
Expand Down Expand Up @@ -40,9 +40,9 @@ extension Storage {
contentType: contentType,
customMetadata: customMetadata
)

let bodyData = try! JSONEncoder().encode(body)

let bucket = percentEncode(bucket)
let filePath = percentEncode(filePath)
let path = "v0/b/\(bucket)/o/\(filePath)"
Expand All @@ -57,11 +57,11 @@ extension Storage {
.contentLength: bodyData.count.description,
]
)

let (data, _) = try await httpClient.execute(for: request, from: bodyData)

let item = try JSONDecoder().decode(Item.self, from: data)

return item
}
}
Expand Down
9 changes: 5 additions & 4 deletions Sources/Storage/Upload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ extension Storage {
"uploadType": "resumable",
"name": filePath,
]
let path = "v0/b/\(bucket)/o/\(filePath)?\(queries.map { [$0.key, $0.value].joined(separator: "=") }.joined(separator: "&"))"
let path =
"v0/b/\(bucket)/o/\(filePath)?\(queries.map { [$0.key, $0.value].joined(separator: "=") }.joined(separator: "&"))"

let endpoint = URL(string: "\(baseUrl.absoluteString)\(path)")!

Expand All @@ -24,14 +25,14 @@ extension Storage {
url: endpoint,
headerFields: [
.contentType: contentType,
.contentLength: data.count.description
.contentLength: data.count.description,
]
)

let (data, _) = try await httpClient.execute(for: request, from: data)

let item = try JSONDecoder().decode(Item.self, from: data)

return item
}
}
22 changes: 11 additions & 11 deletions Tests/StorageTests/StorageTests.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Testing
import Foundation
import Storage
import Testing

let bucket = ProcessInfo.processInfo.environment["FIREBASE_STORAGE_BUCKET"]!
let storage = Storage(httpClient: URLSession.shared)
Expand All @@ -11,14 +11,14 @@ func uploadItem() async throws {
let svgData = try Data(contentsOf: svgFilepath)
let filePath = "images/swift/Swift_logo.svg"
let contentType = "image/svg+xml"

let item = try await storage.upload(
bucket: bucket,
filePath: filePath,
data: svgData,
contentType: contentType
)

#expect(item.bucket == bucket)
#expect(item.name == filePath)
#expect(item.contentType == contentType)
Expand All @@ -32,7 +32,7 @@ func getItem() async throws {
data: Data("Hello".utf8),
contentType: "text/plain"
)

let item = try await storage.item(
bucket: uploadedItem.bucket,
filePath: uploadedItem.name
Expand All @@ -50,7 +50,7 @@ func contentUrl() async throws {
data: Data("Hello".utf8),
contentType: "text/plain"
)

_ = storage.contentUrl(item: item)
}

Expand All @@ -62,7 +62,7 @@ func updateItem() async throws {
"key1": "value1",
"key2": "value2",
]

let uploadedItem = try await storage.upload(
bucket: bucket,
filePath: "memos/\(UUID())",
Expand All @@ -79,7 +79,7 @@ func updateItem() async throws {
contentType: value,
customMetadata: customMetadata
)

#expect(item.cacheControl == value)
#expect(item.contentDisposition == value)
#expect(item.contentEncoding == value)
Expand All @@ -102,22 +102,22 @@ func deleteItem() async throws {
@Test
func getList() async throws {
let directoryPath = "images/"

let listResponse1 = try await storage.list(
bucket: bucket,
directoryPath: directoryPath,
maxResults: 2
)

for prefix in listResponse1.prefixes {
_ = try await storage.list(
bucket: bucket,
directoryPath: prefix
)
}

let nextPageToken = try #require(listResponse1.nextPageToken)

_ = try await storage.list(
bucket: bucket,
directoryPath: directoryPath,
Expand Down

0 comments on commit 6ded734

Please sign in to comment.