From 5005506ec4e3cf25dea48f470fbe228b5377caba Mon Sep 17 00:00:00 2001 From: cweinberger Date: Thu, 8 Oct 2020 11:23:16 +0200 Subject: [PATCH] Alternative approach --- Sources/Storage/NetworkDriver.swift | 15 ++++++++++++--- Sources/Storage/Storage.swift | 9 +++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Sources/Storage/NetworkDriver.swift b/Sources/Storage/NetworkDriver.swift index 147558f..dcbb0c9 100644 --- a/Sources/Storage/NetworkDriver.swift +++ b/Sources/Storage/NetworkDriver.swift @@ -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 + func upload(entity: inout FileEntity, access: AccessControlList?, on container: Container) throws -> Future func get(path: String, on container: Container) throws -> Future func delete(path: String, on container: Container) throws -> Future } +extension NetworkDriver { + func upload( + entity: inout FileEntity, + on container: Container + ) throws -> Future { + try upload(entity: &entity, access: nil, on: container) + } +} + public final class S3Driver: NetworkDriver { public enum Error: Swift.Error { case nilFileUpload @@ -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 { guard let bytes = entity.bytes else { @@ -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) } diff --git a/Sources/Storage/Storage.swift b/Sources/Storage/Storage.swift index 315147e..a6f08ba 100644 --- a/Sources/Storage/Storage.swift +++ b/Sources/Storage/Storage.swift @@ -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 { let networkDriver = try container.make(NetworkDriver.self) @@ -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 { var entity = FileEntity( @@ -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) } /** @@ -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 { let (bytes, type) = try dataURI.dataURIDecoded() @@ -92,6 +92,7 @@ public class Storage { fileExtension: fileExtension, mime: type, folder: folder, + access: access, on: container ) }