Skip to content

Commit

Permalink
Use directory folder
Browse files Browse the repository at this point in the history
  • Loading branch information
3lvis committed Sep 28, 2016
1 parent ca86903 commit 8904a88
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
14 changes: 12 additions & 2 deletions Sources/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,12 @@ public class Networking {
let finalPath = "\(folderPath)/\(normalizedResourcesPath)"

if let url = URL(string: finalPath) {
if let cachesURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first {
#if os(tvOS)
let directory = FileManager.SearchPathDirectory.cachesDirectory
#else
let directory = TestCheck.isTesting ? FileManager.SearchPathDirectory.cachesDirectory : FileManager.SearchPathDirectory.documentDirectory
#endif
if let cachesURL = FileManager.default.urls(for: directory, in: .userDomainMask).first {
try (cachesURL as NSURL).setResourceValue(true, forKey: URLResourceKey.isExcludedFromBackupKey)
let folderURL = cachesURL.appendingPathComponent(URL(string: folderPath)!.absoluteString)

Expand Down Expand Up @@ -328,7 +333,12 @@ public class Networking {
Deletes the downloaded/cached files.
*/
public static func deleteCachedFiles() {
if let cachesURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first {
#if os(tvOS)
let directory = FileManager.SearchPathDirectory.cachesDirectory
#else
let directory = TestCheck.isTesting ? FileManager.SearchPathDirectory.cachesDirectory : FileManager.SearchPathDirectory.documentDirectory
#endif
if let cachesURL = FileManager.default.urls(for: directory, in: .userDomainMask).first {
let folderURL = cachesURL.appendingPathComponent(URL(string: Networking.domain)!.absoluteString)

if FileManager.default.exists(at: folderURL) {
Expand Down
7 changes: 6 additions & 1 deletion Tests/NetworkingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,12 @@ class NetworkingTests: XCTestCase {
func testDeleteDownloadedFiles() {
let networking = Networking(baseURL: self.baseURL)
networking.downloadImage("/image/png") { image, error in
let cachesURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
#if os(tvOS)
let directory = FileManager.SearchPathDirectory.cachesDirectory
#else
let directory = TestCheck.isTesting ? FileManager.SearchPathDirectory.cachesDirectory : FileManager.SearchPathDirectory.documentDirectory
#endif
let cachesURL = FileManager.default.urls(for: directory, in: .userDomainMask).first!
let folderURL = cachesURL.appendingPathComponent(URL(string: Networking.domain)!.absoluteString)
XCTAssertTrue(FileManager.default.exists(at: folderURL))
Networking.deleteCachedFiles()
Expand Down

0 comments on commit 8904a88

Please sign in to comment.