fixed tmp folder issues

This commit is contained in:
cranci1 2025-06-15 10:21:22 +02:00
parent 583075abaa
commit ef4b18f622
3 changed files with 20 additions and 73 deletions

View file

@ -19,6 +19,7 @@ struct SoraApp: App {
if let userAccentColor = UserDefaults.standard.color(forKey: "accentColor") { if let userAccentColor = UserDefaults.standard.color(forKey: "accentColor") {
UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = userAccentColor UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = userAccentColor
} }
clearTmpFolder()
TraktToken.checkAuthenticationStatus { isAuthenticated in TraktToken.checkAuthenticationStatus { isAuthenticated in
if isAuthenticated { if isAuthenticated {
@ -103,4 +104,20 @@ struct SoraApp: App {
break break
} }
} }
}
private func clearTmpFolder() {
let fileManager = FileManager.default
let tmpDirectory = NSTemporaryDirectory()
do {
let tmpURL = URL(fileURLWithPath: tmpDirectory)
let tmpContents = try fileManager.contentsOfDirectory(at: tmpURL, includingPropertiesForKeys: nil)
for url in tmpContents {
try fileManager.removeItem(at: url)
}
} catch {
Logger.shared.log("Failed to clear tmp folder: \(error.localizedDescription)", type: "Error")
}
}
}

View file

@ -9,7 +9,6 @@ import Foundation
import SwiftUI import SwiftUI
import AVFoundation import AVFoundation
struct DownloadRequest { struct DownloadRequest {
let url: URL let url: URL
let headers: [String: String] let headers: [String: String]

View file

@ -141,10 +141,9 @@ struct SettingsViewData: View {
@State private var isCalculatingSize: Bool = false @State private var isCalculatingSize: Bool = false
@State private var cacheSize: Int64 = 0 @State private var cacheSize: Int64 = 0
@State private var documentsSize: Int64 = 0 @State private var documentsSize: Int64 = 0
@State private var downloadsSize: Int64 = 0
enum ActiveAlert { enum ActiveAlert {
case eraseData, removeDocs, removeDownloads, clearCache case eraseData, removeDocs, clearCache
} }
@State private var activeAlert: ActiveAlert = .eraseData @State private var activeAlert: ActiveAlert = .eraseData
@ -154,7 +153,7 @@ struct SettingsViewData: View {
VStack(spacing: 24) { VStack(spacing: 24) {
SettingsSection( SettingsSection(
title: NSLocalizedString("App Storage", comment: ""), title: NSLocalizedString("App Storage", comment: ""),
footer: NSLocalizedString("The app cache helps the app load images faster.\n\nClearing the Documents folder will delete all downloaded modules.\n\nDo not erase App Data unless you understand the consequences — it may cause the app to malfunction.", comment: "") footer: NSLocalizedString("The app cache helps the app load images faster.\n\nClearing the Documents folder will delete all downloaded modules.\n\nErasing the App Data will clears all your settings and data of the app.", comment: "")
) { ) {
VStack(spacing: 0) { VStack(spacing: 0) {
SettingsButtonRow( SettingsButtonRow(
@ -169,18 +168,6 @@ struct SettingsViewData: View {
Divider().padding(.horizontal, 16) Divider().padding(.horizontal, 16)
SettingsButtonRow(
icon: "film",
title: NSLocalizedString("Remove Downloads", comment: ""),
subtitle: formatSize(downloadsSize),
action: {
activeAlert = .removeDownloads
showAlert = true
}
)
Divider().padding(.horizontal, 16)
SettingsButtonRow( SettingsButtonRow(
icon: "doc.text", icon: "doc.text",
title: NSLocalizedString("Remove All Documents", comment: ""), title: NSLocalizedString("Remove All Documents", comment: ""),
@ -209,7 +196,6 @@ struct SettingsViewData: View {
.onAppear { .onAppear {
calculateCacheSize() calculateCacheSize()
updateSizes() updateSizes()
calculateDownloadsSize()
} }
.alert(isPresented: $showAlert) { .alert(isPresented: $showAlert) {
switch activeAlert { switch activeAlert {
@ -231,15 +217,6 @@ struct SettingsViewData: View {
}, },
secondaryButton: .cancel() secondaryButton: .cancel()
) )
case .removeDownloads:
return Alert(
title: Text(NSLocalizedString("Remove Downloaded Media", comment: "")),
message: Text(NSLocalizedString("Are you sure you want to remove all downloaded media files (.mov, .mp4, .pkg)? This action cannot be undone.", comment: "")),
primaryButton: .destructive(Text(NSLocalizedString("Remove", comment: ""))) {
removeDownloadedMedia()
},
secondaryButton: .cancel()
)
case .clearCache: case .clearCache:
return Alert( return Alert(
title: Text(NSLocalizedString("Clear Cache", comment: "")), title: Text(NSLocalizedString("Clear Cache", comment: "")),
@ -286,17 +263,6 @@ struct SettingsViewData: View {
} }
} }
func calculateDownloadsSize() {
DispatchQueue.global(qos: .background).async {
if let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let size = calculateMediaFilesSize(in: documentsURL)
DispatchQueue.main.async {
self.downloadsSize = size
}
}
}
}
func calculateMediaFilesSize(in directory: URL) -> Int64 { func calculateMediaFilesSize(in directory: URL) -> Int64 {
let fileManager = FileManager.default let fileManager = FileManager.default
var totalSize: Int64 = 0 var totalSize: Int64 = 0
@ -337,47 +303,12 @@ struct SettingsViewData: View {
Logger.shared.log("Cache cleared successfully!", type: "General") Logger.shared.log("Cache cleared successfully!", type: "General")
calculateCacheSize() calculateCacheSize()
updateSizes() updateSizes()
calculateDownloadsSize()
} }
} catch { } catch {
Logger.shared.log("Failed to clear cache.", type: "Error") Logger.shared.log("Failed to clear cache.", type: "Error")
} }
} }
func removeDownloadedMedia() {
let fileManager = FileManager.default
let mediaExtensions = [".mov", ".mp4", ".pkg"]
if let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first {
removeMediaFiles(in: documentsURL, extensions: mediaExtensions)
Logger.shared.log("Downloaded media files removed", type: "General")
updateSizes()
calculateDownloadsSize()
}
}
func removeMediaFiles(in directory: URL, extensions: [String]) {
let fileManager = FileManager.default
do {
let contents = try fileManager.contentsOfDirectory(at: directory, includingPropertiesForKeys: [.isDirectoryKey])
for url in contents {
let resourceValues = try url.resourceValues(forKeys: [.isDirectoryKey])
if resourceValues.isDirectory == true {
removeMediaFiles(in: url, extensions: extensions)
} else {
let fileExtension = ".\(url.pathExtension.lowercased())"
if extensions.contains(fileExtension) {
try fileManager.removeItem(at: url)
Logger.shared.log("Removed media file: \(url.lastPathComponent)", type: "General")
}
}
}
} catch {
Logger.shared.log("Error removing media files in \(directory.path): \(error)", type: "Error")
}
}
func removeAllFilesInDocuments() { func removeAllFilesInDocuments() {
let fileManager = FileManager.default let fileManager = FileManager.default
if let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first { if let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first {