ModelDownloader.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2020 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. /// Possible errors with model downloading.
  16. public enum DownloadError: Error, Equatable {
  17. /// No model with this name found on server.
  18. case notFound
  19. /// Caller does not have necessary permissions for this operation.
  20. case permissionDenied
  21. /// Conditions not met to perform download.
  22. case failedPrecondition
  23. /// Not enough space for model on device.
  24. case notEnoughSpace
  25. /// Malformed model name.
  26. case invalidArgument
  27. /// Other errors with description.
  28. case internalError(description: String)
  29. }
  30. /// Possible errors with locating model on device.
  31. public enum DownloadedModelError: Error {
  32. /// File system error.
  33. case fileIOError
  34. /// Model not found on device.
  35. case notFound
  36. }
  37. /// Possible ways to get a custom model.
  38. public enum ModelDownloadType {
  39. /// Get local model stored on device.
  40. case localModel
  41. /// Get local model on device and update to latest model from server in the background.
  42. case localModelUpdateInBackground
  43. /// Get latest model from server.
  44. case latestModel
  45. }
  46. /// Downloader to manage custom model downloads.
  47. public struct ModelDownloader {
  48. /// Downloads a custom model to device or gets a custom model already on device, w/ optional handler for progress.
  49. public func getModel(name modelName: String, downloadType: ModelDownloadType,
  50. conditions: ModelDownloadConditions,
  51. progressHandler: ((Float) -> Void)? = nil,
  52. completion: @escaping (Result<CustomModel, DownloadError>) -> Void) {
  53. // TODO: Model download
  54. let modelSize = Int()
  55. let modelPath = String()
  56. let modelHash = String()
  57. let customModel = CustomModel(
  58. name: modelName,
  59. size: modelSize,
  60. path: modelPath,
  61. hash: modelHash
  62. )
  63. completion(.success(customModel))
  64. completion(.failure(.notFound))
  65. }
  66. /// Gets all downloaded models.
  67. public func listDownloadedModels(completion: @escaping (Result<Set<CustomModel>,
  68. DownloadedModelError>) -> Void) {
  69. let customModels = Set<CustomModel>()
  70. // TODO: List downloaded models
  71. completion(.success(customModels))
  72. completion(.failure(.notFound))
  73. }
  74. /// Deletes a custom model from device.
  75. public func deleteDownloadedModel(name modelName: String,
  76. completion: @escaping (Result<Void, DownloadedModelError>)
  77. -> Void) {
  78. // TODO: Delete previously downloaded model
  79. completion(.success(()))
  80. completion(.failure(.notFound))
  81. }
  82. }