Skip to content

Commit

Permalink
Postpone the cleanup of temporary files
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanzhong committed Dec 11, 2024
1 parent 0f922b2 commit bd71ac9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// FileManager+Extension.swift
// WhatCopied
//
// Created by cyan on 2024/12/11.
//

import Foundation

public extension FileManager {
func directoryExists(at url: URL) -> Bool {
var isDirectory: ObjCBool = false
let fileExists = fileExists(atPath: url.path, isDirectory: &isDirectory)
return fileExists && isDirectory.boolValue
}
}
4 changes: 4 additions & 0 deletions WhatCopiedMac/Sources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
}
}

func applicationWillTerminate(_ notification: Notification) {
try? FileManager.default.removeItem(at: .previewingDirectory)
}

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
true
}
Expand Down
2 changes: 1 addition & 1 deletion WhatCopiedMac/Sources/Controllers/MainVC+Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension MainVC {
}

var previewingFileURL: URL {
URL.temporaryDirectory.appendingPathComponent(suggestedFileName)
.previewingDirectory.appendingPathComponent(suggestedFileName)
}

func reloadTypes() {
Expand Down
4 changes: 1 addition & 3 deletions WhatCopiedMac/Sources/Controllers/MainVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ final class MainVC: NSViewController {
}

override func endPreviewPanelControl(_ panel: QLPreviewPanel?) {
Task { @MainActor in
try? FileManager.default.removeItem(at: previewingFileURL)
}
// no-op
}
}
25 changes: 25 additions & 0 deletions WhatCopiedMac/Sources/Extensions/URL+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// URL+Extension.swift
// WhatCopied
//
// Created by cyan on 2024/12/11.
//

import Foundation

extension URL {
static var previewingDirectory: URL {
let directory = temporaryDirectory.appendingPathComponent("QuickLook")
let fileManager = FileManager.default

if !fileManager.directoryExists(at: directory) {
do {
try fileManager.createDirectory(at: directory, withIntermediateDirectories: false)
} catch {
Logger.log(.error, "Failed to create previewing directory: \(error.localizedDescription)")
}
}

return directory
}
}

0 comments on commit bd71ac9

Please sign in to comment.