FileManager+AlamofireTests.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // FileManager+AlamofireTests.swift
  3. //
  4. // Copyright (c) 2014-2016 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import Foundation
  25. extension FileManager {
  26. // MARK: - Common Directories
  27. static var documentsDirectoryPath: String {
  28. return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
  29. }
  30. static var documentsDirectoryURL: URL {
  31. return URL(fileURLWithPath: FileManager.documentsDirectoryPath, isDirectory: true)
  32. }
  33. static var libraryDirectoryPath: String {
  34. return NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0]
  35. }
  36. static var libraryDirectoryURL: URL {
  37. return URL(fileURLWithPath: FileManager.libraryDirectoryPath, isDirectory: true)
  38. }
  39. static var applicationSupportDirectoryPath: String {
  40. return NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true)[0]
  41. }
  42. static var applicationSupportDirectoryURL: URL {
  43. return URL(fileURLWithPath: FileManager.applicationSupportDirectoryPath, isDirectory: true)
  44. }
  45. static var cachesDirectoryPath: String {
  46. return NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
  47. }
  48. static var cachesDirectoryURL: URL {
  49. return URL(fileURLWithPath: FileManager.cachesDirectoryPath, isDirectory: true)
  50. }
  51. static var temporaryDirectoryPath: String {
  52. return NSTemporaryDirectory()
  53. }
  54. static var temporaryDirectoryURL: URL {
  55. return URL(fileURLWithPath: FileManager.temporaryDirectoryPath, isDirectory: true)
  56. }
  57. // MARK: - File System Modification
  58. @discardableResult
  59. static func createDirectory(atPath path: String) -> Bool {
  60. do {
  61. try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
  62. return true
  63. } catch {
  64. return false
  65. }
  66. }
  67. @discardableResult
  68. static func removeItem(atPath path: String) -> Bool {
  69. do {
  70. try FileManager.default.removeItem(atPath: path)
  71. return true
  72. } catch {
  73. return false
  74. }
  75. }
  76. @discardableResult
  77. static func removeAllItemsInsideDirectory(atPath path: String) -> Bool {
  78. let enumerator = FileManager.default.enumerator(atPath: path)
  79. var result = true
  80. while let fileName = enumerator?.nextObject() as? String {
  81. let success = removeItem(atPath: path + "/\(fileName)")
  82. if !success { result = false }
  83. }
  84. return result
  85. }
  86. }