aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Library/BookmarkDocument.swift
blob: 47fa442b1e0ae0d3e4e712934412d3e788fadbd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import SwiftUI
import Foundation
import UniformTypeIdentifiers

struct BookmarkDocument: FileDocument {
  static var readableContentTypes: [UTType] { [.data, UTType(filenameExtension: "hlbm")!] }
  static var writableContentTypes: [UTType] { [.data, UTType(filenameExtension: "hlbm")!] }

  var bookmark: Bookmark
  
  init(bookmark: Bookmark) {
    self.bookmark = bookmark
  }
  
  init(configuration: ReadConfiguration) throws {
    guard configuration.file.isRegularFile,
          let data = configuration.file.regularFileContents,
          let fileName = configuration.file.preferredFilename,
          let bookmark = Bookmark(fileData: data, name: (fileName as NSString).deletingPathExtension)
    else {
      throw CocoaError(.fileReadCorruptFile)
    }
    self.bookmark = bookmark
  }
  
  func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
    let wrapper = FileWrapper(regularFileWithContents: self.bookmark.bookmarkFileData()!)
    wrapper.fileAttributes[FileAttributeKey.hfsCreatorCode.rawValue] = "HTLC".fourCharCode()
    wrapper.fileAttributes[FileAttributeKey.hfsTypeCode.rawValue] = "HTbm".fourCharCode()
    return wrapper
  }
}