From ef4b18f622c5b5a39bbd624b48e9b7fa7305d826 Mon Sep 17 00:00:00 2001 From: cranci1 <100066266+cranci1@users.noreply.github.com> Date: Sun, 15 Jun 2025 10:21:22 +0200 Subject: [PATCH] fixed tmp folder issues --- Sora/SoraApp.swift | 19 ++++- .../Downloads/JSController+Downloader.swift | 1 - .../SettingsSubViews/SettingsViewData.swift | 73 +------------------ 3 files changed, 20 insertions(+), 73 deletions(-) diff --git a/Sora/SoraApp.swift b/Sora/SoraApp.swift index f83c741..5b33256 100644 --- a/Sora/SoraApp.swift +++ b/Sora/SoraApp.swift @@ -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 } } -} \ No newline at end of file + + 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") + } + } +} diff --git a/Sora/Utils/JSLoader/Downloads/JSController+Downloader.swift b/Sora/Utils/JSLoader/Downloads/JSController+Downloader.swift index 621152c..9ebe998 100644 --- a/Sora/Utils/JSLoader/Downloads/JSController+Downloader.swift +++ b/Sora/Utils/JSLoader/Downloads/JSController+Downloader.swift @@ -9,7 +9,6 @@ import Foundation import SwiftUI import AVFoundation - struct DownloadRequest { let url: URL let headers: [String: String] diff --git a/Sora/Views/SettingsView/SettingsSubViews/SettingsViewData.swift b/Sora/Views/SettingsView/SettingsSubViews/SettingsViewData.swift index 15e3b6e..13c7685 100644 --- a/Sora/Views/SettingsView/SettingsSubViews/SettingsViewData.swift +++ b/Sora/Views/SettingsView/SettingsSubViews/SettingsViewData.swift @@ -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 {