Skip to content

Commit 2e39605

Browse files
authored
Merge 578c283 into 92eebaf
2 parents 92eebaf + 578c283 commit 2e39605

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

FirebaseStorage/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 10.5.0
2+
- [added] Added Storage API to limit upload chunk size. (#10137)
3+
14
# 10.3.0
25
- [fixed] Use dedicated serial queue for Storage uploads and downloads instead of a (concurrent) global queue.
36
Fixes regression introduced in 10.0.0. (#10487)

FirebaseStorage/Sources/Storage.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ import FirebaseAuthInterop
123123
}
124124
}
125125

126+
/**
127+
* Specify the maximum upload chunk size. Values less than 256K (262144) will be rounded up to 256K. Values
128+
* above 256K will be rounded down to the nearest 256K multiple. The default is no maximum.
129+
*/
130+
@objc public var uploadChunkSizeBytes: Int64 = .max
131+
126132
/**
127133
* A `DispatchQueue` that all developer callbacks are fired on. Defaults to the main queue.
128134
*/

FirebaseStorage/Sources/StorageUploadTask.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ import Foundation
7676
let uploadFetcher = GTMSessionUploadFetcher(
7777
request: request,
7878
uploadMIMEType: contentType,
79-
chunkSize: Int64.max,
79+
chunkSize: self.reference.storage.uploadChunkSizeBytes,
8080
fetcherService: self.fetcherService
8181
)
8282
if let data = self.uploadData {

FirebaseStorage/Tests/Integration/StorageIntegration.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,52 @@ class StorageResultTests: StorageIntegrationCommon {
265265
waitForExpectations()
266266
}
267267

268+
func testPutFileLimitedChunk() throws {
269+
let expectation = self.expectation(description: #function)
270+
let putFileExpectation = self.expectation(description: "putFile")
271+
let ref = storage.reference(withPath: "ios/public/testPutFilePauseResume")
272+
let bundle = Bundle(for: StorageIntegrationCommon.self)
273+
let filePath = try XCTUnwrap(bundle.path(forResource: "1mb", ofType: "dat"),
274+
"Failed to get filePath")
275+
let data = try XCTUnwrap(try Data(contentsOf: URL(fileURLWithPath: filePath)),
276+
"Failed to load file")
277+
let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory())
278+
let fileURL = tmpDirURL.appendingPathComponent("LargePutFile.txt")
279+
var progressCount = 0
280+
281+
try data.write(to: fileURL, options: .atomicWrite)
282+
283+
// Limit the upload chunk size
284+
storage.uploadChunkSizeBytes = 256_000
285+
286+
let task = ref.putFile(from: fileURL) { result in
287+
XCTAssertGreaterThanOrEqual(progressCount, 4)
288+
self.assertResultSuccess(result)
289+
putFileExpectation.fulfill()
290+
}
291+
292+
task.observe(StorageTaskStatus.success) { snapshot in
293+
XCTAssertEqual(snapshot.description, "<State: Success>")
294+
expectation.fulfill()
295+
}
296+
297+
var uploadedBytes: Int64 = -1
298+
299+
task.observe(StorageTaskStatus.progress) { snapshot in
300+
XCTAssertTrue(snapshot.description.starts(with: "<State: Progress") ||
301+
snapshot.description.starts(with: "<State: Resume"))
302+
guard let progress = snapshot.progress else {
303+
XCTFail("Failed to get snapshot.progress")
304+
return
305+
}
306+
progressCount = progressCount + 1
307+
XCTAssertGreaterThanOrEqual(progress.completedUnitCount, uploadedBytes)
308+
uploadedBytes = progress.completedUnitCount
309+
}
310+
waitForExpectations()
311+
storage.uploadChunkSizeBytes = Int64.max
312+
}
313+
268314
func testAttemptToUploadDirectoryShouldFail() throws {
269315
// This `.numbers` file is actually a directory.
270316
let fileName = "HomeImprovement.numbers"

0 commit comments

Comments
 (0)