1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 14:50:32 +05:30
yattee/Model/URLBookmarkModel.swift

157 lines
5.1 KiB
Swift
Raw Normal View History

2022-11-10 22:41:28 +05:30
import Foundation
import Logging
struct URLBookmarkModel {
2022-11-15 03:43:29 +05:30
static let bookmarkPrefix = "urlbookmark-"
2022-11-10 22:41:28 +05:30
static var shared = URLBookmarkModel()
2022-11-15 03:43:29 +05:30
2022-11-10 22:41:28 +05:30
var logger = Logger(label: "stream.yattee.url-bookmark")
2022-11-15 03:43:29 +05:30
var allBookmarksKeys: [String] {
2022-12-11 01:38:03 +05:30
guard let defaults = BookmarksCacheModel.shared.defaults else { return [] }
2022-11-15 03:43:29 +05:30
return defaults.dictionaryRepresentation().keys.filter { $0.starts(with: Self.bookmarkPrefix) }
}
var allURLs: [URL] {
allBookmarksKeys.compactMap { urlFromBookmark($0) }
}
2022-11-10 22:41:28 +05:30
func saveBookmark(_ url: URL) {
2022-12-04 17:51:50 +05:30
var urlForBookmark = url
if let yatteeSanitizedUrl = url.byReplacingYatteeProtocol() {
urlForBookmark = yatteeSanitizedUrl
}
guard urlForBookmark.isFileURL else {
logger.error("trying to save bookmark for something that is not a file")
logger.error("not a file: \(urlForBookmark.absoluteString)")
return
}
2022-12-11 01:38:03 +05:30
guard let defaults = BookmarksCacheModel.shared.defaults else {
logger.error("could not open bookmarks defaults")
return
}
2022-12-04 17:51:50 +05:30
if let bookmarkData = try? urlForBookmark.bookmarkData(options: bookmarkCreationOptions, includingResourceValuesForKeys: nil, relativeTo: nil) {
defaults.set(bookmarkData, forKey: bookmarkKey(urlForBookmark))
logger.info("saved bookmark for \(bookmarkKey(urlForBookmark))")
} else {
logger.error("no bookmark data for \(urlForBookmark)")
}
}
func saveBookmark(_ url: NSURL) {
guard url.isFileURL else {
logger.error("trying to save bookmark for something that is not a file")
2022-12-04 17:51:50 +05:30
logger.error("not a file: \(url.absoluteString)")
return
}
2022-12-11 01:38:03 +05:30
guard let defaults = BookmarksCacheModel.shared.defaults else {
2022-12-04 17:51:50 +05:30
logger.error("could not open bookmarks defaults")
return
}
2022-11-10 22:41:28 +05:30
if let bookmarkData = try? url.bookmarkData(options: bookmarkCreationOptions, includingResourceValuesForKeys: nil, relativeTo: nil) {
2022-11-15 03:43:29 +05:30
defaults.set(bookmarkData, forKey: bookmarkKey(url))
logger.info("saved bookmark for \(bookmarkKey(url))")
} else {
2022-11-15 03:43:29 +05:30
logger.error("no bookmark data for \(bookmarkKey(url))")
2022-11-10 22:41:28 +05:30
}
}
func loadBookmark(_ url: URL) -> URL? {
2022-12-04 17:51:50 +05:30
var urlForBookmark = url
if let yatteeSanitizedUrl = url.byReplacingYatteeProtocol() {
urlForBookmark = yatteeSanitizedUrl
}
logger.info("loading bookmark for \(bookmarkKey(urlForBookmark))")
2022-11-10 22:41:28 +05:30
2022-12-11 01:38:03 +05:30
guard let defaults = BookmarksCacheModel.shared.defaults else {
logger.error("could not open bookmarks defaults")
2022-11-10 22:41:28 +05:30
return nil
}
2022-12-04 17:51:50 +05:30
if let data = defaults.data(forKey: bookmarkKey(urlForBookmark)) {
do {
var isStale = false
let url = try URL(
resolvingBookmarkData: data,
options: bookmarkResolutionOptions,
relativeTo: nil,
bookmarkDataIsStale: &isStale
)
if isStale {
2022-12-04 17:51:50 +05:30
saveBookmark(urlForBookmark)
}
2022-12-04 17:51:50 +05:30
logger.info("loaded bookmark for \(bookmarkKey(urlForBookmark))")
return url
} catch {
print("Error resolving bookmark:", error)
return nil
}
} else {
2022-12-04 17:51:50 +05:30
logger.warning("could not find bookmark for \(bookmarkKey(urlForBookmark))")
2022-11-10 22:41:28 +05:30
return nil
}
}
2022-11-13 04:31:04 +05:30
func removeBookmark(_ url: URL) {
2022-11-15 03:43:29 +05:30
logger.info("removing bookmark for \(bookmarkKey(url))")
2022-11-13 04:31:04 +05:30
2022-12-11 01:38:03 +05:30
guard let defaults = BookmarksCacheModel.shared.defaults else {
2022-11-13 04:31:04 +05:30
logger.error("could not open bookmarks defaults")
return
}
2022-11-15 03:43:29 +05:30
defaults.removeObject(forKey: bookmarkKey(url))
}
func refreshAll() {
2022-11-18 03:19:08 +05:30
logger.info("refreshing all bookmarks")
2022-11-15 03:43:29 +05:30
allURLs.forEach { url in
if loadBookmark(url) != nil {
logger.info("bookmark for \(url) exists")
} else {
logger.info("bookmark does not exist")
}
}
}
private func bookmarkKey(_ url: URL) -> String {
2022-12-04 17:51:50 +05:30
"\(Self.bookmarkPrefix)\(NSString(string: url.absoluteString).standardizingPath)"
}
private func bookmarkKey(_ url: NSURL) -> String {
"\(Self.bookmarkPrefix)\(url.standardizingPath?.absoluteString ?? "unknown")"
2022-11-15 03:43:29 +05:30
}
private func urlFromBookmark(_ key: String) -> URL? {
if let urlString = key.components(separatedBy: Self.bookmarkPrefix).last {
return URL(string: urlString)
}
return nil
2022-11-13 04:31:04 +05:30
}
2022-11-15 03:43:29 +05:30
private var bookmarkCreationOptions: URL.BookmarkCreationOptions {
2022-11-10 22:41:28 +05:30
#if os(macOS)
return [.withSecurityScope, .securityScopeAllowOnlyReadAccess]
#else
return []
#endif
}
2022-11-15 03:43:29 +05:30
private var bookmarkResolutionOptions: URL.BookmarkResolutionOptions {
2022-11-10 22:41:28 +05:30
#if os(macOS)
return [.withSecurityScope]
#else
return []
#endif
}
}