Atoll/DynamicIsland/components/Shelf/DropItem.swift
2025-07-13 20:26:03 +05:30

58 lines
1.4 KiB
Swift

//
// TrayDrop+DropItem.swift
// TrayDrop
//
// Created by 秋星桥 on 2024/7/8.
//
import Cocoa
import Foundation
import QuickLook
extension TrayDrop {
struct DropItem: Identifiable, Codable, Equatable, Hashable {
let id: UUID
let fileName: String
let size: Int
let copiedDate: Date
let workspacePreviewImageData: Data
init(url: URL) throws {
assert(!Thread.isMainThread)
id = UUID()
fileName = url.lastPathComponent
size = (try? url.resourceValues(forKeys: [.fileSizeKey]))?.fileSize ?? 0
copiedDate = Date()
workspacePreviewImageData = url.snapshotPreview().pngRepresentation
try FileManager.default.createDirectory(
at: storageURL.deletingLastPathComponent(),
withIntermediateDirectories: true
)
try FileManager.default.copyItem(at: url, to: storageURL)
}
}
}
extension TrayDrop.DropItem {
static let mainDir = "CopiedItems"
var storageURL: URL {
documentsDirectory
.appendingPathComponent(Self.mainDir)
.appendingPathComponent(id.uuidString)
.appendingPathComponent(fileName)
}
var workspacePreviewImage: NSImage {
.init(data: workspacePreviewImageData) ?? .init()
}
var shouldClean: Bool { // TODO: In the future clean if old
return false
}
}