Skip to content

Commit 7c48f73

Browse files
author
Ruben Nine
committed
Postpone deletion of deletable files until MultipartUpload completion block completes. This brings the opportunity to move or copy any temporary Uploadable files before returning from the completion block.
1 parent 8c73e49 commit 7c48f73

File tree

2 files changed

+92
-16
lines changed

2 files changed

+92
-16
lines changed

Sources/FilestackSDK/Internal/Uploaders/MultipartUpload.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,17 +189,17 @@ private extension MultipartUpload {
189189
state = .completed
190190
}
191191

192-
// Delete any deletable files if `deleteTemporaryFilesAfterUpload` option is enabled.
193-
if options.deleteTemporaryFilesAfterUpload, let deletables = (uploadables?.compactMap { $0 as? Deletable }) {
194-
for deletable in deletables {
195-
deletable.delete()
196-
}
197-
}
198-
199192
queue.async {
200193
self.completionHandler?(results)
201194
self.completionHandler = nil
202195
self.uploadProgress = nil
196+
197+
// Delete any deletable files if `deleteTemporaryFilesAfterUpload` option is enabled.
198+
if self.options.deleteTemporaryFilesAfterUpload, let deletables = (self.uploadables?.compactMap { $0 as? Deletable }) {
199+
for deletable in deletables {
200+
deletable.delete()
201+
}
202+
}
203203
}
204204
}
205205
}

Tests/FilestackSDKTests/UploadTests.swift

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class UploadTests: XCTestCase {
6060
expectation.fulfill()
6161
}
6262

63-
waitForExpectations(timeout: 15, handler: nil)
63+
waitForExpectations(timeout: 15)
6464

6565
XCTAssertEqual(uploader.progress.totalUnitCount, Int64(largeFileSize))
6666
XCTAssertEqual(uploader.progress.completedUnitCount, Int64(largeFileSize))
@@ -75,6 +75,8 @@ class UploadTests: XCTestCase {
7575
XCTAssertEqual(response?.json?["url"] as? String, "https://cdn.filestackcontent.com/6GKA0wnQWO7tKaGu2YXA")
7676
XCTAssertEqual(response?.json?["mimetype"] as? String, "image/jpeg")
7777
XCTAssertEqual(response?.context as? URL, largeFileURL)
78+
79+
XCTAssertTrue(FileManager.default.fileExists(atPath: largeFileURL.path))
7880
}
7981

8082
func testIntelligentMultiPartUpload() {
@@ -92,7 +94,7 @@ class UploadTests: XCTestCase {
9294
expectation.fulfill()
9395
}
9496

95-
waitForExpectations(timeout: 15, handler: nil)
97+
waitForExpectations(timeout: 15)
9698

9799
XCTAssertEqual(uploader.progress.totalUnitCount, Int64(largeFileSize))
98100
XCTAssertEqual(uploader.progress.completedUnitCount, Int64(largeFileSize))
@@ -127,7 +129,7 @@ class UploadTests: XCTestCase {
127129
uploader.cancel()
128130
}
129131

130-
waitForExpectations(timeout: 15, handler: nil)
132+
waitForExpectations(timeout: 15)
131133

132134
XCTAssertEqual(uploader.progress.totalUnitCount, 0)
133135
XCTAssertEqual(uploader.progress.completedUnitCount, 0)
@@ -156,7 +158,7 @@ class UploadTests: XCTestCase {
156158
uploader.cancel()
157159
}
158160

159-
waitForExpectations(timeout: 15, handler: nil)
161+
waitForExpectations(timeout: 15)
160162

161163
XCTAssertEqual(response, [])
162164
XCTAssertTrue(uploader.progress.isCancelled)
@@ -179,7 +181,7 @@ class UploadTests: XCTestCase {
179181
uploader.cancel()
180182
}
181183

182-
waitForExpectations(timeout: 15, handler: nil)
184+
waitForExpectations(timeout: 15)
183185

184186
XCTAssertEqual(response, [])
185187
XCTAssertTrue(uploader.progress.isCancelled)
@@ -209,7 +211,7 @@ class UploadTests: XCTestCase {
209211
expectation.fulfill()
210212
}
211213

212-
waitForExpectations(timeout: 15, handler: nil)
214+
waitForExpectations(timeout: 15)
213215

214216
XCTAssertEqual(hitCount, 1)
215217
XCTAssertNotNil(response?.json)
@@ -253,7 +255,7 @@ class UploadTests: XCTestCase {
253255
expectation.fulfill()
254256
}
255257

256-
waitForExpectations(timeout: 15, handler: nil)
258+
waitForExpectations(timeout: 15)
257259

258260
XCTAssertEqual(hitCount, 1)
259261
XCTAssertEqual(responses.count, 1)
@@ -292,7 +294,7 @@ class UploadTests: XCTestCase {
292294
expectation.fulfill()
293295
}
294296

295-
waitForExpectations(timeout: 15, handler: nil)
297+
waitForExpectations(timeout: 15)
296298

297299
XCTAssertEqual(uploader.progress.totalUnitCount, Int64(sampleFileSize + largeFileSize))
298300
XCTAssertEqual(uploader.progress.completedUnitCount, Int64(sampleFileSize + largeFileSize))
@@ -325,7 +327,7 @@ class UploadTests: XCTestCase {
325327
uploader.add(uploadables: [sampleFileURL, largeFileURL])
326328
uploader.start()
327329

328-
waitForExpectations(timeout: 15, handler: nil)
330+
waitForExpectations(timeout: 15)
329331

330332
XCTAssertEqual(uploader.progress.totalUnitCount, Int64(sampleFileSize + largeFileSize))
331333
XCTAssertEqual(uploader.progress.completedUnitCount, Int64(sampleFileSize + largeFileSize))
@@ -336,6 +338,80 @@ class UploadTests: XCTestCase {
336338
XCTAssertEqual(responses[0].context as? URL, sampleFileURL)
337339
XCTAssertEqual(responses[1].context as? URL, largeFileURL)
338340
}
341+
342+
func testUploadFileAtTemporaryLocation() {
343+
var hitCount = 0
344+
345+
stubRegularMultipartRequest(hitCount: &hitCount)
346+
347+
let expectation = self.expectation(description: "request should succeed")
348+
349+
let fm = FileManager.default
350+
let temporaryURL = fm.temporaryDirectory.appendingPathComponent(largeFileURL.lastPathComponent)
351+
352+
if fm.fileExists(atPath: temporaryURL.path) {
353+
XCTAssertNoThrow(try? fm.removeItem(at: temporaryURL))
354+
}
355+
356+
XCTAssertNoThrow(try fm.copyItem(at: largeFileURL, to: temporaryURL))
357+
358+
var response: JSONResponse?
359+
360+
let uploadOptions = UploadOptions(preferIntelligentIngestion: false,
361+
startImmediately: true,
362+
deleteTemporaryFilesAfterUpload: false,
363+
storeOptions: defaultStoreOptions)
364+
365+
let uploader = client.upload(using: temporaryURL, options: uploadOptions) { resp in
366+
XCTAssertTrue(fm.fileExists(atPath: temporaryURL.path), "File should exist")
367+
368+
response = resp
369+
expectation.fulfill()
370+
371+
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
372+
XCTAssertFalse(fm.fileExists(atPath: temporaryURL.path), "File should no longer exist")
373+
}
374+
}
375+
376+
waitForExpectations(timeout: 15)
377+
378+
XCTAssertEqual(uploader.state, .completed)
379+
XCTAssertEqual(hitCount, 1)
380+
XCTAssertEqual(response?.context as? URL, temporaryURL)
381+
}
382+
383+
func testUploadFileAtPermanentLocation() {
384+
var hitCount = 0
385+
386+
stubRegularMultipartRequest(hitCount: &hitCount)
387+
388+
let expectation = self.expectation(description: "request should succeed")
389+
let fm = FileManager.default
390+
391+
var response: JSONResponse?
392+
393+
let uploadOptions = UploadOptions(preferIntelligentIngestion: false,
394+
startImmediately: true,
395+
deleteTemporaryFilesAfterUpload: false,
396+
storeOptions: defaultStoreOptions)
397+
398+
let uploader = client.upload(using: largeFileURL, options: uploadOptions) { resp in
399+
XCTAssertTrue(fm.fileExists(atPath: self.largeFileURL.path), "File should exist")
400+
401+
response = resp
402+
expectation.fulfill()
403+
404+
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
405+
XCTAssertFalse(fm.fileExists(atPath: self.largeFileURL.path), "File should still exist")
406+
}
407+
}
408+
409+
waitForExpectations(timeout: 15)
410+
411+
XCTAssertEqual(uploader.state, .completed)
412+
XCTAssertEqual(hitCount, 1)
413+
XCTAssertEqual(response?.context as? URL, largeFileURL)
414+
}
339415
}
340416

341417
// MARK: - Private Functions

0 commit comments

Comments
 (0)