From 91f124130cd46dcbf4f90c3732586c49aed98e99 Mon Sep 17 00:00:00 2001 From: kingbri Date: Thu, 6 Jun 2024 10:44:44 -0400 Subject: [PATCH] Debrid: Fix cache alert Change the returned error to one that's unique to caching. Also make deleteTorrents optional to delete the first torrent if necessary since that's always being cached. Signed-off-by: kingbri --- Ferrite/API/AllDebridWrapper.swift | 6 ++++- Ferrite/API/PremiumizeWrapper.swift | 2 +- Ferrite/API/RealDebridWrapper.swift | 23 +++++++++++++++---- Ferrite/Models/DebridModels.swift | 1 + Ferrite/Protocols/Debrid.swift | 2 +- Ferrite/ViewModels/DebridManager.swift | 10 ++++++-- .../SearchResult/SearchResultButtonView.swift | 5 ++-- 7 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Ferrite/API/AllDebridWrapper.swift b/Ferrite/API/AllDebridWrapper.swift index cf533f9..7778aa2 100644 --- a/Ferrite/API/AllDebridWrapper.swift +++ b/Ferrite/API/AllDebridWrapper.swift @@ -323,7 +323,11 @@ public class AllDebrid: PollingDebridSource, ObservableObject { return cloudTorrents } - public func deleteTorrent(torrentId: String) async throws { + public func deleteTorrent(torrentId: String?) async throws { + guard let torrentId else { + throw DebridError.FailedRequest(description: "The torrentID \(String(describing: torrentId)) is invalid") + } + let queryItems = [ URLQueryItem(name: "id", value: torrentId) ] diff --git a/Ferrite/API/PremiumizeWrapper.swift b/Ferrite/API/PremiumizeWrapper.swift index fbbf6aa..468f406 100644 --- a/Ferrite/API/PremiumizeWrapper.swift +++ b/Ferrite/API/PremiumizeWrapper.swift @@ -359,5 +359,5 @@ public class Premiumize: OAuthDebridSource, ObservableObject { [] } - public func deleteTorrent(torrentId: String) async throws {} + public func deleteTorrent(torrentId: String?) async throws {} } diff --git a/Ferrite/API/RealDebridWrapper.swift b/Ferrite/API/RealDebridWrapper.swift index 798bb68..daf42b0 100644 --- a/Ferrite/API/RealDebridWrapper.swift +++ b/Ferrite/API/RealDebridWrapper.swift @@ -408,9 +408,9 @@ public class RealDebrid: PollingDebridSource, ObservableObject { if let torrentLink = rawResponse.links[safe: linkIndex ?? -1], rawResponse.status == "downloaded" { return torrentLink } else if rawResponse.status == "downloading" || rawResponse.status == "queued" { - throw DebridError.EmptyTorrents + throw DebridError.IsCaching } else { - throw DebridError.EmptyData + throw DebridError.EmptyTorrents } } @@ -454,8 +454,23 @@ public class RealDebrid: PollingDebridSource, ObservableObject { } // Deletes a torrent download from RD - public func deleteTorrent(torrentId: String) async throws { - var request = URLRequest(url: URL(string: "\(baseApiUrl)/torrents/delete/\(torrentId)")!) + public func deleteTorrent(torrentId: String?) async throws { + let deleteId: String + + if let torrentId { + deleteId = torrentId + } else { + // Refresh the torrent cloud + // The first file is the currently caching one + let _ = try await getUserTorrents() + guard let firstTorrent = cloudTorrents[safe: -1] else { + throw DebridError.EmptyTorrents + } + + deleteId = firstTorrent.torrentId + } + + var request = URLRequest(url: URL(string: "\(baseApiUrl)/torrents/delete/\(deleteId)")!) request.httpMethod = "DELETE" try await performRequest(request: &request, requestName: #function) diff --git a/Ferrite/Models/DebridModels.swift b/Ferrite/Models/DebridModels.swift index 9bce883..31ea356 100644 --- a/Ferrite/Models/DebridModels.swift +++ b/Ferrite/Models/DebridModels.swift @@ -51,6 +51,7 @@ public enum DebridError: Error { case InvalidToken case EmptyData case EmptyTorrents + case IsCaching case FailedRequest(description: String) case AuthQuery(description: String) } diff --git a/Ferrite/Protocols/Debrid.swift b/Ferrite/Protocols/Debrid.swift index 2012492..3c0570c 100644 --- a/Ferrite/Protocols/Debrid.swift +++ b/Ferrite/Protocols/Debrid.swift @@ -45,7 +45,7 @@ public protocol DebridSource: AnyObservableObject { // User torrent functions func getUserTorrents() async throws -> [DebridCloudTorrent] - func deleteTorrent(torrentId: String) async throws + func deleteTorrent(torrentId: String?) async throws } public protocol PollingDebridSource: DebridSource { diff --git a/Ferrite/ViewModels/DebridManager.swift b/Ferrite/ViewModels/DebridManager.swift index b811d0f..06e50e8 100644 --- a/Ferrite/ViewModels/DebridManager.swift +++ b/Ferrite/ViewModels/DebridManager.swift @@ -483,8 +483,14 @@ public class DebridManager: ObservableObject { // TODO: Add common fetch cloud method //await fetchRdCloud(bypassTTL: true) } catch { - // TODO: Fix error types and unify errors - print("Error \(error)") + switch error { + case DebridError.IsCaching: + showDeleteAlert.toggle() + default: + await sendDebridError(error, prefix: "\(debridSource.id) download error", cancelString: "Download cancelled") + } + + logManager?.hideIndeterminateToast() } } diff --git a/Ferrite/Views/ComponentViews/SearchResult/SearchResultButtonView.swift b/Ferrite/Views/ComponentViews/SearchResult/SearchResultButtonView.swift index efe438b..fa2d24e 100644 --- a/Ferrite/Views/ComponentViews/SearchResult/SearchResultButtonView.swift +++ b/Ferrite/Views/ComponentViews/SearchResult/SearchResultButtonView.swift @@ -127,13 +127,14 @@ struct SearchResultButtonView: View { .alert("Caching file", isPresented: $debridManager.showDeleteAlert) { Button("Yes", role: .destructive) { Task { - await debridManager.deleteRdTorrent() + try? await debridManager.selectedDebridSource?.deleteTorrent(torrentId: nil) } } Button("Cancel", role: .cancel) {} } message: { Text( - "RealDebrid is currently caching this file. Would you like to delete it? \n\n" + + "\(String(describing: debridManager.selectedDebridSource?.id)) is currently caching this file. " + + "Would you like to delete it? \n\n" + "Progress can be checked on the RealDebrid website." ) }