FileStorage.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import Disk
  2. import Foundation
  3. protocol FileStorage {
  4. func save<Value: JSON>(_ value: Value, as name: String) throws
  5. func retrieve<Value: JSON>(_ name: String, as type: Value.Type) throws -> Value
  6. func retrieveRaw(_ name: String) -> RawJSON?
  7. func append<Value: JSON>(_ newValue: Value, to name: String) throws
  8. func append<Value: JSON>(_ newValues: [Value], to name: String) throws
  9. func append<Value: JSON, T: Equatable>(_ newValue: Value, to name: String, uniqBy keyPath: KeyPath<Value, T>) throws
  10. func append<Value: JSON, T: Equatable>(_ newValues: [Value], to name: String, uniqBy keyPath: KeyPath<Value, T>) throws
  11. func remove(_ name: String) throws
  12. func rename(_ name: String, to newName: String) throws
  13. func transaction(_ exec: (FileStorage) throws -> Void) throws
  14. func urlFor(file: String) -> URL?
  15. }
  16. final class BaseFileStorage: FileStorage {
  17. private let processQueue = DispatchQueue.markedQueue(label: "BaseFileStorage.processQueue", qos: .utility)
  18. private var encoder: JSONEncoder {
  19. let encoder = JSONEncoder()
  20. encoder.outputFormatting = .prettyPrinted
  21. encoder.dateEncodingStrategy = .customISO8601
  22. return encoder
  23. }
  24. private var decoder: JSONDecoder {
  25. let decoder = JSONDecoder()
  26. decoder.dateDecodingStrategy = .customISO8601
  27. return decoder
  28. }
  29. func save<Value: JSON>(_ value: Value, as name: String) throws {
  30. try processQueue.safeSync {
  31. if let value = value as? RawJSON, let data = value.data(using: .utf8) {
  32. try Disk.save(data, to: .documents, as: name)
  33. } else {
  34. try Disk.save(value, to: .documents, as: name, encoder: self.encoder)
  35. }
  36. }
  37. }
  38. func retrieve<Value: JSON>(_ name: String, as type: Value.Type) throws -> Value {
  39. try processQueue.safeSync {
  40. try Disk.retrieve(name, from: .documents, as: type, decoder: decoder)
  41. }
  42. }
  43. func retrieveRaw(_ name: String) -> RawJSON? {
  44. processQueue.safeSync {
  45. guard let data = try? Disk.retrieve(name, from: .documents, as: Data.self) else {
  46. return nil
  47. }
  48. return String(data: data, encoding: .utf8)
  49. }
  50. }
  51. func append<Value: JSON>(_ newValue: Value, to name: String) throws {
  52. try processQueue.safeSync {
  53. try Disk.append(newValue, to: name, in: .documents, decoder: decoder, encoder: encoder)
  54. }
  55. }
  56. func append<Value: JSON>(_ newValues: [Value], to name: String) throws {
  57. try processQueue.safeSync {
  58. try Disk.append(newValues, to: name, in: .documents, decoder: decoder, encoder: encoder)
  59. }
  60. }
  61. func append<Value: JSON, T: Equatable>(_ newValue: Value, to name: String, uniqBy keyPath: KeyPath<Value, T>) throws {
  62. if let value = try? retrieve(name, as: Value.self) {
  63. if value[keyPath: keyPath] != newValue[keyPath: keyPath] {
  64. try append(newValue, to: name)
  65. }
  66. } else if let values = try? retrieve(name, as: [Value].self) {
  67. guard values.first(where: { $0[keyPath: keyPath] == newValue[keyPath: keyPath] }) == nil else {
  68. return
  69. }
  70. try append(newValue, to: name)
  71. } else {
  72. try save(newValue, as: name)
  73. }
  74. }
  75. func append<Value: JSON, T: Equatable>(_ newValues: [Value], to name: String, uniqBy keyPath: KeyPath<Value, T>) throws {
  76. if let value = try? retrieve(name, as: Value.self) {
  77. guard newValues.first(where: { $0[keyPath: keyPath] == value[keyPath: keyPath] }) == nil else {
  78. return
  79. }
  80. try append(newValues, to: name)
  81. } else if let values = try? retrieve(name, as: [Value].self) {
  82. try newValues.forEach { newValue in
  83. guard values.first(where: { $0[keyPath: keyPath] == newValue[keyPath: keyPath] }) == nil else {
  84. return
  85. }
  86. try append(newValue, to: name)
  87. }
  88. } else {
  89. try save(newValues, as: name)
  90. }
  91. }
  92. func remove(_ name: String) throws {
  93. try processQueue.safeSync {
  94. try Disk.remove(name, from: .documents)
  95. }
  96. }
  97. func rename(_ name: String, to newName: String) throws {
  98. try processQueue.safeSync {
  99. try Disk.rename(name, in: .documents, to: newName)
  100. }
  101. }
  102. func transaction(_ exec: (FileStorage) throws -> Void) throws {
  103. try processQueue.safeSync {
  104. try exec(self)
  105. }
  106. }
  107. func urlFor(file: String) -> URL? {
  108. try? Disk.url(for: file, in: .documents)
  109. }
  110. }