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

Commit

Permalink
Alternative approach
Browse files Browse the repository at this point in the history
  • Loading branch information
cweinberger committed Oct 8, 2020
1 parent 68f8ca1 commit 5005506
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
15 changes: 12 additions & 3 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, access: AccessControlList, 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 @@ -59,7 +68,7 @@ public final class S3Driver: NetworkDriver {

public func upload(
entity: inout FileEntity,
access: AccessControlList = .publicRead,
access: AccessControlList?,
on container: Container
) throws -> Future<String> {
guard let bytes = entity.bytes else {
Expand Down Expand Up @@ -92,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
9 changes: 5 additions & 4 deletions Sources/Storage/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Storage {
@discardableResult
public static func upload(
entity: inout FileEntity,
access: AccessControlList = .publicRead,
access: AccessControlList? = nil,
on container: Container
) throws -> Future<String> {
let networkDriver = try container.make(NetworkDriver.self)
Expand All @@ -51,7 +51,7 @@ public class Storage {
fileExtension: String? = nil,
mime: String? = nil,
folder: String? = nil,
access: AccessControlList = .publicRead,
access: AccessControlList? = nil,
on container: Container
) throws -> Future<String> {
var entity = FileEntity(
Expand All @@ -62,7 +62,7 @@ public class Storage {
mime: mime
)

return try upload(entity: &entity, on: container)
return try upload(entity: &entity, access: access, on: container)
}

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

0 comments on commit 5005506

Please sign in to comment.