Skip to content
Merged

Chunk #192

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions Sources/NextcloudKit/NKCommon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,10 @@
completion: @escaping (_ filesChunk: [(fileName: String, size: Int64)], _ error: Error?) -> Void = { _, _ in }) {
// Return existing chunks immediately
if !filesChunk.isEmpty {
numChunks(max(0, filesChunk.count - 1))
return completion(filesChunk, nil)
}

defer {
NotificationCenter.default.removeObserver(self, name: notificationCenterChunkedFileStop, object: nil)
}

let fileManager = FileManager.default
var isDirectory: ObjCBool = false
var reader: FileHandle?
Expand All @@ -116,10 +113,6 @@
let bufferSize = 1_000_000
var stop = false

NotificationCenter.default.addObserver(forName: notificationCenterChunkedFileStop, object: nil, queue: nil) { _ in
stop = true
}

// If max chunk count is > 10000 (max count), add + 100 MB to the chunk size to reduce the count. This is an edge case.
let inputFilePath = inputDirectory + "/" + fileName
let totalSize = getFileSize(filePath: inputFilePath)
Expand All @@ -136,7 +129,7 @@
do {
try fileManager.createDirectory(atPath: outputDirectory, withIntermediateDirectories: true, attributes: nil)
} catch {
return completion([], NSError(domain: "chunkedFile", code: -2,userInfo: [NSLocalizedDescriptionKey: "Failed to create the output directory for file chunks."]))

Check warning on line 132 in Sources/NextcloudKit/NKCommon.swift

View workflow job for this annotation

GitHub Actions / Lint

Comma Spacing Violation: There should be no space before and one after any comma (comma)
}
}

Expand All @@ -147,6 +140,17 @@
return completion([], NSError(domain: "chunkedFile", code: -1, userInfo: [NSLocalizedDescriptionKey: "Failed to open the input file for reading."]))
}

let tokenObserver = NotificationCenter.default.addObserver(forName: notificationCenterChunkedFileStop, object: nil, queue: nil) { _ in
stop = true
}

defer {
NotificationCenter.default.removeObserver(tokenObserver)

try? writer?.close()
try? reader?.close()
}

outerLoop: repeat {
if stop {
return completion([], NSError(domain: "chunkedFile", code: -5, userInfo: [NSLocalizedDescriptionKey: "Chunking was stopped by user request or system notification."]))
Expand All @@ -172,7 +176,7 @@

let safeBuffer = Data(rawBuffer)


Check warning on line 179 in Sources/NextcloudKit/NKCommon.swift

View workflow job for this annotation

GitHub Actions / Lint

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)
if writer == nil {
let fileNameChunk = String(counter)
let outputFileName = outputDirectory + "/" + fileNameChunk
Expand Down Expand Up @@ -228,9 +232,6 @@
}
} while true

writer?.closeFile()
reader?.closeFile()

// Update incremental chunk sizes
for i in 0..<filesChunk.count {
let path = outputDirectory + "/" + filesChunk[i].fileName
Expand Down
Loading