From 078e48d31600267e04bd23c5d3e96fbe41360b28 Mon Sep 17 00:00:00 2001 From: kingbri Date: Sun, 16 Jun 2024 15:37:00 -0500 Subject: [PATCH] Treewide: Cleanup and rename Signed-off-by: kingbri --- Ferrite.xcodeproj/project.pbxproj | 8 +-- Ferrite/API/AllDebridWrapper.swift | 33 ++++++----- Ferrite/API/OffCloudWrapper.swift | 42 ++++++-------- Ferrite/API/PremiumizeWrapper.swift | 10 ++-- Ferrite/API/RealDebridWrapper.swift | 55 +++++++++---------- Ferrite/API/TorBoxWrapper.swift | 52 ++++++++---------- Ferrite/Models/DebridModels.swift | 6 +- Ferrite/Protocols/Debrid.swift | 12 ++-- Ferrite/ViewModels/DebridManager.swift | 16 +++--- ...orrentView.swift => CloudMagnetView.swift} | 30 +++++----- .../Library/DebridCloudView.swift | 4 +- .../SearchResult/SearchResultButtonView.swift | 2 +- 12 files changed, 128 insertions(+), 142 deletions(-) rename Ferrite/Views/ComponentViews/Library/Cloud/{CloudTorrentView.swift => CloudMagnetView.swift} (77%) diff --git a/Ferrite.xcodeproj/project.pbxproj b/Ferrite.xcodeproj/project.pbxproj index 7d5c314..d13f4ba 100644 --- a/Ferrite.xcodeproj/project.pbxproj +++ b/Ferrite.xcodeproj/project.pbxproj @@ -139,7 +139,7 @@ 0CB6516528C5A5D700DCA721 /* InlinedList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB6516428C5A5D700DCA721 /* InlinedList.swift */; }; 0CB6516A28C5B4A600DCA721 /* InlineHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB6516928C5B4A600DCA721 /* InlineHeader.swift */; }; 0CB725322C123E6F0047FC0B /* CloudDownloadView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB725312C123E6F0047FC0B /* CloudDownloadView.swift */; }; - 0CB725342C123E760047FC0B /* CloudTorrentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB725332C123E760047FC0B /* CloudTorrentView.swift */; }; + 0CB725342C123E760047FC0B /* CloudMagnetView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CB725332C123E760047FC0B /* CloudMagnetView.swift */; }; 0CBAB83628D12ED500AC903E /* DisableInteraction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CBAB83528D12ED500AC903E /* DisableInteraction.swift */; }; 0CBC76FD288D914F0054BE44 /* BatchChoiceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CBC76FC288D914F0054BE44 /* BatchChoiceView.swift */; }; 0CBC76FF288DAAD00054BE44 /* NavigationViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CBC76FE288DAAD00054BE44 /* NavigationViewModel.swift */; }; @@ -294,7 +294,7 @@ 0CB6516428C5A5D700DCA721 /* InlinedList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InlinedList.swift; sourceTree = ""; }; 0CB6516928C5B4A600DCA721 /* InlineHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InlineHeader.swift; sourceTree = ""; }; 0CB725312C123E6F0047FC0B /* CloudDownloadView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloudDownloadView.swift; sourceTree = ""; }; - 0CB725332C123E760047FC0B /* CloudTorrentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloudTorrentView.swift; sourceTree = ""; }; + 0CB725332C123E760047FC0B /* CloudMagnetView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloudMagnetView.swift; sourceTree = ""; }; 0CBAB83528D12ED500AC903E /* DisableInteraction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisableInteraction.swift; sourceTree = ""; }; 0CBC76FC288D914F0054BE44 /* BatchChoiceView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatchChoiceView.swift; sourceTree = ""; }; 0CBC76FE288DAAD00054BE44 /* NavigationViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationViewModel.swift; sourceTree = ""; }; @@ -430,7 +430,7 @@ isa = PBXGroup; children = ( 0CB725312C123E6F0047FC0B /* CloudDownloadView.swift */, - 0CB725332C123E760047FC0B /* CloudTorrentView.swift */, + 0CB725332C123E760047FC0B /* CloudMagnetView.swift */, ); path = Cloud; sourceTree = ""; @@ -896,7 +896,7 @@ 0C84FCE529E4B43200B0DFE4 /* SelectedDebridFilterView.swift in Sources */, 0CA148EC288903F000DE2211 /* ContentView.swift in Sources */, 0CC389532970AD900066D06F /* Action+CoreDataClass.swift in Sources */, - 0CB725342C123E760047FC0B /* CloudTorrentView.swift in Sources */, + 0CB725342C123E760047FC0B /* CloudMagnetView.swift in Sources */, 0C03EB72296F619900162E9A /* PluginList+CoreDataProperties.swift in Sources */, 0C95D8D828A55B03005E22B3 /* DefaultActionPickerView.swift in Sources */, 0C44E2AF28D52E8A007711AE /* BackupsView.swift in Sources */, diff --git a/Ferrite/API/AllDebridWrapper.swift b/Ferrite/API/AllDebridWrapper.swift index a5a31b8..7f5f3b9 100644 --- a/Ferrite/API/AllDebridWrapper.swift +++ b/Ferrite/API/AllDebridWrapper.swift @@ -28,7 +28,7 @@ class AllDebrid: PollingDebridSource, ObservableObject { @Published var IAValues: [DebridIA] = [] @Published var cloudDownloads: [DebridCloudDownload] = [] - @Published var cloudTorrents: [DebridCloudTorrent] = [] + @Published var cloudMagnets: [DebridCloudMagnet] = [] var cloudTTL: Double = 0.0 private let baseApiUrl = "https://api.alldebrid.com/v4" @@ -217,8 +217,8 @@ class AllDebrid: PollingDebridSource, ObservableObject { func getRestrictedFile(magnet: Magnet, ia: DebridIA?, iaFile: DebridIAFile?) async throws -> (restrictedFile: DebridIAFile?, newIA: DebridIA?) { let selectedMagnetId: String - if let existingMagnet = cloudTorrents.first(where: { $0.hash == magnet.hash && $0.status == "Ready" }) { - selectedMagnetId = existingMagnet.torrentId + if let existingMagnet = cloudMagnets.first(where: { $0.hash == magnet.hash && $0.status == "Ready" }) { + selectedMagnetId = existingMagnet.cloudMagnetId } else { let magnetId = try await addMagnet(magnet: magnet) selectedMagnetId = String(magnetId) @@ -269,10 +269,10 @@ class AllDebrid: PollingDebridSource, ObservableObject { let rawResponse = try jsonDecoder.decode(ADResponse.self, from: data).data // Better to fetch no link at all than the wrong link - if let torrentFile = rawResponse.magnets[safe: 0]?.links[safe: selectedIndex ?? -1] { - return DebridIAFile(fileId: 0, name: torrentFile.filename, streamUrlString: torrentFile.link) + if let cloudMagnetFile = rawResponse.magnets[safe: 0]?.links[safe: selectedIndex ?? -1] { + return DebridIAFile(fileId: 0, name: cloudMagnetFile.filename, streamUrlString: cloudMagnetFile.link) } else { - throw DebridError.EmptyTorrents + throw DebridError.EmptyUserMagnets } } @@ -300,32 +300,31 @@ class AllDebrid: PollingDebridSource, ObservableObject { // MARK: - Cloud methods - // Referred to as "User magnets" in AllDebrid's API - func getUserTorrents() async throws { + func getUserMagnets() async throws { var request = URLRequest(url: try buildRequestURL(urlString: "\(baseApiUrl)/magnet/status")) let data = try await performRequest(request: &request, requestName: #function) let rawResponse = try jsonDecoder.decode(ADResponse.self, from: data).data - cloudTorrents = rawResponse.magnets.map { magnetResponse in - DebridCloudTorrent( - torrentId: String(magnetResponse.id), + cloudMagnets = rawResponse.magnets.map { magnetResponse in + DebridCloudMagnet( + cloudMagnetId: String(magnetResponse.id), source: self.id, fileName: magnetResponse.filename, - status: magnetResponse.status, + status: magnetResponse.status == "Ready" ? "downloaded" : magnetResponse.status, hash: magnetResponse.hash, links: magnetResponse.links.map(\.link) ) } } - func deleteTorrent(torrentId: String?) async throws { - guard let torrentId else { - throw DebridError.FailedRequest(description: "The torrentID \(String(describing: torrentId)) is invalid") + func deleteUserMagnet(cloudMagnetId: String?) async throws { + guard let cloudMagnetId else { + throw DebridError.FailedRequest(description: "The cloud magnetID \(String(describing: cloudMagnetId)) is invalid") } let queryItems = [ - URLQueryItem(name: "id", value: torrentId) + URLQueryItem(name: "id", value: cloudMagnetId) ] var request = URLRequest(url: try buildRequestURL(urlString: "\(baseApiUrl)/magnet/delete", queryItems: queryItems)) @@ -352,7 +351,7 @@ class AllDebrid: PollingDebridSource, ObservableObject { } // The downloadId is actually the download link - func deleteDownload(downloadId: String) async throws { + func deleteUserDownload(downloadId: String) async throws { let queryItems = [ URLQueryItem(name: "link", value: downloadId) ] diff --git a/Ferrite/API/OffCloudWrapper.swift b/Ferrite/API/OffCloudWrapper.swift index 9785d08..45d4b93 100644 --- a/Ferrite/API/OffCloudWrapper.swift +++ b/Ferrite/API/OffCloudWrapper.swift @@ -7,19 +7,13 @@ import Foundation -// Torrents: /cloud/history -// IA: /cache (JSON array of hashes) -// Add Magnet: /cloud (URL param in JSON body) -// Get files/unrestrict: /cloud/explore/\(requestId) -// Delete torrent (website URL, not API URL): /cloud/remove/\(torrentId) - class OffCloud: DebridSource, ObservableObject { let id = "OffCloud" let abbreviation = "OC" let website = "https://offcloud.com" let description: String? = "OffCloud is a debrid service that is used for downloads and media playback. " + "You must pay to access this service. \n\n" + - "This service does not inform if a torrent is a batch before downloading." + "This service does not inform if a magnet link is a batch before downloading." @Published var authProcessing: Bool = false var isLoggedIn: Bool { @@ -36,7 +30,7 @@ class OffCloud: DebridSource, ObservableObject { @Published var IAValues: [DebridIA] = [] @Published var cloudDownloads: [DebridCloudDownload] = [] - @Published var cloudTorrents: [DebridCloudTorrent] = [] + @Published var cloudMagnets: [DebridCloudMagnet] = [] var cloudTTL: Double = 0.0 private let baseApiUrl = "https://offcloud.com/api" @@ -140,11 +134,11 @@ class OffCloud: DebridSource, ObservableObject { // Cloud in OffCloud's API func getRestrictedFile(magnet: Magnet, ia: DebridIA?, iaFile: DebridIAFile?) async throws -> (restrictedFile: DebridIAFile?, newIA: DebridIA?) { - let selectedTorrent: DebridCloudTorrent + let selectedMagnet: DebridCloudMagnet - // Don't queue a new job if the torrent already exists - if let existingTorrent = cloudTorrents.first(where: { $0.hash == magnet.hash && $0.status == "downloaded" }) { - selectedTorrent = existingTorrent + // Don't queue a new job if the magnet already exists in the user's account + if let existingCloudMagnet = cloudMagnets.first(where: { $0.hash == magnet.hash && $0.status == "downloaded" }) { + selectedMagnet = existingCloudMagnet } else { let cloudDownloadResponse = try await offcloudDownload(magnet: magnet) @@ -152,8 +146,8 @@ class OffCloud: DebridSource, ObservableObject { throw DebridError.IsCaching } - selectedTorrent = DebridCloudTorrent( - torrentId: cloudDownloadResponse.requestId, + selectedMagnet = DebridCloudMagnet( + cloudMagnetId: cloudDownloadResponse.requestId, source: id, fileName: cloudDownloadResponse.fileName, status: cloudDownloadResponse.status, @@ -162,7 +156,7 @@ class OffCloud: DebridSource, ObservableObject { ) } - let cloudExploreLinks = try await cloudExplore(requestId: selectedTorrent.torrentId) + let cloudExploreLinks = try await cloudExplore(requestId: selectedMagnet.cloudMagnetId) if cloudExploreLinks.count > 1 { var copiedIA = ia @@ -183,7 +177,7 @@ class OffCloud: DebridSource, ObservableObject { } else if let exploreLink = cloudExploreLinks.first { let restrictedFile = DebridIAFile( fileId: 0, - name: selectedTorrent.fileName, + name: selectedMagnet.fileName, streamUrlString: exploreLink ) @@ -235,21 +229,21 @@ class OffCloud: DebridSource, ObservableObject { link } - func deleteDownload(downloadId: String) {} + func deleteUserDownload(downloadId: String) {} - func getUserTorrents() async throws { + func getUserMagnets() async throws { var request = URLRequest(url: try buildRequestURL(urlString: "\(baseApiUrl)/cloud/history")) let data = try await performRequest(request: &request, requestName: "cloudHistory") let rawResponse = try jsonDecoder.decode([CloudHistoryResponse].self, from: data) - cloudTorrents = rawResponse.compactMap { cloudHistory in + cloudMagnets = rawResponse.compactMap { cloudHistory in guard let magnetHash = Magnet(hash: nil, link: cloudHistory.originalLink).hash else { return nil } - return DebridCloudTorrent( - torrentId: cloudHistory.requestId, + return DebridCloudMagnet( + cloudMagnetId: cloudHistory.requestId, source: self.id, fileName: cloudHistory.fileName, status: cloudHistory.status, @@ -260,12 +254,12 @@ class OffCloud: DebridSource, ObservableObject { } // Uses the base website because this isn't present in the API path but still works like the API? - func deleteTorrent(torrentId: String?) async throws { - guard let torrentId else { + func deleteUserMagnet(cloudMagnetId: String?) async throws { + guard let cloudMagnetId else { throw DebridError.InvalidPostBody } - var request = URLRequest(url: try buildRequestURL(urlString: "\(website)/cloud/remove/\(torrentId)")) + var request = URLRequest(url: try buildRequestURL(urlString: "\(website)/cloud/remove/\(cloudMagnetId)")) try await performRequest(request: &request, requestName: "cloudRemove") } } diff --git a/Ferrite/API/PremiumizeWrapper.swift b/Ferrite/API/PremiumizeWrapper.swift index 3f6d478..cb6eff1 100644 --- a/Ferrite/API/PremiumizeWrapper.swift +++ b/Ferrite/API/PremiumizeWrapper.swift @@ -29,7 +29,7 @@ class Premiumize: OAuthDebridSource, ObservableObject { @Published var IAValues: [DebridIA] = [] @Published var cloudDownloads: [DebridCloudDownload] = [] - @Published var cloudTorrents: [DebridCloudTorrent] = [] + @Published var cloudMagnets: [DebridCloudMagnet] = [] var cloudTTL: Double = 0.0 private let baseAuthUrl = "https://www.premiumize.me/authorize" @@ -355,7 +355,7 @@ class Premiumize: OAuthDebridSource, ObservableObject { try await itemDetails(itemID: link).link } - func deleteDownload(downloadId: String) async throws { + func deleteUserDownload(downloadId: String) async throws { var request = URLRequest(url: URL(string: "\(baseApiUrl)/item/delete")!) request.httpMethod = "POST" request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") @@ -368,8 +368,8 @@ class Premiumize: OAuthDebridSource, ObservableObject { try await performRequest(request: &request, requestName: #function) } - // No user torrents for Premiumize - func getUserTorrents() {} + // No user magnets for Premiumize + func getUserMagnets() {} - func deleteTorrent(torrentId: String?) {} + func deleteUserMagnet(cloudMagnetId: String?) {} } diff --git a/Ferrite/API/RealDebridWrapper.swift b/Ferrite/API/RealDebridWrapper.swift index bd65250..9eb3195 100644 --- a/Ferrite/API/RealDebridWrapper.swift +++ b/Ferrite/API/RealDebridWrapper.swift @@ -30,7 +30,7 @@ class RealDebrid: PollingDebridSource, ObservableObject { @Published var IAValues: [DebridIA] = [] @Published var cloudDownloads: [DebridCloudDownload] = [] - @Published var cloudTorrents: [DebridCloudTorrent] = [] + @Published var cloudMagnets: [DebridCloudMagnet] = [] var cloudTTL: Double = 0.0 private let baseAuthUrl = "https://api.real-debrid.com/oauth/v2" @@ -261,7 +261,6 @@ class RealDebrid: PollingDebridSource, ObservableObject { let data = try await performRequest(request: &request, requestName: #function) - // Does not account for torrent packs at the moment let rawResponseDict = try jsonDecoder.decode([String: InstantAvailabilityResponse].self, from: data) for (hash, response) in rawResponseDict { @@ -320,9 +319,9 @@ class RealDebrid: PollingDebridSource, ObservableObject { var selectedMagnetId = "" do { - // Don't queue a new job if the torrent already exists - if let existingTorrent = cloudTorrents.first(where: { $0.hash == magnet.hash && $0.status == "downloaded" }) { - selectedMagnetId = existingTorrent.torrentId + // Don't queue a new job if the magnet already exists in the user's library + if let existingCloudMagnet = cloudMagnets.first(where: { $0.hash == magnet.hash && $0.status == "downloaded" }) { + selectedMagnetId = existingCloudMagnet.cloudMagnetId } else { selectedMagnetId = try await addMagnet(magnet: magnet) @@ -330,15 +329,15 @@ class RealDebrid: PollingDebridSource, ObservableObject { } // RealDebrid has 1 as the first ID for a file - let torrentFile = try await torrentInfo( + let restrictedFile = try await torrentInfo( debridID: selectedMagnetId, selectedFileId: iaFile?.fileId ?? 1 ) - return (torrentFile, nil) + return (restrictedFile, nil) } catch { - if case DebridError.EmptyTorrents = error, !selectedMagnetId.isEmpty { - try? await deleteTorrent(torrentId: selectedMagnetId) + if case DebridError.EmptyUserMagnets = error, !selectedMagnetId.isEmpty { + try? await deleteUserMagnet(cloudMagnetId: selectedMagnetId) } // Re-raise the error to the calling function @@ -396,17 +395,17 @@ class RealDebrid: PollingDebridSource, ObservableObject { let filteredFiles = rawResponse.files.filter { $0.selected == 1 } let linkIndex = filteredFiles.firstIndex(where: { $0.id == selectedFileId }) - // Let the user know if a torrent is downloading - if let torrentLink = rawResponse.links[safe: linkIndex ?? -1], rawResponse.status == "downloaded" { + // Let the user know if a magnet is downloading + if let cloudMagnetLink = rawResponse.links[safe: linkIndex ?? -1], rawResponse.status == "downloaded" { return DebridIAFile( fileId: 0, name: rawResponse.filename, - streamUrlString: torrentLink + streamUrlString: cloudMagnetLink ) } else if rawResponse.status == "downloading" || rawResponse.status == "queued" { throw DebridError.IsCaching } else { - throw DebridError.EmptyTorrents + throw DebridError.EmptyUserMagnets } } @@ -429,15 +428,15 @@ class RealDebrid: PollingDebridSource, ObservableObject { // MARK: - Cloud methods - // Gets the user's torrent library - func getUserTorrents() async throws { + // Gets the user's cloud magnet library + func getUserMagnets() async throws { var request = URLRequest(url: URL(string: "\(baseApiUrl)/torrents")!) let data = try await performRequest(request: &request, requestName: #function) let rawResponse = try jsonDecoder.decode([UserTorrentsResponse].self, from: data) - cloudTorrents = rawResponse.map { response in - DebridCloudTorrent( - torrentId: response.id, + cloudMagnets = rawResponse.map { response in + DebridCloudMagnet( + cloudMagnetId: response.id, source: self.id, fileName: response.filename, status: response.status, @@ -447,21 +446,21 @@ class RealDebrid: PollingDebridSource, ObservableObject { } } - // Deletes a torrent download from RD - func deleteTorrent(torrentId: String?) async throws { + // Deletes a magnet download from RD + func deleteUserMagnet(cloudMagnetId: String?) async throws { let deleteId: String - if let torrentId { - deleteId = torrentId + if let cloudMagnetId { + deleteId = cloudMagnetId } else { - // Refresh the torrent cloud + // Refresh the user magnet list // The first file is the currently caching one - let _ = try await getUserTorrents() - guard let firstTorrent = cloudTorrents[safe: -1] else { - throw DebridError.EmptyTorrents + let _ = try await getUserMagnets() + guard let firstCloudMagnet = cloudMagnets[safe: -1] else { + throw DebridError.EmptyUserMagnets } - deleteId = firstTorrent.torrentId + deleteId = firstCloudMagnet.cloudMagnetId } var request = URLRequest(url: URL(string: "\(baseApiUrl)/torrents/delete/\(deleteId)")!) @@ -486,7 +485,7 @@ class RealDebrid: PollingDebridSource, ObservableObject { link } - func deleteDownload(downloadId: String) async throws { + func deleteUserDownload(downloadId: String) async throws { var request = URLRequest(url: URL(string: "\(baseApiUrl)/downloads/delete/\(downloadId)")!) request.httpMethod = "DELETE" diff --git a/Ferrite/API/TorBoxWrapper.swift b/Ferrite/API/TorBoxWrapper.swift index 7abdf33..e17a662 100644 --- a/Ferrite/API/TorBoxWrapper.swift +++ b/Ferrite/API/TorBoxWrapper.swift @@ -7,12 +7,6 @@ import Foundation -// Torrents: /torrents/mylist -// IA: /torrents/checkcached -// Add Magnet: /torrents/createtorrent -// Delete torrent: /torrents/controltorrent -// Unrestrict: /torrents/requestdl - class TorBox: DebridSource, ObservableObject { let id = "TorBox" let abbreviation = "TB" @@ -35,7 +29,7 @@ class TorBox: DebridSource, ObservableObject { @Published var IAValues: [DebridIA] = [] @Published var cloudDownloads: [DebridCloudDownload] = [] - @Published var cloudTorrents: [DebridCloudTorrent] = [] + @Published var cloudMagnets: [DebridCloudMagnet] = [] var cloudTTL: Double = 0.0 private let baseApiUrl = "https://api.torbox.app/v1/api" @@ -148,22 +142,22 @@ class TorBox: DebridSource, ObservableObject { // MARK: - Downloading func getRestrictedFile(magnet: Magnet, ia: DebridIA?, iaFile: DebridIAFile?) async throws -> (restrictedFile: DebridIAFile?, newIA: DebridIA?) { - let torrentId = try await createTorrent(magnet: magnet) - let torrentList = try await myTorrentList() - guard let filteredTorrent = torrentList.first(where: { $0.id == torrentId }) else { - throw DebridError.FailedRequest(description: "A torrent wasn't found. Are you sure it's cached?") + let cloudMagnetId = try await createTorrent(magnet: magnet) + let cloudMagnetList = try await myTorrentList() + guard let filteredCloudMagnet = cloudMagnetList.first(where: { $0.id == cloudMagnetId }) else { + throw DebridError.FailedRequest(description: "Could not find a cached magnet. Are you sure it's cached?") } - // If the torrent isn't saved, it's considered as caching - guard filteredTorrent.downloadState == "cached" || filteredTorrent.downloadState == "completed" else { + // If the user magnet isn't saved, it's considered as caching + guard filteredCloudMagnet.downloadState == "cached" || filteredCloudMagnet.downloadState == "completed" else { throw DebridError.IsCaching } - guard let torrentFile = filteredTorrent.files[safe: iaFile?.fileId ?? 0] else { - throw DebridError.EmptyTorrents + guard let cloudMagnetFile = filteredCloudMagnet.files[safe: iaFile?.fileId ?? 0] else { + throw DebridError.EmptyUserMagnets } - let restrictedFile = DebridIAFile(fileId: torrentFile.id, name: torrentFile.name, streamUrlString: String(torrentId)) + let restrictedFile = DebridIAFile(fileId: cloudMagnetFile.id, name: cloudMagnetFile.name, streamUrlString: String(cloudMagnetId)) return (restrictedFile, nil) } @@ -235,26 +229,26 @@ class TorBox: DebridSource, ObservableObject { link } - func deleteDownload(downloadId: String) {} + func deleteUserDownload(downloadId: String) {} - func getUserTorrents() async throws { - let torrentList = try await myTorrentList() - cloudTorrents = torrentList.map { torrent in + func getUserMagnets() async throws { + let cloudMagnetList = try await myTorrentList() + cloudMagnets = cloudMagnetList.map { cloudMagnet in // Only need one link to force a green badge - DebridCloudTorrent( - torrentId: String(torrent.id), + DebridCloudMagnet( + cloudMagnetId: String(cloudMagnet.id), source: self.id, - fileName: torrent.name, - status: torrent.downloadState == "cached" || torrent.downloadState == "completed" ? "downloaded" : torrent.downloadState, - hash: torrent.hash, - links: torrent.files.map { String($0.id) } + fileName: cloudMagnet.name, + status: cloudMagnet.downloadState == "cached" || cloudMagnet.downloadState == "completed" ? "downloaded" : cloudMagnet.downloadState, + hash: cloudMagnet.hash, + links: cloudMagnet.files.map { String($0.id) } ) } } - func deleteTorrent(torrentId: String?) async throws { - guard let torrentId else { + func deleteUserMagnet(cloudMagnetId: String?) async throws { + guard let cloudMagnetId else { throw DebridError.InvalidPostBody } @@ -262,7 +256,7 @@ class TorBox: DebridSource, ObservableObject { request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") - let body = ControlTorrentRequest(torrentId: torrentId, operation: "Delete") + let body = ControlTorrentRequest(torrentId: cloudMagnetId, operation: "Delete") request.httpBody = try jsonEncoder.encode(body) try await performRequest(request: &request, requestName: "controltorrent") diff --git a/Ferrite/Models/DebridModels.swift b/Ferrite/Models/DebridModels.swift index 60ca4b8..7ce96ca 100644 --- a/Ferrite/Models/DebridModels.swift +++ b/Ferrite/Models/DebridModels.swift @@ -35,8 +35,8 @@ struct DebridCloudDownload: Hashable, Sendable { let link: String } -struct DebridCloudTorrent: Hashable, Sendable { - let torrentId: String +struct DebridCloudMagnet: Hashable, Sendable { + let cloudMagnetId: String let source: String let fileName: String let status: String @@ -50,7 +50,7 @@ enum DebridError: Error { case InvalidResponse case InvalidToken case EmptyData - case EmptyTorrents + case EmptyUserMagnets case IsCaching case FailedRequest(description: String) case AuthQuery(description: String) diff --git a/Ferrite/Protocols/Debrid.swift b/Ferrite/Protocols/Debrid.swift index 5ead94e..d7ecbff 100644 --- a/Ferrite/Protocols/Debrid.swift +++ b/Ferrite/Protocols/Debrid.swift @@ -27,7 +27,7 @@ protocol DebridSource: AnyObservableObject { // Cloud variables var cloudDownloads: [DebridCloudDownload] { get set } - var cloudTorrents: [DebridCloudTorrent] { get set } + var cloudMagnets: [DebridCloudMagnet] { get set } var cloudTTL: Double { get set } // Common authentication functions @@ -39,7 +39,7 @@ protocol DebridSource: AnyObservableObject { // Fetches a download link from a source // Include the instant availability information with the args - // Torrents also checked here + // Cloud magnets also checked here func getRestrictedFile(magnet: Magnet, ia: DebridIA?, iaFile: DebridIAFile?) async throws -> (restrictedFile: DebridIAFile?, newIA: DebridIA?) // Unrestricts a locked file @@ -48,11 +48,11 @@ protocol DebridSource: AnyObservableObject { // User downloads functions func getUserDownloads() async throws func checkUserDownloads(link: String) async throws -> String? - func deleteDownload(downloadId: String) async throws + func deleteUserDownload(downloadId: String) async throws - // User torrent functions - func getUserTorrents() async throws - func deleteTorrent(torrentId: String?) async throws + // User magnet functions + func getUserMagnets() async throws + func deleteUserMagnet(cloudMagnetId: String?) async throws } extension DebridSource { diff --git a/Ferrite/ViewModels/DebridManager.swift b/Ferrite/ViewModels/DebridManager.swift index 382e319..c388f17 100644 --- a/Ferrite/ViewModels/DebridManager.swift +++ b/Ferrite/ViewModels/DebridManager.swift @@ -157,7 +157,7 @@ class DebridManager: ObservableObject { func selectDebridResult(magnet: Magnet) -> Bool { guard let magnetHash = magnet.hash else { - logManager?.error("DebridManager: Could not find the torrent magnet hash") + logManager?.error("DebridManager: Could not find the magnet hash") return false } @@ -415,9 +415,9 @@ class DebridManager: ObservableObject { if bypassTTL || Date().timeIntervalSince1970 > selectedSource.cloudTTL { do { - // Populates the inner downloads and torrent arrays + // Populates the inner downloads and magnet arrays try await selectedSource.getUserDownloads() - try await selectedSource.getUserTorrents() + try await selectedSource.getUserMagnets() // Update the TTL to 5 minutes from now selectedSource.cloudTTL = Date().timeIntervalSince1970 + 300 @@ -436,7 +436,7 @@ class DebridManager: ObservableObject { } do { - try await selectedSource.deleteDownload(downloadId: download.downloadId) + try await selectedSource.deleteUserDownload(downloadId: download.downloadId) await fetchDebridCloud(bypassTTL: true) } catch { @@ -456,19 +456,19 @@ class DebridManager: ObservableObject { } } - func deleteCloudTorrent(_ torrent: DebridCloudTorrent) async { + func deleteUserMagnet(_ cloudMagnet: DebridCloudMagnet) async { guard let selectedSource = selectedDebridSource else { return } do { - try await selectedSource.deleteTorrent(torrentId: torrent.torrentId) + try await selectedSource.deleteUserMagnet(cloudMagnetId: cloudMagnet.cloudMagnetId) await fetchDebridCloud(bypassTTL: true) } catch { switch error { case DebridError.NotImplemented: - let message = "Torrent deletion for \(selectedSource.id) is not implemented. Please use the service's website." + let message = "Magnet deletion for \(selectedSource.id) is not implemented. Please use the service's website." notImplementedMessage = message showNotImplementedAlert.toggle() @@ -477,7 +477,7 @@ class DebridManager: ObservableObject { showToast: false ) default: - await sendDebridError(error, prefix: "\(selectedSource.id) torrent delete error") + await sendDebridError(error, prefix: "\(selectedSource.id) magnet delete error") } } } diff --git a/Ferrite/Views/ComponentViews/Library/Cloud/CloudTorrentView.swift b/Ferrite/Views/ComponentViews/Library/Cloud/CloudMagnetView.swift similarity index 77% rename from Ferrite/Views/ComponentViews/Library/Cloud/CloudTorrentView.swift rename to Ferrite/Views/ComponentViews/Library/Cloud/CloudMagnetView.swift index 07acfc1..8da3066 100644 --- a/Ferrite/Views/ComponentViews/Library/Cloud/CloudTorrentView.swift +++ b/Ferrite/Views/ComponentViews/Library/Cloud/CloudMagnetView.swift @@ -1,5 +1,5 @@ // -// CloudTorrentView.swift +// CloudMagnetView.swift // Ferrite // // Created by Brian Dashore on 6/6/24. @@ -7,7 +7,7 @@ import SwiftUI -struct CloudTorrentView: View { +struct CloudMagnetView: View { @EnvironmentObject var navModel: NavigationViewModel @EnvironmentObject var debridManager: DebridManager @EnvironmentObject var pluginManager: PluginManager @@ -17,27 +17,27 @@ struct CloudTorrentView: View { @Binding var searchText: String var body: some View { - DisclosureGroup("Torrents") { - ForEach(debridSource.cloudTorrents.filter { + DisclosureGroup("Magnets") { + ForEach(debridSource.cloudMagnets.filter { searchText.isEmpty ? true : $0.fileName.lowercased().contains(searchText.lowercased()) - }, id: \.self) { cloudTorrent in + }, id: \.self) { cloudMagnet in Button { - if cloudTorrent.status == "downloaded", !cloudTorrent.links.isEmpty { + if cloudMagnet.status == "downloaded", !cloudMagnet.links.isEmpty { navModel.resultFromCloud = true - navModel.selectedTitle = cloudTorrent.fileName + navModel.selectedTitle = cloudMagnet.fileName var historyInfo = HistoryEntryJson( - name: cloudTorrent.fileName, + name: cloudMagnet.fileName, source: debridSource.id ) Task { - let magnet = Magnet(hash: cloudTorrent.hash, link: nil) + let magnet = Magnet(hash: cloudMagnet.hash, link: nil) await debridManager.populateDebridIA([magnet]) if debridManager.selectDebridResult(magnet: magnet) { // Is this a batch? - if cloudTorrent.links.count == 1 { + if cloudMagnet.links.count == 1 { await debridManager.fetchDebridDownload(magnet: magnet) // Bump to batch @@ -67,15 +67,15 @@ struct CloudTorrentView: View { } } label: { VStack(alignment: .leading, spacing: 10) { - Text(cloudTorrent.fileName) + Text(cloudMagnet.fileName) .font(.callout) .fixedSize(horizontal: false, vertical: true) .lineLimit(4) HStack { - Text(cloudTorrent.status.capitalizingFirstLetter()) + Text(cloudMagnet.status.capitalizingFirstLetter()) Spacer() - DebridLabelView(debridSource: debridSource, cloudLinks: cloudTorrent.links) + DebridLabelView(debridSource: debridSource, cloudLinks: cloudMagnet.links) } .font(.caption) } @@ -85,9 +85,9 @@ struct CloudTorrentView: View { } .onDelete { offsets in for index in offsets { - if let cloudTorrent = debridSource.cloudTorrents[safe: index] { + if let cloudMagnet = debridSource.cloudMagnets[safe: index] { Task { - await debridManager.deleteCloudTorrent(cloudTorrent) + await debridManager.deleteUserMagnet(cloudMagnet) } } } diff --git a/Ferrite/Views/ComponentViews/Library/DebridCloudView.swift b/Ferrite/Views/ComponentViews/Library/DebridCloudView.swift index 9a612a8..63a5932 100644 --- a/Ferrite/Views/ComponentViews/Library/DebridCloudView.swift +++ b/Ferrite/Views/ComponentViews/Library/DebridCloudView.swift @@ -20,8 +20,8 @@ struct DebridCloudView: View { CloudDownloadView(debridSource: debridSource, searchText: $searchText) } - if !debridSource.cloudTorrents.isEmpty { - CloudTorrentView(debridSource: debridSource, searchText: $searchText) + if !debridSource.cloudMagnets.isEmpty { + CloudMagnetView(debridSource: debridSource, searchText: $searchText) } } .listStyle(.plain) diff --git a/Ferrite/Views/ComponentViews/SearchResult/SearchResultButtonView.swift b/Ferrite/Views/ComponentViews/SearchResult/SearchResultButtonView.swift index 5e41b5a..d523bee 100644 --- a/Ferrite/Views/ComponentViews/SearchResult/SearchResultButtonView.swift +++ b/Ferrite/Views/ComponentViews/SearchResult/SearchResultButtonView.swift @@ -125,7 +125,7 @@ struct SearchResultButtonView: View { .alert("Caching file", isPresented: $debridManager.showDeleteAlert) { Button("Yes", role: .destructive) { Task { - try? await debridManager.selectedDebridSource?.deleteTorrent(torrentId: nil) + try? await debridManager.selectedDebridSource?.deleteUserMagnet(cloudMagnetId: nil) } } Button("Cancel", role: .cancel) {}