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 <bdashore3@proton.me>
This commit is contained in:
kingbri 2024-06-06 10:44:44 -04:00
parent ec8455c08d
commit 91f124130c
7 changed files with 38 additions and 11 deletions

View file

@ -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)
]

View file

@ -359,5 +359,5 @@ public class Premiumize: OAuthDebridSource, ObservableObject {
[]
}
public func deleteTorrent(torrentId: String) async throws {}
public func deleteTorrent(torrentId: String?) async throws {}
}

View file

@ -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)

View file

@ -51,6 +51,7 @@ public enum DebridError: Error {
case InvalidToken
case EmptyData
case EmptyTorrents
case IsCaching
case FailedRequest(description: String)
case AuthQuery(description: String)
}

View file

@ -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 {

View file

@ -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()
}
}

View file

@ -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."
)
}