Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #72 from nodes-vapor/feature/add-access-control-flag
Browse files Browse the repository at this point in the history
Allow to provide an ACL flag
  • Loading branch information
cweinberger authored Oct 8, 2020
2 parents 936c671 + 3303ac0 commit 9f3b323
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
24 changes: 13 additions & 11 deletions Sources/Storage/NetworkDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ import Foundation
public protocol NetworkDriver: Service {
var pathBuilder: PathBuilder { get set }

func upload(entity: inout FileEntity, on container: Container) throws -> Future<String>
func upload(entity: inout FileEntity, access: AccessControlList?, on container: Container) throws -> Future<String>
func get(path: String, on container: Container) throws -> Future<Response>
func delete(path: String, on container: Container) throws -> Future<Response>
}

extension NetworkDriver {
func upload(
entity: inout FileEntity,
on container: Container
) throws -> Future<String> {
try upload(entity: &entity, access: nil, on: container)
}
}

public final class S3Driver: NetworkDriver {
public enum Error: Swift.Error {
case nilFileUpload
Expand Down Expand Up @@ -43,7 +52,7 @@ public final class S3Driver: NetworkDriver {
fileExtension: String? = nil,
mime: String? = nil,
folder: String? = nil,
access: AccessControlList,
access: AccessControlList = .publicRead,
on container: Container
) throws -> Future<String> {
var entity = FileEntity(
Expand All @@ -59,14 +68,7 @@ public final class S3Driver: NetworkDriver {

public func upload(
entity: inout FileEntity,
on container: Container
) throws -> Future<String> {
return try self.upload(entity: &entity, access: .publicRead, on: container)
}

public func upload(
entity: inout FileEntity,
access: AccessControlList,
access: AccessControlList?,
on container: Container
) throws -> Future<String> {
guard let bytes = entity.bytes else {
Expand Down Expand Up @@ -99,7 +101,7 @@ public final class S3Driver: NetworkDriver {
bytes: Data(bytes),
path: path,
contentType: mime,
access: access,
access: access ?? .publicRead,
on: container
).transform(to: path)
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/Storage/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ public class Storage {
@discardableResult
public static func upload(
entity: inout FileEntity,
access: AccessControlList? = nil,
on container: Container
) throws -> Future<String> {
let networkDriver = try container.make(NetworkDriver.self)
return try networkDriver.upload(entity: &entity, on: container)
return try networkDriver.upload(entity: &entity, access: access, on: container)
}

/**
Expand All @@ -50,6 +51,7 @@ public class Storage {
fileExtension: String? = nil,
mime: String? = nil,
folder: String? = nil,
access: AccessControlList? = nil,
on container: Container
) throws -> Future<String> {
var entity = FileEntity(
Expand Down Expand Up @@ -80,6 +82,7 @@ public class Storage {
fileName: String? = nil,
fileExtension: String? = nil,
folder: String? = nil,
access: AccessControlList? = nil,
on container: Container
) throws -> Future<String> {
let (bytes, type) = try dataURI.dataURIDecoded()
Expand Down

0 comments on commit 9f3b323

Please sign in to comment.