Skip to content

Commit 4e50809

Browse files
improved + fix
Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
1 parent b9b563e commit 4e50809

2 files changed

Lines changed: 37 additions & 17 deletions

File tree

Sources/NextcloudKit/NKCommon.swift

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Sources/NextcloudKit/NextcloudKit+Upload.swift

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,17 +244,29 @@ public extension NextcloudKit {
244244
let freeDisk = ((fsAttributes[FileAttributeKey.systemFreeSize] ?? 0) as? Int64) ?? 0
245245
#elseif os(visionOS) || os(iOS)
246246
var freeDisk: Int64 = 0
247-
let fileURL = URL(fileURLWithPath: directory as String)
247+
let outputPath = fileChunksOutputDirectory ?? directory
248+
let outputURL = URL(fileURLWithPath: outputPath)
249+
248250
do {
249-
let values = try fileURL.resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey])
250-
if let capacity = values.volumeAvailableCapacityForImportantUsage {
251-
freeDisk = capacity
251+
let keys: Set<URLResourceKey> = [
252+
.volumeAvailableCapacityForImportantUsageKey,
253+
.volumeAvailableCapacityKey
254+
]
255+
let values = try outputURL.resourceValues(forKeys: keys)
256+
257+
if let importantUsage = values.volumeAvailableCapacityForImportantUsage {
258+
freeDisk = importantUsage
259+
} else if let legacyCapacity = values.volumeAvailableCapacity {
260+
freeDisk = Int64(legacyCapacity)
252261
}
253-
} catch { }
262+
} catch {
263+
// fallback zero
264+
freeDisk = 0
265+
}
254266
#endif
255267

256268
#if os(visionOS) || os(iOS)
257-
if freeDisk < fileNameLocalSize * 4 {
269+
if freeDisk < fileNameLocalSize * 2 {
258270
// It seems there is not enough space to send the file
259271
return completion(account, nil, nil, .errorChunkNoEnoughMemory)
260272
}

0 commit comments

Comments
 (0)