// MARK: Download helper methods
// This method generates a permanent local file path to save a track to by appending
// the lastPathComponent of the URL (i.e. the file name and extension of the file)
// to the path of the app’s Documents directory.
func localFilePathForUrl(_ previewUrl: String) → URL? {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
if let url = URL(string: previewUrl), let lastPathComponent = url.lastPathComponent {
let fullPath = documentsPath.appendingPathComponent(lastPathComponent)
return URL(fileURLWithPath:fullPath)
}
return nil
}
// This method checks if the local file exists at the path generated by localFilePathForUrl(
func localFileExistsForTrack( track: Track) → Bool {
if let urlString = track.previewUrl, let localUrl = localFilePathForUrl(urlString) {
var isDir : ObjCBool = false
if let path = localUrl.path {
return FileManager.default.fileExists(atPath: path, isDirectory: &isDir)
}
}
return false
}