This repository has been archived by the owner on Nov 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d57db67
Showing
9 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.DS_Store | ||
.build | ||
.swiftpm | ||
Packages | ||
*.xcodeproj | ||
xcuserdata/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |