FileStorage.swift 4.1 KB

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