Skip to content

Commit

Permalink
Add support for FileWrapper in HwpFile.init (#43)
Browse files Browse the repository at this point in the history
* Change Init

* swiftlint

* Add preprocessor and test

* swiftlint
  • Loading branch information
sboh1214 committed Jan 18, 2021
1 parent c54b520 commit ae21bf6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let package = Package(
.library(name: "CoreHwp", targets: ["CoreHwp"])
],
dependencies: [
.package(url: "https://github.com/CoreOffice/OLEKit.git", .exact("0.2.0")),
.package(url: "https://github.com/CoreOffice/OLEKit.git", .exact("0.3.0")),
.package(url: "https://github.com/tsolomko/SWCompression.git", .exact("4.5.7")),

.package(url: "https://github.com/Quick/Nimble", .exact("9.0.0"))
Expand Down
4 changes: 2 additions & 2 deletions Sources/CoreHwp/HwpError.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

public enum HwpError: Error {
case invalidFilePath(path: String)
case invalidFile(path: String)
case streamDoesNotExist(name: HwpStreamName)
case streamDecompressFailed(name: HwpStreamName)
case invalidDataForString(data: Data, name: String)
Expand All @@ -16,7 +16,7 @@ public enum HwpError: Error {
extension HwpError: CustomStringConvertible {
public var description: String {
switch self {
case let .invalidFilePath(path):
case let .invalidFile(path):
return "Invalid File Path '\(path)'"
case let .streamDoesNotExist(name):
return "Stream '\(name)' does not exist"
Expand Down
23 changes: 18 additions & 5 deletions Sources/CoreHwp/HwpFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,27 @@ public struct HwpFile: HwpPrimitive {
previewText = HwpPreviewText()
}

public init(filePath: String) throws {
let ole: OLEFile
public init(fromPath filePath: String) throws {
do {
ole = try OLEFile(filePath)
let ole = try OLEFile(filePath)
try self.init(fromOLE: ole)
} catch {
throw HwpError.invalidFilePath(path: filePath)
throw HwpError.invalidFile(path: filePath)
}
}

#if os(iOS) || os(watchOS) || os(tvOS) || os(macOS)
public init(fromWrapper fileWrapper: FileWrapper) throws {
do {
let ole = try OLEFile(fileWrapper)
try self.init(fromOLE: ole)
} catch {
throw HwpError.invalidFile(path: fileWrapper.filename ?? "")
}
}
#endif

private init(fromOLE ole: OLEFile) throws {
let streams = Dictionary(uniqueKeysWithValues: ole.root.children.map { ($0.name, $0 ) })
let reader = StreamReader(ole, streams)

Expand All @@ -41,5 +55,4 @@ public struct HwpFile: HwpPrimitive {
let previewTextReader = try ole.stream(previewTextStream)
previewText = try HwpPreviewText.load(previewTextReader.readDataToEnd())
}

}
9 changes: 9 additions & 0 deletions Tests/CoreHwpTests/HwpErrorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import CoreHwp
import XCTest
import Nimble

class HwpErrorTests: XCTestCase {
func test() throws {
expect {try openHwp(#file, "")}.to(throwError(HwpError.invalidFile(path: "")))
}
}
4 changes: 2 additions & 2 deletions Tests/CoreHwpTests/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ func openHwp(_ location: String, _ name: String) throws -> HwpFile {
let url = URL(fileURLWithPath: location)
.deletingLastPathComponent()
.appendingPathComponent(name + ".hwp")
return try HwpFile(filePath: url.path)
return try HwpFile(fromPath: url.path)
}

func createHwp(_ location: String, _ name: String) throws -> (HwpFile, HwpFile) {
let url = URL(fileURLWithPath: location)
.deletingLastPathComponent()
.appendingPathComponent(name + ".hwp")
let this = HwpFile()
let official = try HwpFile(filePath: url.path)
let official = try HwpFile(fromPath: url.path)
return (this, official)
}

0 comments on commit ae21bf6

Please sign in to comment.