Skip to content

Commit dfe28a9

Browse files
author
Ruben Nine
committed
Adding optional setting to allow automatic deletion of uploaded files if they are located in the user's temporary directory.
1 parent 4e38850 commit dfe28a9

File tree

6 files changed

+63
-3
lines changed

6 files changed

+63
-3
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// URL+Deletable.swift
3+
// FilestackSDK
4+
//
5+
// Created by Ruben Nine on 16/10/2020.
6+
// Copyright © 2020 Filestack. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
extension URL: Deletable {
12+
func delete() {
13+
guard isDeletable else { return }
14+
try? FileManager.default.removeItem(at: self)
15+
}
16+
}
17+
18+
private extension URL {
19+
var isDeletable: Bool {
20+
return path.starts(with: FileManager.default.temporaryDirectory.path)
21+
}
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Deletable.swift
3+
// FilestackSDK
4+
//
5+
// Created by Ruben Nine on 16/10/2020.
6+
// Copyright © 2020 Filestack. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
protocol Deletable {
12+
/// Deletes this deletable.
13+
func delete()
14+
}

Sources/FilestackSDK/Internal/Uploaders/MultipartUpload.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ private extension MultipartUpload {
147147

148148
// Block to run when operation completes.
149149
operation.completionBlock = {
150+
if self.options.deleteTemporaryFilesAfterUpload, let deletable = uploadable as? Deletable {
151+
deletable.delete()
152+
}
153+
150154
if let fileCompletedCount = self.progress.fileCompletedCount {
151155
self.progress.fileCompletedCount = fileCompletedCount + 1
152156
}

Sources/FilestackSDK/Public/Extensions/Client+Deprecated.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extension Client {
2424
completionHandler: @escaping (JSONResponse) -> Void) -> Uploader {
2525
let options = UploadOptions(preferIntelligentIngestion: useIntelligentIngestionIfAvailable,
2626
startImmediately: startUploadImmediately,
27+
deleteTemporaryFilesAfterUpload: false,
2728
storeOptions: storeOptions)
2829

2930
return upload(using: localURL,
@@ -46,6 +47,7 @@ extension Client {
4647
completionHandler: @escaping ([JSONResponse]) -> Void) -> Uploader {
4748
let options = UploadOptions(preferIntelligentIngestion: useIntelligentIngestionIfAvailable,
4849
startImmediately: startUploadImmediately,
50+
deleteTemporaryFilesAfterUpload: false,
4951
storeOptions: storeOptions)
5052

5153
return upload(using: localURLs,

Sources/FilestackSDK/Public/Models/UploadOptions.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ public class UploadOptions: NSObject {
2121

2222
/// A default set of upload options.
2323
@objc public static var defaults: UploadOptions = {
24-
UploadOptions(preferIntelligentIngestion: true, startImmediately: true)
24+
UploadOptions(
25+
preferIntelligentIngestion: true,
26+
startImmediately: true,
27+
deleteTemporaryFilesAfterUpload: false
28+
)
2529
}()
2630

2731
// MARK: - Public Properties
@@ -32,6 +36,9 @@ public class UploadOptions: NSObject {
3236
/// Whether the upload should start immediately.
3337
@objc public var startImmediately: Bool
3438

39+
/// Whether uploaded files located in the user's temporary directoy should be deleted after being uploaded.
40+
@objc public var deleteTemporaryFilesAfterUpload: Bool
41+
3542
/// An object containing the store options (e.g. location, region, container, access, etc.)
3643
@objc public var storeOptions: StorageOptions
3744

@@ -50,12 +57,14 @@ public class UploadOptions: NSObject {
5057
/// Default initializer.
5158
@objc public init(preferIntelligentIngestion: Bool,
5259
startImmediately: Bool,
60+
deleteTemporaryFilesAfterUpload: Bool,
5361
storeOptions: StorageOptions = .defaults,
5462
uploadTags: [String: String] = [:],
5563
partUploadConcurrency: Int = UploadOptions.defaultPartUploadConcurrency,
5664
chunkUploadConcurrency: Int = UploadOptions.defaultChunkUploadConcurrency) {
5765
self.preferIntelligentIngestion = preferIntelligentIngestion
5866
self.startImmediately = startImmediately
67+
self.deleteTemporaryFilesAfterUpload = deleteTemporaryFilesAfterUpload
5968
self.storeOptions = storeOptions
6069
self.uploadTags = uploadTags
6170
self.partUploadConcurrency = partUploadConcurrency

Tests/FilestackSDKTests/UploadTests.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class UploadTests: XCTestCase {
5252

5353
let uploadOptions = UploadOptions(preferIntelligentIngestion: false,
5454
startImmediately: true,
55+
deleteTemporaryFilesAfterUpload: false,
5556
storeOptions: defaultStoreOptions)
5657

5758
let uploader = client.upload(using: largeFileURL, options: uploadOptions) { resp in
@@ -140,7 +141,9 @@ class UploadTests: XCTestCase {
140141
let expectation = self.expectation(description: "request should succeed")
141142
var response: [JSONResponse] = []
142143

143-
let uploadOptions = UploadOptions(preferIntelligentIngestion: true, startImmediately: true)
144+
let uploadOptions = UploadOptions(preferIntelligentIngestion: true,
145+
startImmediately: true,
146+
deleteTemporaryFilesAfterUpload: false)
144147

145148
let uploader = client.upload(options: uploadOptions) { resp in
146149
response = resp
@@ -161,7 +164,9 @@ class UploadTests: XCTestCase {
161164
let expectation = self.expectation(description: "request should succeed")
162165
var response: [JSONResponse] = []
163166

164-
let uploadOptions = UploadOptions(preferIntelligentIngestion: true, startImmediately: false)
167+
let uploadOptions = UploadOptions(preferIntelligentIngestion: true,
168+
startImmediately: false,
169+
deleteTemporaryFilesAfterUpload: false)
165170

166171
let uploader = client.upload(options: uploadOptions) { resp in
167172
response = resp
@@ -251,6 +256,7 @@ class UploadTests: XCTestCase {
251256

252257
let uploadOptions = UploadOptions(preferIntelligentIngestion: true,
253258
startImmediately: true,
259+
deleteTemporaryFilesAfterUpload: false,
254260
storeOptions: storeOptions)
255261

256262
uploadOptions.uploadTags = uploadTags
@@ -303,6 +309,7 @@ class UploadTests: XCTestCase {
303309

304310
let uploadOptions = UploadOptions(preferIntelligentIngestion: false,
305311
startImmediately: true,
312+
deleteTemporaryFilesAfterUpload: false,
306313
storeOptions: defaultStoreOptions)
307314

308315
let uploader = client.upload(using: [sampleFileURL], options: uploadOptions) { resp in
@@ -340,6 +347,7 @@ class UploadTests: XCTestCase {
340347

341348
let uploadOptions = UploadOptions(preferIntelligentIngestion: false,
342349
startImmediately: true,
350+
deleteTemporaryFilesAfterUpload: false,
343351
storeOptions: defaultStoreOptions)
344352

345353
let uploader = client.upload(using: [sampleFileURL, sampleFileURL], options: uploadOptions) { resp in
@@ -367,6 +375,7 @@ class UploadTests: XCTestCase {
367375

368376
let uploadOptions = UploadOptions(preferIntelligentIngestion: false,
369377
startImmediately: false,
378+
deleteTemporaryFilesAfterUpload: false,
370379
storeOptions: defaultStoreOptions)
371380

372381
let uploader = client.upload(options: uploadOptions) { resp in

0 commit comments

Comments
 (0)