Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shouldRemove in upload method to delete file after successful upload #3854

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions Documentation/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,16 @@ let fileURL = Bundle.main.url(forResource: "video", withExtension: "mov")
AF.upload(fileURL, to: "https://httpbin.org/post").responseDecodable(of: DecodableType.self) { response in
debugPrint(response)
}

let tempFileURL = Bundle.main.url(forResource: "image", withExtension: "jpg")

// Flag to indicate that the file should be automatically removed once uploaded
let shouldRemove = true

AF.upload(fileURL, to: "https://httpbin.org/post", shouldRemove: shouldRemove).responseDecodable(of: DecodableType.self) { response in
debugPrint(response)
}

```

#### Uploading Multipart Form Data
Expand Down
12 changes: 8 additions & 4 deletions Source/Core/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ open class Session {
/// default.
/// - requestModifier: `RequestModifier` which will be applied to the `URLRequest` created from the provided
/// parameters. `nil` by default.
/// - shouldRemove: A `Bool` indicating whether whether the source file should be automatically removed once uploaded. `false` by default.
///
/// - Returns: The created `UploadRequest`.
open func upload(_ fileURL: URL,
Expand All @@ -760,13 +761,14 @@ open class Session {
headers: HTTPHeaders? = nil,
interceptor: RequestInterceptor? = nil,
fileManager: FileManager = .default,
requestModifier: RequestModifier? = nil) -> UploadRequest {
requestModifier: RequestModifier? = nil,
shouldRemove: Bool = false) -> UploadRequest {
let convertible = ParameterlessRequestConvertible(url: convertible,
method: method,
headers: headers,
requestModifier: requestModifier)

return upload(fileURL, with: convertible, interceptor: interceptor, fileManager: fileManager)
return upload(fileURL, with: convertible, interceptor: interceptor, fileManager: fileManager, shouldRemove: shouldRemove)
}

/// Creates an `UploadRequest` for the file at the given file `URL` using the `URLRequestConvertible` value and
Expand All @@ -778,13 +780,15 @@ open class Session {
/// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
/// - fileManager: `FileManager` instance to be used by the returned `UploadRequest`. `.default` instance by
/// default.
/// - shouldRemove: A `Bool` indicating whether whether the source file should be automatically removed once uploaded. `false` by default.
///
/// - Returns: The created `UploadRequest`.
open func upload(_ fileURL: URL,
with convertible: URLRequestConvertible,
interceptor: RequestInterceptor? = nil,
fileManager: FileManager = .default) -> UploadRequest {
upload(.file(fileURL, shouldRemove: false), with: convertible, interceptor: interceptor, fileManager: fileManager)
fileManager: FileManager = .default,
shouldRemove: Bool = false) -> UploadRequest {
upload(.file(fileURL, shouldRemove: shouldRemove), with: convertible, interceptor: interceptor, fileManager: fileManager)
}

// MARK: InputStream
Expand Down
27 changes: 27 additions & 0 deletions Tests/UploadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -870,3 +870,30 @@ final class UploadRequestEventsTestCase: BaseTestCase {
XCTAssertEqual(request.state, .cancelled)
}
}

final class UploadFileWithDeletion: BaseTestCase {
func testFileUploadSuccessAndDeletion() {
// Given
let endpoint = Endpoint(path: .delay(interval: 1),
method: .post,
headers: [.contentType("text/plain")],
timeout: 0.1)

let fileURL = url(forResource: "utf8_string", withExtension: "txt")
let session = Session()
var response: AFDataResponse<TestResponse>?
let completion = expectation(description: "upload should complete and file should be deleted")

// When
session.upload(fileURL, with: endpoint, shouldRemove: true).responseDecodable(of: TestResponse.self) {
response = $0
completion.fulfill()
}

waitForExpectations(timeout: timeout, handler: nil)

// Then
XCTAssertTrue(response?.result.isSuccess == true)
XCTAssertFalse(FileManager.default.fileExists(atPath: fileURL.path), "file should be deleted after upload")
}
}