FilesystemLocationResolver.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import Capacitor
  2. import Foundation
  3. import IONFilesystemLib
  4. struct FilesystemLocationResolver {
  5. let service: FileService
  6. func resolveSinglePath(from call: CAPPluginCall) -> Result<URL, FilesystemError> {
  7. guard let path = call.getString(Constants.MethodParameter.path) else {
  8. return .failure(.invalidInput(method: call.getIONFileMethod()))
  9. }
  10. let directory = call.getSearchPath(Constants.MethodParameter.directory)
  11. return resolveURL(path: path, directory: directory)
  12. }
  13. func resolveDualPaths(from call: CAPPluginCall) -> Result<(source: URL, destination: URL), FilesystemError> {
  14. guard let fromPath = call.getString(Constants.MethodParameter.from), let toPath = call.getString(Constants.MethodParameter.to) else {
  15. return .failure(.invalidInput(method: call.getIONFileMethod()))
  16. }
  17. let fromDirectory = call.getSearchPath(Constants.MethodParameter.directory)
  18. let toDirectory = call.getSearchPath(Constants.MethodParameter.toDirectory, withDefault: fromDirectory)
  19. return resolveURL(path: fromPath, directory: fromDirectory)
  20. .flatMap { sourceURL in
  21. resolveURL(path: toPath, directory: toDirectory)
  22. .map { (source: sourceURL, destination: $0) }
  23. }
  24. }
  25. private func resolveURL(path: String, directory: IONFILESearchPath) -> Result<URL, FilesystemError> {
  26. return if let url = try? service.getFileURL(atPath: path, withSearchPath: directory) {
  27. .success(url)
  28. } else {
  29. .failure(.invalidPath(path))
  30. }
  31. }
  32. }