Skip to content

Commit 1cd7591

Browse files
committed
Add extension method to work around Swift compiler buffoonery
1 parent 62f5bc7 commit 1cd7591

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// FileManager+FileOperations.swift
3+
//
4+
5+
import Foundation
6+
7+
extension FileManager {
8+
9+
// Work around Swift compiler not bridging Dictionary
10+
// and NSDictionary properly when calling attributesOfItem
11+
func fileSizeOfItem(
12+
atPath path: String
13+
) throws -> UInt64 {
14+
(try attributesOfItem(atPath: path) as NSDictionary)
15+
.fileSize()
16+
}
17+
}

Sources/MuxUploadSDK/InternalUtilities/ChunkedFile.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class ChunkedFile {
5656
func openFile(fileURL: URL) throws {
5757
if state == nil {
5858
do {
59-
let fileSize = try (fileManager.attributesOfItem(atPath: fileURL.path) as NSDictionary).fileSize()
59+
let fileSize = try fileManager.fileSizeOfItem(atPath: fileURL.path)
6060
let fileHandle = try FileHandle(forReadingFrom: fileURL)
6161
state = State(
6262
fileHandle: fileHandle,
@@ -93,9 +93,9 @@ class ChunkedFile {
9393
}
9494
let data = try fileHandle.read(upToCount: chunkSize)
9595

96-
let fileSize = try (fileManager.attributesOfItem(
96+
let fileSize = try fileManager.fileSizeOfItem(
9797
atPath: fileURL.path
98-
) as NSDictionary).fileSize()
98+
)
9999

100100
guard let data = data else {
101101
// Called while already at the end of the file. We read zero bytes, "ending" at the end of the file

Sources/MuxUploadSDK/Upload/ChunkedFileUploader.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ class ChunkedFileUploader {
8888
let task = Task.detached { [self] in
8989
do {
9090
// It's fine if it's already open, that's handled by ignoring the call
91-
let fileSize = (try FileManager.default.attributesOfItem(atPath: uploadInfo.videoFile.path) as NSDictionary).fileSize()
91+
let fileSize = try FileManager.default.fileSizeOfItem(
92+
atPath: uploadInfo.videoFile.path
93+
)
9294
let result = try await makeWorker().performUpload()
9395
file.close()
9496

@@ -240,9 +242,9 @@ fileprivate actor Worker {
240242
try chunkedFile.seekTo(byte: startingReadCount)
241243

242244
let startTime = Date().timeIntervalSince1970
243-
let fileSize = (try FileManager.default.attributesOfItem(
245+
let fileSize = try FileManager.default.fileSizeOfItem(
244246
atPath: uploadInfo.videoFile.path
245-
) as NSDictionary).fileSize()
247+
)
246248

247249
let wideFileSize: Int64
248250

0 commit comments

Comments
 (0)