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

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
tib committed Apr 28, 2020
0 parents commit d57db67
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
.build
.swiftpm
Packages
*.xcodeproj
xcuserdata/
17 changes: 17 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2018-2019 Binary Birds

Authors:

Tibor Bodecs <mail.tib@gmail.com>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.
23 changes: 23 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// swift-tools-version:5.2
import PackageDescription

let package = Package(
name: "liquid",
platforms: [
.macOS(.v10_15)
],
products: [
.library(name: "Liquid", targets: ["Liquid"]),
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "4.4.0"),
.package(url: "https://github.com/binarybirds/liquid-kit.git", from: "1.0.0"),
],
targets: [
.target(name: "Liquid", dependencies: [
.product(name: "Vapor", package: "vapor"),
.product(name: "LiquidKit", package: "liquid-kit"),
]),
.testTarget(name: "LiquidTests", dependencies: ["Liquid"]),
]
)
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Liquid

Abstract file storage component made for Vapor 4.


## Usage example

Add Liquid as a dependency using SPM, you can choose between the local and the AWS S3 driver.

```swift
// swift-tools-version:5.2
import PackageDescription

let package = Package(
name: "myProject",
platforms: [
.macOS(.v10_15)
],
dependencies: [
// 💧 A server-side Swift web framework.
.package(url: "https://github.com/vapor/vapor.git", from: "4.4.0"),
.package(url: "https://github.com/binarybirds/liquid.git", from: "0.0.1"),
.package(url: "https://github.com/binarybirds/liquid-local-driver.git", from: "0.0.1"),
.package(url: "https://github.com/binarybirds/liquid-aws-s3-driver.git", from: "0.0.1"),
],
targets: [
.target(name: "App", dependencies: [
.product(name: "Vapor", package: "vapor"),
.product(name: "Liquid", package: "liquid"),
.product(name: "LiquidLocalDriver", package: "liquid-local-driver"),
.product(name: "LiquidAwsS3Driver", package: "liquid-aws-s3-driver"),
]),
]
)
```

Driver configuration

```swift
import Liquid
import LiquidLocalDriver
import LiquidAwsS3Driver

public func configure(_ app: Application) throws {

app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))

// using the local driver
app.fileStorages.use(.local(publicUrl: "http://localhost:8080/",
publicPath: app.directory.publicDirectory,
workDirectory: "assets"), as: .local)

// using the AWS S3 driver
app.fileStorages.use(.awsS3(key: Environment.awsKey,
secret: Environment.awsSecret,
bucket: "vaportestbucket",
region: .uswest1), as: .awsS3)

}
```

File upload example:

```swift

func testUpload(req: Request) -> EventLoopFuture<String> {
let data: Data! = //...
let key = "path/to/my/file.txt"
return req.fs.upload(key: key, data: data)
// returns the full public url of the uploaded image
}

// resolve public url based on a key
// func resolve(key: String) -> String
req.fs.resolve(key: myImageKey)

// delete file based on a key
// func delete(key: String) -> EventLoopFuture<Void>
req.fs.delete(key: myImageKey)
```


## License

[WTFPL](LICENSE) - Do what the fuck you want to.
78 changes: 78 additions & 0 deletions Sources/Liquid/Application+Liquid.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// File.swift
//
//
// Created by Tibor Bodecs on 2020. 04. 28..
//

import Vapor
import LiquidKit
import NIO

extension Application {

public struct Liquid {
final class Storage {
let fileStorages: FileStorages

init(fileio: NonBlockingFileIO) {
self.fileStorages = FileStorages(fileio: fileio)
}
}

struct Key: StorageKey {
typealias Value = Storage
}

struct Lifecycle: LifecycleHandler {
func shutdown(_ application: Application) {
application.fileStorages.shutdown()
}
}

let application: Application

var storage: Storage {
if self.application.storage[Key.self] == nil {
self.initialize()
}
return self.application.storage[Key.self]!
}

func initialize() {
self.application.storage[Key.self] = .init(fileio: self.application.fileio)
self.application.lifecycle.use(Lifecycle())
}
}

public var liquid: Liquid {
.init(application: self)
}

}

public extension Request {

var fs: FileStorage {
self.fs(nil)
}

func fs(_ id: FileStorageID?) -> FileStorage {
self.application.fileStorages.fileStorage(id, logger: self.logger, on: self.eventLoop)!
}
}

public extension Application {

var fs: FileStorage {
self.fs(nil)
}

func fs(_ id: FileStorageID?) -> FileStorage {
self.fileStorages.fileStorage(id, logger: self.logger, on: self.eventLoopGroup.next())!
}

var fileStorages: FileStorages {
self.liquid.storage.fileStorages
}
}
8 changes: 8 additions & 0 deletions Sources/Liquid/Exports.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// File.swift
//
//
// Created by Tibor Bodecs on 2020. 04. 28..
//

@_exported import LiquidKit
7 changes: 7 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import XCTest

import LiquidTests

var tests = [XCTestCaseEntry]()
tests += LiquidTests.allTests()
XCTMain(tests)
13 changes: 13 additions & 0 deletions Tests/LiquidTests/LiquidTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import XCTest
@testable import Liquid

final class LiquidTests: XCTestCase {

static var allTests = [
("testExample", testExample),
]

func testExample() {
XCTAssertEqualTrue(true)
}
}
9 changes: 9 additions & 0 deletions Tests/LiquidTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import XCTest

#if !canImport(ObjectiveC)
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(LiquidTests.allTests),
]
}
#endif

0 comments on commit d57db67

Please sign in to comment.