mirror of
https://github.com/danbee/persephone
synced 2025-03-04 08:39:11 +00:00
44 lines
1.4 KiB
Swift
44 lines
1.4 KiB
Swift
//
|
|
// AlbumArtService+Caching.swift
|
|
// Persephone
|
|
//
|
|
// Created by Daniel Barber on 2019/3/17.
|
|
// Copyright © 2019 Dan Barber. All rights reserved.
|
|
//
|
|
|
|
import Cocoa
|
|
|
|
extension AlbumArtService {
|
|
func getCachedArtwork(for album: AlbumItem, callback: @escaping (_ image: NSImage) -> Void) -> Bool {
|
|
guard let bundleIdentifier = Bundle.main.bundleIdentifier,
|
|
let cacheDir = try? FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
|
|
.appendingPathComponent(bundleIdentifier)
|
|
else { return false }
|
|
|
|
let cacheFilePath = cacheDir.appendingPathComponent(album.hash).path
|
|
|
|
if FileManager.default.fileExists(atPath: cacheFilePath) {
|
|
guard let data = FileManager.default.contents(atPath: cacheFilePath),
|
|
let image = NSImage(data: data)
|
|
else { return true }
|
|
|
|
callback(image)
|
|
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
func cacheArtwork(for album: AlbumItem, data: Data?) {
|
|
guard let bundleIdentifier = Bundle.main.bundleIdentifier,
|
|
let cacheDir = try? FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
|
|
.appendingPathComponent(bundleIdentifier)
|
|
else { return }
|
|
|
|
let cacheFilePath = cacheDir.appendingPathComponent(album.hash).path
|
|
|
|
FileManager.default.createFile(atPath: cacheFilePath, contents: data, attributes: nil)
|
|
}
|
|
}
|