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 #70 from nodes-vapor/feature/add-delete-function
Browse files Browse the repository at this point in the history
Implement delete function
  • Loading branch information
steffendsommer committed Mar 23, 2020
2 parents 702ee1f + e626ba0 commit 936c671
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ Storage.upload(url: "http://mysite.com/myimage.png", fileName: "profile.png", on

## Download a file ✅

> NB: This is not implemented yet (see https://github.com/nodes-vapor/storage/issues/47)
To download a file that was previously uploaded you simply use the generated path.
```swift
// download image as `Foundation.Data`
Expand Down Expand Up @@ -122,14 +120,11 @@ Storage.cdnPathBuilder = { baseURL, path in

## Delete a file ❌

> NB: This is not implemented yet (see https://github.com/nodes-vapor/storage/issues/47)
Deleting a file can be done as follows.
```swift
try Storage.delete("/images/profile.png")
```


## Configuration ⚙

`Storage` has a variety of configurable options.
Expand Down
7 changes: 3 additions & 4 deletions Sources/Storage/NetworkDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public protocol NetworkDriver: Service {

func upload(entity: inout FileEntity, 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<Void>
func delete(path: String, on container: Container) throws -> Future<Response>
}

public final class S3Driver: NetworkDriver {
Expand Down Expand Up @@ -108,8 +108,7 @@ public final class S3Driver: NetworkDriver {
return try s3.get(path: path, on: container).map { $0 }
}

public func delete(path: String, on container: Container) throws -> Future<Void> {
#warning("Not implemented yet")
fatalError("Missing implementation for NetworkDriver.delete(path:on:) on S3Driver.")
public func delete(path: String, on container: Container) throws -> Future<Response> {
return try s3.delete(path: path, on: container).map { $0 }
}
}
2 changes: 1 addition & 1 deletion Sources/Storage/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public class Storage {
- Parameters:
- path: The path of the file to be deleted.
*/
public static func delete(path: String, on container: Container) throws -> Future<Void> {
public static func delete(path: String, on container: Container) throws -> Future<Response> {
let networkDriver = try container.make(NetworkDriver.self)
return try networkDriver.delete(path: path, on: container)
}
Expand Down
30 changes: 30 additions & 0 deletions Sources/Storage/Utilities/S3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,36 @@ public struct S3: Service {
req.http.url = url
return client.send(req)
}

public func delete(
path: String,
contentType: String = "application/x-www-form-urlencoded; charset=utf-8",
access: AccessControlList = .publicRead,
on container: Container
) throws -> Future<Response> {
guard let url = URL(string: generateURL(for: path)) else {
throw Error.invalidPath
}

let signedHeaders = try signer.sign(
contentType: contentType,
method: .delete,
path: path,
headers: ["x-amz-acl": access.rawValue]
)

var headers: HTTPHeaders = [:]
signedHeaders.forEach {
headers.add(name: $0.key, value: $0.value)
}

let client = try container.client()
let req = Request(using: container)
req.http.method = .DELETE
req.http.headers = headers
req.http.url = url
return client.send(req)
}
}

extension S3 {
Expand Down

0 comments on commit 936c671

Please sign in to comment.