Skip to content
This repository has been archived by the owner on Nov 19, 2023. It is now read-only.

Commit

Permalink
Updated deps, new API methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tib committed Dec 1, 2020
1 parent 7784afb commit 949a3d4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 36 deletions.
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
"repositoryURL": "https://github.com/apple/swift-log.git",
"state": {
"branch": null,
"revision": "74d7b91ceebc85daf387ebb206003f78813f71aa",
"version": "1.2.0"
"revision": "173f567a2dfec11d74588eea82cecea555bdc0bc",
"version": "1.4.0"
}
},
{
"package": "swift-nio",
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "e876fb37410e0036b98b5361bb18e6854739572b",
"version": "2.16.0"
"revision": "2bae395344d41710dffab456265d534d7dc34ab8",
"version": "2.25.0"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.library(name: "LiquidKit", targets: ["LiquidKit"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.23.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.25.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.0"),
],
targets: [
Expand Down
8 changes: 7 additions & 1 deletion Sources/LiquidKit/FileStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ public protocol FileStorage {

//returns a key as a full url
func resolve(key: String) -> String

// uploads the data under the given key
func upload(key: String, data: Data) -> EventLoopFuture<String>

// create a new directory for a given key
func createDirectory(key: String) -> EventLoopFuture<Void>

// list the contents of a given object for a key
func list(key: String) -> EventLoopFuture<[String]>

// deletes the data under the given key
func delete(key: String) -> EventLoopFuture<Void>
}
61 changes: 31 additions & 30 deletions Sources/LiquidKit/FileStorages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,81 +30,82 @@ public final class FileStorages {
}

public func use(_ factory: FileStorageConfigurationFactory, as id: FileStorageID, isDefault: Bool? = nil) {
self.use(factory.make(), as: id, isDefault: isDefault)
use(factory.make(), as: id, isDefault: isDefault)
}

public func use(_ config: FileStorageConfiguration, as id: FileStorageID, isDefault: Bool? = nil) {
self.lock.lock()
defer { self.lock.unlock() }
self.configurations[id] = config
if isDefault == true || (self.defaultID == nil && isDefault != false) {
self.defaultID = id
lock.lock()
defer { lock.unlock() }
configurations[id] = config
if isDefault == true || (defaultID == nil && isDefault != false) {
defaultID = id
}
}

public func `default`(to id: FileStorageID) {
self.lock.lock()
defer { self.lock.unlock() }
self.defaultID = id
lock.lock()
defer { lock.unlock() }
defaultID = id
}

public func configuration(for id: FileStorageID? = nil) -> FileStorageConfiguration? {
self.lock.lock()
defer { self.lock.unlock() }
return self.configurations[id ?? self._requireDefaultID()]
lock.lock()
defer { lock.unlock() }
return configurations[id ?? _requireDefaultID()]
}

public func fileStorage(_ id: FileStorageID? = nil, logger: Logger, on eventLoop: EventLoop) -> FileStorage? {
self.lock.lock()
defer { self.lock.unlock() }
let id = id ?? self._requireDefaultID()
lock.lock()
defer { lock.unlock() }
let id = id ?? _requireDefaultID()
var logger = logger
logger[metadataKey: "file-storage-id"] = .string(id.string)
let configuration = self._requireConfiguration(for: id)
let configuration = _requireConfiguration(for: id)
let context = FileStorageContext(
configuration: configuration,
logger: logger,
eventLoop: eventLoop
)
let driver: FileStorageDriver
if let existing = self.drivers[id] {
if let existing = drivers[id] {
driver = existing
} else {
}
else {
let new = configuration.makeDriver(for: self)
self.drivers[id] = new
drivers[id] = new
driver = new
}
return driver.makeStorage(with: context)
}

public func reinitialize(_ id: FileStorageID? = nil) {
self.lock.lock()
defer { self.lock.unlock() }
let id = id ?? self._requireDefaultID()
if let driver = self.drivers[id] {
self.drivers[id] = nil
lock.lock()
defer { lock.unlock() }
let id = id ?? _requireDefaultID()
if let driver = drivers[id] {
drivers[id] = nil
driver.shutdown()
}
}

public func shutdown() {
self.lock.lock()
defer { self.lock.unlock() }
for driver in self.drivers.values {
lock.lock()
defer { lock.unlock() }
for driver in drivers.values {
driver.shutdown()
}
self.drivers = [:]
drivers = [:]
}

private func _requireConfiguration(for id: FileStorageID) -> FileStorageConfiguration {
guard let configuration = self.configurations[id] else {
guard let configuration = configurations[id] else {
fatalError("No file storage configuration registered for \(id).")
}
return configuration
}

private func _requireDefaultID() -> FileStorageID {
guard let id = self.defaultID else {
guard let id = defaultID else {
fatalError("No default file storage configured.")
}
return id
Expand Down

0 comments on commit 949a3d4

Please sign in to comment.