diff --git a/Sources/InfomaniakCore/Chunking/ChunkProvider.swift b/Sources/InfomaniakCore/Chunking/ChunkProvider.swift index f9972ed..98be160 100644 --- a/Sources/InfomaniakCore/Chunking/ChunkProvider.swift +++ b/Sources/InfomaniakCore/Chunking/ChunkProvider.swift @@ -32,31 +32,24 @@ public protocol ChunkProvidable: IteratorProtocol { public final class ChunkProvider: ChunkProvidable { public typealias Element = Data - let fileHandle: FileHandlable + let chunkReader: ChunkReader var ranges: [DataRange] - deinit { - do { - // For the sake of consistency - try fileHandle.close() - } catch {} - } - public init?(fileURL: URL, ranges: [DataRange]) { self.ranges = ranges - do { - fileHandle = try FileHandle(forReadingFrom: fileURL) - } catch { + guard let chunkReader = ChunkReader(fileURL: fileURL) else { return nil } + + self.chunkReader = chunkReader } /// Internal testing method init(mockedHandlable: FileHandlable, ranges: [DataRange]) { self.ranges = ranges - fileHandle = mockedHandlable + chunkReader = ChunkReader(mockedHandlable: mockedHandlable) } /// Will provide chunks one by one, using the IteratorProtocol @@ -69,23 +62,12 @@ public final class ChunkProvider: ChunkProvidable { let range = ranges.removeFirst() do { - let chunk = try readChunk(range: range) + let chunk = try chunkReader.readChunk(range: range) return chunk } catch { return nil } } - - // MARK: Internal - - func readChunk(range: DataRange) throws -> Data? { - let offset = range.lowerBound - try fileHandle.seek(toOffset: offset) - - let byteCount = Int(range.upperBound - range.lowerBound) + 1 - let chunk = try fileHandle.read(upToCount: byteCount) - return chunk - } } /// Print the FileHandle shows the current offset diff --git a/Sources/InfomaniakCore/Chunking/ChunkReader.swift b/Sources/InfomaniakCore/Chunking/ChunkReader.swift new file mode 100644 index 0000000..7615d8e --- /dev/null +++ b/Sources/InfomaniakCore/Chunking/ChunkReader.swift @@ -0,0 +1,53 @@ +/* + Infomaniak kDrive - iOS App + Copyright (C) 2024 Infomaniak Network SA + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +import Foundation + +@available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) +public final class ChunkReader { + let fileHandle: FileHandlable + + deinit { + do { + // For the sake of consistency + try fileHandle.close() + } catch {} + } + + public init?(fileURL: URL) { + do { + fileHandle = try FileHandle(forReadingFrom: fileURL) + } catch { + return nil + } + } + + /// Internal testing method + init(mockedHandlable: FileHandlable) { + fileHandle = mockedHandlable + } + + public func readChunk(range: DataRange) throws -> Data? { + let offset = range.lowerBound + try fileHandle.seek(toOffset: offset) + + let byteCount = Int(range.upperBound - range.lowerBound) + 1 + let chunk = try fileHandle.read(upToCount: byteCount) + return chunk + } +} diff --git a/Tests/InfomaniakCoreTests/Chunking/UTChunkProvider.swift b/Tests/InfomaniakCoreTests/Chunking/UTChunkProvider.swift index 64db526..66d87bd 100644 --- a/Tests/InfomaniakCoreTests/Chunking/UTChunkProvider.swift +++ b/Tests/InfomaniakCoreTests/Chunking/UTChunkProvider.swift @@ -118,7 +118,7 @@ final class UTChunkProvider: XCTestCase { XCTAssertEqual(index, range.lowerBound) } - let chunkProvider = ChunkProvider(mockedHandlable: mckFileHandle, ranges: []) + let chunkProvider = ChunkReader(mockedHandlable: mckFileHandle) // WHEN do { @@ -141,7 +141,7 @@ final class UTChunkProvider: XCTestCase { mckFileHandle.readUpToCountClosure = { _ in Data() } mckFileHandle.seekToOffsetError = NSError(domain: "k", code: 1337) - let chunkProvider = ChunkProvider(mockedHandlable: mckFileHandle, ranges: []) + let chunkProvider = ChunkReader(mockedHandlable: mckFileHandle) // WHEN do { @@ -168,7 +168,7 @@ final class UTChunkProvider: XCTestCase { mckFileHandle.readUpToCountClosure = { _ in Data() } mckFileHandle.readUpToCountError = NSError(domain: "k", code: 1337) - let chunkProvider = ChunkProvider(mockedHandlable: mckFileHandle, ranges: []) + let chunkProvider = ChunkReader(mockedHandlable: mckFileHandle) // WHEN do {