Giter VIP home page Giter VIP logo

liquid-kit's Introduction

LiquidKit

An abstract FileStorage solution, based on the SwiftNIO framework.

Key-based file storage

  • The main concept of LiquidKit is somewhat similar how AWS S3 buckets work.
  • You can use keys (relative path components) to store files using various drivers.
  • Keys should be designed to work with all the supported drivers.
  • Drivers should provide a resolution mechanism to return the absolute URL for a given key.

e.g.

the key "test.txt" could be resolved to "http://localhost:8080/assets/test.txt" when using the local fs driver.

Drivers and Vapor 4 support

Currently available drivers:

LiquidKit is also compatible with Vapor 4 through the Liquid repository, that contains Vapor specific extensions.

Usage with SwiftNIO

You can use the Liquid FileStorage driver directly with SwiftNIO, here's a possible usage example:

/// setup thread pool
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let pool = NIOThreadPool(numberOfThreads: 1)
pool.start()

/// create fs  
let fileio = NonBlockingFileIO(threadPool: pool)
let storages = FileStorages(fileio: fileio)
storages.use(.custom(exampleConfigVariable: "assets"), as: .custom)
let fs = storages.fileStorage(.custom, logger: .init(label: "[test-logger]"), on: elg.next())!

/// test file upload
let key = "test.txt"
let data = Data("file storage test".utf8)
let res = try fs.upload(key: key, data: data).wait()

How to implement a custom driver?

Drivers should implement the following protocols:

FileStorageID

Used to uniquely identify the file storage driver.

public extension FileStorageID {
    static var customDriver: FileStorageID { .init(string: "custom-driver-identifier") }
}

FileStorageConfiguration

A custom set of configuration variables required to initialize or setup the driver.

struct LiquidCustomStorageConfiguration: FileStorageConfiguration {
    let exampleConfigVariable: String

    func makeDriver(for storages: FileStorages) -> FileStorageDriver {
        return LiquidCustomStorageDriver(fileio: storages.fileio, configuration: self)
    }
}

FileStorageDriver

The file storage driver used to create the underlying storage object (that implements the API methods) using the configuration and context.

struct LiquidCustomStorageDriver: FileStorageDriver {

    let fileio: NonBlockingFileIO
    let configuration: LiquidCustomStorageConfiguration

    func makeStorage(with context: FileStorageContext) -> FileStorage {
        LiquidCustomStorage(fileio: fileio, configuration: configuration, context: context)
    }
    
    func shutdown() {

    }
}

FileStorage

Actual storage implementation that handles the necessary API methods.

struct LiquidCustomStorage: FileStorage {

    let fileio: NonBlockingFileIO
    let configuration: LiquidCustomStorageConfiguration
    let context: FileStorageContext
    
    
    init(fileio: NonBlockingFileIO, configuration: LiquidCustomStorageConfiguration, context: FileStorageContext) {
        self.fileio = fileio
        self.configuration = configuration
        self.context = context
    }

    // MARK: - api

    func resolve(key: String) -> String { /* ... */ }
    func upload(key: String, data: Data) async throws -> String { /* ... */ }
    func createDirectory(key: String) async throws { /* ... */ }
    func list(key: String?) async throws -> [String] { /* ... */ }
    func copy(key source: String, to destination: String) async throws -> String { /* ... */ }
    func move(key source: String, to destination: String) async throws -> String { /* ... */ }
    func delete(key: String) async throws { /* ... */ }
    func exists(key: String) async -> Bool { /* ... */ }
}

FileStorageConfigurationFactory

An extension on the FileStorageConfigurationFactory object that helps you to create the custom driver with the necessary config values.

public extension FileStorageConfigurationFactory {

    static func custom(exampleConfigVariable: String) -> FileStorageConfigurationFactory {
        .init { LiquidCustomStorageConfiguration(exampleConfigVariable) }
    }
}

liquid-kit's People

Contributors

moveupwardsdev avatar tib avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.