@@ -95,7 +95,9 @@ public struct NKCommon: Sendable {
9595 counterChunk: @escaping ( _ counter: Int ) -> Void = { _ in } ,
9696 completion: @escaping ( _ filesChunk: [ ( fileName: String , size: Int64 ) ] ) -> Void = { _ in } ) {
9797 // Check if filesChunk is empty
98- if !filesChunk. isEmpty { return completion ( filesChunk) }
98+ if !filesChunk. isEmpty {
99+ return completion ( filesChunk)
100+ }
99101
100102 defer {
101103 NotificationCenter . default. removeObserver ( self , name: notificationCenterChunkedFileStop, object: nil )
@@ -113,14 +115,19 @@ public struct NKCommon: Sendable {
113115 let bufferSize = 1000000
114116 var stop : Bool = false
115117
116- NotificationCenter . default. addObserver ( forName: notificationCenterChunkedFileStop, object: nil , queue: nil ) { _ in stop = true }
118+ NotificationCenter . default. addObserver ( forName: notificationCenterChunkedFileStop, object: nil , queue: nil ) { _ in
119+ stop = true
120+ }
117121
118122 // If max chunk count is > 10000 (max count), add + 100 MB to the chunk size to reduce the count. This is an edge case.
119- var num : Int = Int ( getFileSize ( filePath: inputDirectory + " / " + fileName) / Int64( chunkSize) )
123+ let inputFilePath = inputDirectory + " / " + fileName
124+ let totalSize = getFileSize ( filePath: inputFilePath)
125+ var num : Int = Int ( totalSize / Int64( chunkSize) )
126+
120127 if num > 10000 {
121- chunkSize = chunkSize + 100000000
128+ chunkSize += 100_000_000
129+ num = Int ( totalSize / Int64( chunkSize) ) // ricalcolo
122130 }
123- num = Int ( getFileSize ( filePath: inputDirectory + " / " + fileName) / Int64( chunkSize) )
124131 numChunks ( num)
125132
126133 if !fileManager. fileExists ( atPath: outputDirectory, isDirectory: & isDirectory) {
@@ -132,7 +139,7 @@ public struct NKCommon: Sendable {
132139 }
133140
134141 do {
135- reader = try . init( forReadingFrom: URL ( fileURLWithPath: inputDirectory + " / " + fileName ) )
142+ reader = try . init( forReadingFrom: URL ( fileURLWithPath: inputFilePath ) )
136143 } catch {
137144 return completion ( [ ] )
138145 }
@@ -152,7 +159,7 @@ public struct NKCommon: Sendable {
152159 }
153160
154161 let chunkRemaining : Int = chunkSize - chunk
155- let buffer = reader? . readData ( ofLength: min ( bufferSize, chunkRemaining) )
162+ let rawBuffer = reader? . readData ( ofLength: min ( bufferSize, chunkRemaining) )
156163
157164 if writer == nil {
158165 let fileNameChunk = String ( counter)
@@ -167,10 +174,11 @@ public struct NKCommon: Sendable {
167174 filesChunk. append ( ( fileName: fileNameChunk, size: 0 ) )
168175 }
169176
170- if let buffer = buffer {
171- writer? . write ( buffer)
172- chunk = chunk + buffer. count
173- return buffer. count
177+ if let rawBuffer = rawBuffer {
178+ let safeBuffer = Data ( rawBuffer) // secure copy
179+ writer? . write ( safeBuffer)
180+ chunk = chunk + safeBuffer. count
181+ return safeBuffer. count
174182 }
175183 filesChunk = [ ]
176184 return 0
0 commit comments