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") {
UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = userAccentColor
}
clearTmpFolder()
TraktToken.checkAuthenticationStatus { isAuthenticated in
if isAuthenticated {
@ -103,4 +104,20 @@ struct SoraApp: App {
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 AVFoundation
struct DownloadRequest {
let url: URL
let headers: [String: String]

View file

@ -141,10 +141,9 @@ struct SettingsViewData: View {
@State private var isCalculatingSize: Bool = false
@State private var cacheSize: Int64 = 0
@State private var documentsSize: Int64 = 0
@State private var downloadsSize: Int64 = 0
enum ActiveAlert {
case eraseData, removeDocs, removeDownloads, clearCache
case eraseData, removeDocs, clearCache
}
@State private var activeAlert: ActiveAlert = .eraseData
@ -154,7 +153,7 @@ struct SettingsViewData: View {
VStack(spacing: 24) {
SettingsSection(
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) {
SettingsButtonRow(
@ -169,18 +168,6 @@ struct SettingsViewData: View {
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(
icon: "doc.text",
title: NSLocalizedString("Remove All Documents", comment: ""),
@ -209,7 +196,6 @@ struct SettingsViewData: View {
.onAppear {
calculateCacheSize()
updateSizes()
calculateDownloadsSize()
}
.alert(isPresented: $showAlert) {
switch activeAlert {
@ -231,15 +217,6 @@ struct SettingsViewData: View {
},
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:
return Alert(
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 {
let fileManager = FileManager.default
var totalSize: Int64 = 0
@ -337,47 +303,12 @@ struct SettingsViewData: View {
Logger.shared.log("Cache cleared successfully!", type: "General")
calculateCacheSize()
updateSizes()
calculateDownloadsSize()
}
} catch {
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() {
let fileManager = FileManager.default
if let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first {