mirror of
https://github.com/FBukovina/Atoll.git
synced 2026-10-27 15:14:33 +00:00
103 lines
3.6 KiB
Swift
103 lines
3.6 KiB
Swift
/*
|
|
* Atoll (DynamicIsland)
|
|
* Copyright (C) 2024-2026 Atoll Contributors
|
|
*
|
|
* Originally from boring.notch project
|
|
* Modified and adapted for Atoll (DynamicIsland)
|
|
* See NOTICE for details.
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import AppKit
|
|
import Foundation
|
|
import UniformTypeIdentifiers
|
|
|
|
struct ShelfDropService {
|
|
static func items(from providers: [NSItemProvider]) async -> [ShelfItem] {
|
|
var results: [ShelfItem] = []
|
|
|
|
for provider in providers {
|
|
if let item = await processProvider(provider) {
|
|
results.append(item)
|
|
}
|
|
}
|
|
|
|
return results
|
|
}
|
|
|
|
static func items(from fileURLs: [URL]) async -> [ShelfItem] {
|
|
var results: [ShelfItem] = []
|
|
let fileManager = FileManager.default
|
|
|
|
for fileURL in fileURLs {
|
|
let resolvedURL = fileURL.standardizedFileURL
|
|
guard resolvedURL.isFileURL else { continue }
|
|
guard fileManager.fileExists(atPath: resolvedURL.path) else {
|
|
NSLog("⚠️ Ignoring non-existent file URL: \(resolvedURL.path)")
|
|
continue
|
|
}
|
|
|
|
if let bookmark = createBookmark(for: resolvedURL) {
|
|
results.append(await ShelfItem(kind: .file(bookmark: bookmark), isTemporary: false))
|
|
}
|
|
}
|
|
|
|
return results
|
|
}
|
|
|
|
private static func processProvider(_ provider: NSItemProvider) async -> ShelfItem? {
|
|
if let actualFileURL = await provider.extractFileURL() {
|
|
if let bookmark = createBookmark(for: actualFileURL) {
|
|
return await ShelfItem(kind: .file(bookmark: bookmark), isTemporary: false)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
if let url = await provider.extractURL() {
|
|
if url.isFileURL {
|
|
if let bookmark = createBookmark(for: url) {
|
|
return await ShelfItem(kind: .file(bookmark: bookmark), isTemporary: false)
|
|
}
|
|
} else {
|
|
return await ShelfItem(kind: .link(url: url), isTemporary: false)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
if let text = await provider.extractText() {
|
|
return await ShelfItem(kind: .text(string: text), isTemporary: false)
|
|
}
|
|
|
|
if let data = await provider.loadData() {
|
|
if let tempDataURL = await TemporaryFileStorageService.shared.createTempFile(for: .data(data, suggestedName: provider.suggestedName)),
|
|
let bookmark = createBookmark(for: tempDataURL) {
|
|
return await ShelfItem(kind: .file(bookmark: bookmark), isTemporary: true)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
if let fileURL = await provider.extractItem() {
|
|
if let bookmark = createBookmark(for: fileURL) {
|
|
return await ShelfItem(kind: .file(bookmark: bookmark), isTemporary: false)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
private static func createBookmark(for url: URL) -> Data? {
|
|
return (try? Bookmark(url: url))?.data
|
|
}
|
|
}
|