ModelFileManager.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. /// Manager for common file operations.
  16. enum ModelFileManager {
  17. private static let nameSeparator = "__"
  18. private static let modelNamePrefix = "fbml_model"
  19. private static let fileManager = FileManager.default
  20. /// Root directory of model file storage on device.
  21. static var modelsDirectory: URL {
  22. // TODO: Reconsider force unwrapping.
  23. #if os(tvOS)
  24. return fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first!
  25. #else
  26. return fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
  27. #endif
  28. }
  29. /// Name for model file stored on device.
  30. private static func getDownloadedModelFileName(appName: String, modelName: String) -> String {
  31. return [modelNamePrefix, appName, modelName].joined(separator: nameSeparator)
  32. }
  33. /// Model name from file path.
  34. static func getModelNameFromFilePath(_ path: URL) -> String? {
  35. return path.lastPathComponent.components(separatedBy: nameSeparator).last
  36. }
  37. /// Full path of model file stored on device.
  38. static func getDownloadedModelFilePath(appName: String, modelName: String) -> URL {
  39. let modelFileName = ModelFileManager.getDownloadedModelFileName(
  40. appName: appName,
  41. modelName: modelName
  42. )
  43. return ModelFileManager.modelsDirectory
  44. .appendingPathComponent(modelFileName)
  45. }
  46. /// Check if file is available at URL.
  47. static func isFileReachable(at fileURL: URL) -> Bool {
  48. do {
  49. return try fileURL.checkResourceIsReachable()
  50. } catch {
  51. /// File unreachable.
  52. return false
  53. }
  54. }
  55. /// Move file at a location to another location.
  56. static func moveFile(at sourceURL: URL, to destinationURL: URL) throws {
  57. if isFileReachable(at: destinationURL) {
  58. do {
  59. try fileManager.removeItem(at: destinationURL)
  60. } catch {
  61. throw DownloadError
  62. .internalError(
  63. description: "Could not replace existing model file - \(error.localizedDescription)"
  64. )
  65. }
  66. }
  67. do {
  68. try FileManager.default.moveItem(at: sourceURL, to: destinationURL)
  69. } catch {
  70. throw DownloadError
  71. .internalError(description: "Unable to save model file - \(error.localizedDescription)")
  72. }
  73. }
  74. /// Remove model file at a specific location.
  75. static func removeFile(at url: URL) throws {
  76. do {
  77. try fileManager.removeItem(at: url)
  78. } catch {
  79. throw DownloadedModelError
  80. .internalError(
  81. description: "Could not delete old model file - \(error.localizedDescription)"
  82. )
  83. }
  84. }
  85. static func contentsOfModelsDirectory() throws -> [URL] {
  86. do {
  87. let directoryContents = try ModelFileManager.fileManager.contentsOfDirectory(
  88. at: modelsDirectory,
  89. includingPropertiesForKeys: nil,
  90. options: .skipsHiddenFiles
  91. )
  92. return directoryContents.filter { directoryItem in
  93. !directoryItem.hasDirectoryPath
  94. }
  95. } catch {
  96. throw DownloadedModelError
  97. .internalError(
  98. description: "Could not retrieve model files in directory - \(error.localizedDescription)"
  99. )
  100. }
  101. }
  102. }