Skip to content

Commit e6f39f8

Browse files
committed
clean-up
1 parent b194a5b commit e6f39f8

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

Sources/MuxUploadSDK/InternalUtilities/ChunkedFile.swift

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,45 @@ class ChunkedFile {
4444
guard fileHandle != nil else {
4545
return Result.failure(ChunkedFileError.invalidState("readNextChunk() called but the file was not open"))
4646
}
47-
return try Result.success(doReadNextChunk())
47+
48+
SDKLogger.logger?.info("--doReadNextChunk")
49+
guard let fileHandle = fileHandle, let fileURL = fileURL else {
50+
throw ChunkedFileError.invalidState("doReadNextChunk called without file handle. Did you call open()?")
51+
}
52+
var data : Data?
53+
try autoreleasepool {
54+
data = try fileHandle.read(upToCount: chunkSize)
55+
}
56+
57+
let fileSize = try fileManager.fileSizeOfItem(
58+
atPath: fileURL.path
59+
)
60+
61+
guard let data = data else {
62+
// Called while already at the end of the file. We read zero bytes, "ending" at the end of the file
63+
return .success(
64+
FileChunk(
65+
startByte: fileSize,
66+
endByte: fileSize,
67+
totalFileSize: fileSize,
68+
chunkData: Data(capacity: 0)
69+
)
70+
)
71+
}
72+
73+
let chunkLength = data.count
74+
let updatedFilePosition = filePos + UInt64(chunkLength)
75+
76+
let chunk = FileChunk(
77+
startByte: self.filePos,
78+
endByte: updatedFilePosition,
79+
totalFileSize: fileSize,
80+
chunkData: data
81+
)
82+
83+
state?.filePosition = updatedFilePosition
84+
85+
return .success(chunk)
4886
} catch {
4987
return Result.failure(ChunkedFileError.fileHandle(error))
5088
}
@@ -86,40 +124,6 @@ class ChunkedFile {
86124
state?.filePosition = byte
87125
}
88126

89-
private func doReadNextChunk() throws -> FileChunk {
90-
SDKLogger.logger?.info("--doReadNextChunk")
91-
guard let fileHandle = fileHandle, let fileURL = fileURL else {
92-
throw ChunkedFileError.invalidState("doReadNextChunk called without file handle. Did you call open()?")
93-
}
94-
var data : Data?
95-
try autoreleasepool {
96-
data = try fileHandle.read(upToCount: chunkSize)
97-
}
98-
99-
let fileSize = try fileManager.fileSizeOfItem(
100-
atPath: fileURL.path
101-
)
102-
103-
guard let data = data else {
104-
// Called while already at the end of the file. We read zero bytes, "ending" at the end of the file
105-
return FileChunk(startByte: fileSize, endByte: fileSize, totalFileSize: fileSize, chunkData: Data(capacity: 0))
106-
}
107-
108-
let nsData = NSData(data: data)
109-
let readLen = nsData.length
110-
let newFilePos = filePos + UInt64(readLen)
111-
let chunk = FileChunk(
112-
startByte: self.filePos,
113-
endByte: newFilePos,
114-
totalFileSize: fileSize,
115-
chunkData: data
116-
)
117-
118-
state?.filePosition = newFilePos
119-
120-
return chunk
121-
}
122-
123127
/// Creates a ``ChunkedFile`` that wraps the file given by the URL. The file will be opened after calling ``openFile()``
124128
init(chunkSize: Int) {
125129
self.chunkSize = chunkSize

0 commit comments

Comments
 (0)